Práctica 5. Set y Map.
dictionary.cpp
Ir a la documentación de este archivo.
1 
9 #include "dictionary.h"
10 #include <iostream>
11 #include <vector>
12 #include <fstream>
13 
14 using namespace std;
15 
16 ostream& operator<<(ostream& os, const Dictionary& dic){
17  for (Dictionary::iterator it = dic.begin(); it != dic.end(); ++it){
18  os << *it << endl;
19  }
20  return os;
21 }
22 
23 istream& operator>>(istream& is, Dictionary& dic) {
24  dic.clear();
25  string wrd;
26  while (is >> wrd)
27  dic.insert(wrd);
28 
29  return is;
30 }
31 
33 
35  this->words = othr.words;
36 }
37 
38 bool Dictionary::exists(const string &s) const {return (bool)words.count(s);}
39 
40 bool Dictionary::insert(const string &s) {
41  pair<set<string>::iterator, bool> ret = words.insert(s);
42  return ret.second;
43 }
44 
45 bool Dictionary::erase(const std::string &s) {
46  return (bool) this->words.erase(s);
47 }
48 
49 void Dictionary::clear() {this->words.clear();}
50 
51 bool Dictionary::empty() const {return this->words.empty();}
52 
53 unsigned int Dictionary::size() const {return this->words.size();}
54 
55 vector<string> Dictionary::wordsOfLength(int length) const {
56  vector<string> v_ret;
57  for (Dictionary::iterator it = this->begin(); it != this->end(); ++it)
58  if ((*it).length() == length) v_ret.push_back(*it);
59 
60  return v_ret;
61 }
62 
63 int count_ocurrences(char c, const string& word, size_t pos) {
64  int cont = 0;
65  size_t s = word.find(c,pos);
66  if (s != string::npos) {
67  cont++;
68  cont += count_ocurrences(c,word,s+1);
69  }
70  return cont;
71 }
72 
73 int Dictionary::getOcurrences(char c) const {
74  int cont = 0;
75  for (Dictionary::iterator it = this->begin(); it != this->end(); ++it)
76  cont += count_ocurrences(c,*it);
77 
78  return cont;
79 }
80 
81 void Dictionary::anade(const Dictionary &dic) {
82  for (Dictionary::iterator it = dic.begin(); it != dic.end(); ++it)
83  this->insert(*it);
84 
85 }
86 
87 
88 Dictionary::Dictionary(const string & file_name){
89  LoadDictionary(file_name);
90 }
91 
92 
93 void Dictionary::LoadDictionary(const std::string &file_name) {
94  ifstream is;
95  is.open(file_name);
96  if (!is.is_open()) {
97  cerr << "Error opening the file " << file_name;
98  exit(1);
99  }
100  is >> *this;
101 }
Clase para iterar sobre el diccionario.
Definition: dictionary.h:141
TDA Dictionary.
Definition: dictionary.h:25
bool insert(const string &s)
Inserta una palabra en el diccionario.
Definition: dictionary.cpp:40
void clear()
Borra todos los elementos del diccionario.
Definition: dictionary.cpp:49
bool exists(const string &s) const
Comprueba si una palabra existe en el diccionario.
Definition: dictionary.cpp:38
iterator begin() const
Devuelve un iterador al principio del diccionario.
Definition: dictionary.h:157
void anade(const Dictionary &dic)
Une dos diccionarios en uno.
Definition: dictionary.cpp:81
bool empty() const
Comprueba si el diccionario está vacío.
Definition: dictionary.cpp:51
unsigned int size() const
Devuelve el número de palabras del diccionario.
Definition: dictionary.cpp:53
vector< string > wordsOfLength(int length) const
Devuelve un vector con las palabras de una longitud dada.
Definition: dictionary.cpp:55
iterator end() const
Devuelve un iterador al final del diccionario.
Definition: dictionary.h:167
int getOcurrences(char c) const
Devuelve el número de ocurrencias de un carácter en el diccionario.
Definition: dictionary.cpp:73
bool erase(const string &s)
Borra un elemento del diccionario.
Definition: dictionary.cpp:45
Dictionary()
Constructor por defecto de la clase.
Definition: dictionary.cpp:32
int count_ocurrences(char c, const string &word, size_t pos)
Función que cuenta el número de ocurrencias de un carácter en una palabra.
Definition: dictionary.cpp:63
istream & operator>>(istream &is, Dictionary &dic)
Definition: dictionary.cpp:23
ostream & operator<<(ostream &os, const Dictionary &dic)
Definition: dictionary.cpp:16
TDA Dictionary.