Práctica 5. Set y Map.
guiatlf.cpp
Ir a la documentación de este archivo.
1 
8 #include "guiatlf.h"
9 
17 istream & operator>>(istream &is,pair<string,string> &d){
18 
19  getline(is,d.first,'\t');
20  getline(is,d.second);
21  return is;
22 }
23 
30 ostream & operator<<(ostream &os,const pair<const string,string> &d){
31 
32  os<<d.first<<'\t'<<d.second<<endl;
33  return os;
34 }
35 
36 
37 //Acceso a un elemento con operador
38 string & Guia_Tlf::operator[](const string &nombre) {
39  return datos[nombre];
40 }
41 
42 //acceso a un elemento con iteradores
43 string Guia_Tlf::gettelefono(const string & nombre){
44  map<string,string>::iterator it=datos.find(nombre);
45  if (it==datos.end())
46  return string("");
47  else return it->second;
48 }
49 
50 // Insert un nuevo telefono
51 pair<map<string,string>::iterator,bool> Guia_Tlf::insert(string nombre, string tlf){
52  pair<string,string> p (nombre,tlf);
53  pair<map<string,string> ::iterator,bool> ret;
54 
55  ret=datos.insert(p); //datos.insert(datos.begin(),p); tambien funcionaría
56  return ret;
57 }
58 
59 // Insert un nuevo telefono
60 pair<map<string,string>::iterator,bool> Guia_Tlf::insert(pair<string,string> p){
61  pair<map<string,string> ::iterator,bool> ret;
62 
63  ret=datos.insert(p); //datos.insert(datos.begin(),p); tambien funcionaría
64  return ret;
65 }
66 
67 //Borrar un telefono
68 void Guia_Tlf::borrar(const string &nombre){
69  map<string,string>::iterator itlow = datos.lower_bound(nombre);//el primero que tiene dicho nombre
70  map<string,string>::iterator itupper = datos.upper_bound(nombre);//el primero que ya no tiene dicho nombre
71  datos.erase(itlow,itupper);//borramos todos aquellos que tiene dicho nombre
72  //OTRA ALTERNATIVA
73  // pair<map<string,string>::iterator,map<string,string>::iterator>ret;
74  //ret = datos.equal_range(nombre
75  //datos.erase(ret.first,ret.second);
76 }
77 
78 // Borrar un telefono otro
79 //con map siempre hay uno con multimap puede existir mas de uno
80 void Guia_Tlf::borrar(const string &nombre,const string &tlf){
81  map<string,string>::iterator itlow = datos.lower_bound(nombre);//el primero que tiene dicho nombre
82  map<string,string>::iterator itupper = datos.upper_bound(nombre);//el primero que ya no tiene dicho nombre
83  map<string,string>::iterator it;
84  bool salir =false;
85  for (it=itlow; it!=itupper && !salir;++it){
86  if (it->second==tlf){
87  datos.erase(it);
88  salir =true;
89  }
90  }
91 }
92 
93 //Union de guias de telefonos
95  Guia_Tlf aux(*this);
96  map<string,string>::const_iterator it;
97  for (it=g.datos.begin();it!=g.datos.end();++it){
98  aux.insert(it->first,it->second);
99  }
100 
101  return aux;
102 }
103 
104 //Diferencia de guias de telefonos
106  Guia_Tlf aux(*this);
107  map<string,string>::const_iterator it;
108  for (it=g.datos.begin();it!=g.datos.end();++it){
109  aux.borrar(it->first,it->second);
110  }
111 
112  return aux;
113 }
114 
115 //Obtiene una guia con los nombre previos a uno dado
116 Guia_Tlf Guia_Tlf::previos(const string &nombre,const string &tlf){
117  map<string,string>::value_compare vc=datos.value_comp(); //map<string,string>::key_compare vc=datos.key_comp()
118  Guia_Tlf aux;
119  pair<string,string>p(nombre,tlf);
120  map<string,string>::iterator it=datos.begin();
121  while (vc(*it,p)){
122  aux.insert(*it++);
123  }
124 
125  return aux;
126 }
127 
129  Guia_Tlf guia_inter; // Guia para la interasección
130 
131  map<string,string>::const_iterator it = this->datos.cbegin();
132  while (it != this->datos.cend()) {
133  string nombre_que_comparten = it->first;
134  string telefono_del_elemento = it->second;
135 
136  // Par con iteradores que determinan el rango de elementos en la guia que tienen ese nombre
137  pair<map<string,string>::const_iterator, map<string,string>::const_iterator> ret;
138  ret = guia.datos.equal_range(nombre_que_comparten);
139  map<string,string>::const_iterator itb = ret.first;
140 
141  while (itb != ret.second) { // Metemos solo aquellos en los que el telefono tambien coindicida
142  if (itb->second == telefono_del_elemento) guia_inter.insert(nombre_que_comparten,telefono_del_elemento);
143  itb++;
144  }
145  it++;
146  }
147  return guia_inter;
148 }
149 
150 bool Guia_Tlf::cambiar_tlf(const std::string &nombre, const std::string &tlf) {
151  // Asumimos que se usa map, si fuese multimap usamos equal_range e iteramos
152  bool encontrado = (bool)this->datos.count(nombre);
153  if (encontrado) this->datos[nombre] = tlf;
154  return encontrado;
155 }
156 
158  Guia_Tlf guia_ret;
159  size_t found;
160  map<string,string>::iterator it = this->datos.begin();
161  while (it != this->datos.end()) {
162  string nombre = it->first;
163  found = nombre.find(str);
164  if (found != string::npos && found == 0) guia_ret.insert(nombre,it->second);
165  it++;
166  }
167  return guia_ret;
168 }
169 
170 
171 // Escritura de una guia de telefonos
172 ostream & operator<<(ostream & os, const Guia_Tlf & g){
173  map<string,string>::const_iterator it;
174  for (it=g.datos.begin(); it!=g.datos.end();++it)
175  os<<it->first<<"\t"<<it->second<<endl;
176 
177  return os;
178 }
179 
180 // Lectura de una guia de telefonos
181 istream & operator>>(istream & is, Guia_Tlf & g){
182  pair<string,string> p;
183  Guia_Tlf aux;
184 
185  while (is>>p)
186  aux.insert(p);
187 
188  g=aux;
189  return is;
190 }
191 
192 
clase para iterar sobre la guia
Definition: guiatlf.h:178
TDA Guia_Tlf.
Definition: guiatlf.h:26
Guia_Tlf previos(const string &nombre, const string &tlf)
Obtiene una guia con los nombre previos a uno dado.
Definition: guiatlf.cpp:116
bool cambiar_tlf(const string &nombre, const string &tlf)
Cambia el teléfono asociado a un nombre.
Definition: guiatlf.cpp:150
string gettelefono(const string &nombre)
Acceso a un elemento.
Definition: guiatlf.cpp:43
Guia_Tlf interseccion(const Guia_Tlf &guia)
Obtiene una guía con los datos comunes a this y guia.
Definition: guiatlf.cpp:128
Guia_Tlf operator+(const Guia_Tlf &g)
Union de guias de teléfonos.
Definition: guiatlf.cpp:94
void borrar(const string &nombre)
Borrar un teléfono.
Definition: guiatlf.cpp:68
string & operator[](const string &nombre)
Acceso a un elemento.
Definition: guiatlf.cpp:38
Guia_Tlf operator-(const Guia_Tlf &g)
Diferencia de guias de teléfonos.
Definition: guiatlf.cpp:105
pair< map< string, string >::iterator, bool > insert(string nombre, string tlf)
Insert un nuevo teléfono.
Definition: guiatlf.cpp:51
Guia_Tlf guia_con_nombres_comenzando(const string &str)
Obtiene una guía con los nombres que empiezan por una cadena.
Definition: guiatlf.cpp:157
istream & operator>>(istream &is, pair< string, string > &d)
Sobrecarga del operador de entrada para el pair de strings.
Definition: guiatlf.cpp:17
ostream & operator<<(ostream &os, const pair< const string, string > &d)
Sobrecarga del operador de salida para el pair de strings.
Definition: guiatlf.cpp:30
TDA guia de teléfonos.