#include <vector> #include <iostream> class Dico { public: Dico(); ~Dico(); bool load(const std::string& filename); const std::string& operator[](int i) const; int size() const; private: std::vector<std::string> _dico; }; Dico::Dico() { } Dico::~Dico() { } bool Dico::load(const std::string& filename) { std::ifstream file(filename.c_str()); if (!file) return false; std::string line; while (std::getline(file, line)) _dico.push_back(line); return true; } const std::string& Dico::operator[](int i) const { return _dico[i]; } int Dico::size() const { return _dico.size(); } int main() { std::string filename = "dico.txt"; Dico dico; if (!dico.load(filename)) { std::cerr << "Error while loading dictionnary: " << filename << std::endl; } const int size = dico.size(); for (int i = 0; i < size; ++i) std::cout << "dico[" << i << "] = " << dico[i] << std::endl; return 0; }
no match for 'operator>>' (operand types are 'std::ofstream {aka std::basic_ofstream<char>}' and 'std::string {aka std::basic_string<char>}')
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question#ifndef UTILS_HH_ # define UTILS_HH_ # include <string> namespace Utils { std::string renarrow(const std::string& s); std::string cleanAndSort(const std::string& word); } // Utils #endif /* !UTILS_HH_ */
#include "Utils.hh" #include <sstream> #include <algorithm> namespace Utils { std::string renarrow(const std::string& s) { std::ostringstream ws; unsigned int length = s.length(); for (unsigned int i = 0; i < length; i++) { if (i + 1 < length && s[i] == -61) { switch (s[i + 1]) { case -94: ws << "a"; break; case -96: ws << "a"; break; case -89: ws << "c"; break; case -85: ws << "e"; break; case -87: ws << "e"; break; case -86: ws << "e"; break; case -88: ws << "e"; break; case -81: ws << "i"; break; case -82: ws << "i"; break; case -76: ws << "o"; break; case -69: ws << "u"; break; case -71: ws << "u"; break; } i++; } else { switch (s[i]) { case -30: ws << "a"; break; case -28: ws << "a"; break; case -32: ws << "a"; break; case -25: ws << "c"; break; case -21: ws << "e"; break; case -23: ws << "e"; break; case -22: ws << "e"; break; case -24: ws << "e"; break; case -17: ws << "i"; break; case -18: ws << "i"; break; case -12: ws << "o"; break; case -5: ws << "u"; break; case -7: ws << "u"; break; default : ws << s[i]; } } } return ws.str(); } std::string cleanAndSort(const std::string& word) { std::string res = renarrow(word); std::sort(res.begin(), res.end()); return res; } } // Utils
#ifndef COMBINATION_HH_ # define COMBINATION_HH_ # include <string> # include <cassert> # include <sstream> # include <vector> # include <stdint.h> class Combinations { public: typedef std::vector<uint32_t> combination_type; Combinations(uint32_t maxIndex, uint32_t width); combination_type next(); bool completed() const; bool generated() const; private: bool _completed; uint32_t _generated; uint32_t _maxIndex; int32_t _width; combination_type _curr; }; #endif /* !COMBINATION_HH_ */
#include "Combinations.hh" Combinations::Combinations(uint32_t maxIndex, uint32_t width) : _completed(maxIndex < 1 || width > maxIndex), _generated(false), _maxIndex(maxIndex), _width(width) { for (uint32_t c = 1; c <= width; ++c) _curr.push_back(c); } Combinations::combination_type Combinations::next() { combination_type ret = _curr; _completed = true; for (int32_t i = _width - 1; i >= 0; --i) { if (_curr[i] < _maxIndex - _width + i + 1) { uint32_t j = _curr[i] + 1; while (i <= _width) _curr[i++] = j++; _completed = false; ++_generated; break; } } return ret; } bool Combinations::completed() const { return _completed; } bool Combinations::generated() const { return _generated; }
#ifndef DICO_HH_ # define DICO_HH_ # include <string> # include <iostream> # include <unordered_map> # include <vector> # include <stdint.h> class Dico { public: typedef std::vector<std::string> Words; typedef std::unordered_map<std::string, Words*> DicoType; typedef DicoType::iterator iterator; typedef DicoType::const_iterator const_iterator; public: Dico(); ~Dico(); bool load(const std::string& filename); bool getListFromStrictLetters(std::string letters, Dico::Words*& list) const; bool getListFromPartialLetters(std::string letters, std::vector<Dico::Words*>& list, uint32_t width) const; void dump(std::ostream& out) const; private: void add(const std::string& letters); private: DicoType _dico; }; std::ostream& operator<<(std::ostream& out, const Dico& dico); #endif /* !DICO_HH_ */
#include "Dico.hh" #include "Utils.hh" #include "Combinations.hh" #include <fstream> #include <cassert> #include <sstream> #include <iostream> namespace { void generateGroupPermutation(const std::string& word, uint32_t width, std::vector<std::string>& tab) { uint32_t size = word.size(); Combinations cs(size, width); while (!cs.completed()) { Combinations::combination_type c = cs.next(); std::ostringstream buff; auto end = c.cend(); for (auto it = c.cbegin(); it != end; ++it) buff << word[*it - 1]; tab.push_back(buff.str()); } } } // namespace Dico::Dico() { } Dico::~Dico() { auto end = _dico.end(); for (auto it = _dico.begin(); it != end; ++it) delete it->second; } bool Dico::getListFromStrictLetters(std::string letters, Dico::Words*& list) const { const std::string word = Utils::cleanAndSort(letters); auto found = _dico.find(word); if (found == _dico.cend()) return false; list = found->second; return true; } bool Dico::getListFromPartialLetters(std::string letters, std::vector<Dico::Words*>& list, uint32_t width) const { Dico::Words* words = 0; const std::string word = Utils::cleanAndSort(letters); uint32_t size = word.size(); if (size < width) width = size; std::vector<std::string> tab; generateGroupPermutation(letters, width, tab); for (auto it = tab.begin(); it != tab.end(); ++it) if (getListFromStrictLetters(*it, words)) list.push_back(words); return !list.empty(); } bool Dico::load(const std::string& filename) { std::ifstream file(filename.c_str()); if (!file) return false; std::string line; while (std::getline(file, line)) add(line); return true; } void Dico::add(const std::string& letters) { const std::string word = Utils::cleanAndSort(letters); auto found = _dico.find(word); if (found == _dico.cend()) { auto pair = _dico.insert(DicoType::value_type(word, new Dico::Words)); if (!pair.second) return; found = pair.first; } found->second->push_back(letters); } void Dico::dump(std::ostream& out) const { auto end = _dico.cend(); for (auto it = _dico.cbegin(); it != end; ++it) { out << "[" << it->first << "]: "; uint32_t i = 0; bool first = true; auto vec_end = it->second->cend(); for (auto vec = it->second->cbegin(); vec != vec_end; ++vec, ++i) { if (first) first = false; else out << ", "; out << *vec; } out << std::endl; } } std::ostream& operator<<(std::ostream& out, const Dico& dico) { dico.dump(out); return out; }
#include "Utils.hh" #include "Dico.hh" #include <iostream> int main() { std::string filename = "dico.txt"; Dico dico; if (!dico.load(filename)) { std::cerr << "Error while loading dictionnary: " << filename << std::endl; return 1; } //std::cout << "Dico:\n" << dico << std::endl; Dico::Words* words = 0; if (dico.getListFromStrictLetters("cone", words)) { for (auto it = words->cbegin(); it != words->cend(); ++it) std::cout << " " << *it; std::cout << std::endl; } std::vector<Dico::Words*> list; if (dico.getListFromPartialLetters("enexppcottvj", list, 4)) { for (auto vec = list.cbegin(); vec != list.cend(); ++vec) for (auto it = (*vec)->cbegin(); it != (*vec)->cend(); ++it) std::cout << " " << *it; std::cout << std::endl; } else std::cout << " not found!" << std::endl; return 0; }
boutonRechercher->setText(Dictionnaire[4].c_str());