c++ map of vector in structure -
c++ map of vector in structure -
the programme want anagram finder dictionary file , string input, function "anagrams" should homecoming me vector (one each size 1 max) of vector of words found in dictionary match combination of sub-words possible input anagram when create new dictionary function createdictionary, set every anagram in vector of string however, when want check these anagrams in anagrams function, don't know how access (line 74) in uniqueanagram, have every sub-anagram possible, ok, using
if (dict.words.find(it->second)){ cout << dict.words.find(it->second)->second[0] << endl; }
in loop (as test see if look write right words) leads me error , don't understand why:
in function 'std::vector, std::allocator >, std::allocator, > std::allocator > > >, std::allocator std::char_traits, std::allocator >, std::allocator std::char_traits, std::allocator > > > > > anagrams(const std::string&, const > dictionary&, int)':| error: not convert 'dict->dictionary::words.std::map<_key, _tp, _compare, _alloc>::find [with _key = std::basic_string, std::allocator >, _tp = std::vector, std::allocator >, std::allocator, std::allocator > > >, _compare = std::less, std::allocator > >, _alloc = std::allocator
i've been stuck lastly 10 hours on , can't take anymore, don't know how solve issue give thanks help, you'd save me life
//anagrams.h #ifndef anagrams_h_included #define anagrams_h_included #include <vector> #include <string> #include <map> using namespace std; struct dictionary{ map<string, vector<string> > words; }; dictionary createdictionary(const string&); vector<vector<string> > anagrams(const string&, const dictionary&, int); #endif // anagrams_h_included //anagrams.cpp #include "anagrams.h" #include <iostream> #include <fstream> #include <map> #include <vector> #include <algorithm> #include <iterator> using namespace std; string sortstring(string input); dictionary createdictionary(const string& filename){ dictionary dictionary; ifstream ifs; ifs.open(filename.c_str()); string word; while (ifs >> word){ string sortedword = sortstring(word); (dictionary.words[sortedword]).push_back(word); } homecoming dictionary; } string sortstring(string input){ vector<char> vectorword(input.begin(), input.end()); sort(vectorword.begin(), vectorword.end()); string sortedinput(vectorword.begin(), vectorword.end()); homecoming sortedinput; } vector<vector<string> > anagrams(const string& input, const dictionary& dict, int max){ vector<vector<string> > anagrams; size_t n = input.length(); (int r = 0; r < max + 1; r++){ vector<bool> v(n); fill(v.begin() + r, v.end(), true); map<string, string> uniqueanagram; { string word; (size_t = 0; < n; ++i) { if (!v[i]) { word = word + input[i]; } } word = sortstring(word); uniqueanagram[word] = word; } while (next_permutation(v.begin(), v.end())); vector<string> tempanagram; for(map<string, string>::iterator = uniqueanagram.begin(); != uniqueanagram.end(); it++){ if (dict.words.find(it->second)){ cout << dict.words.find(it->second)->second[0] << endl; } } sort(tempanagram.begin(), tempanagram.end()); anagrams.push_back(tempanagram); } vector<char> vectorword(input.begin(), input.end()); sort(vectorword.begin(), vectorword.end()); string sortedword(vectorword.begin(), vectorword.end()); // cout << (dict.words.find(sortedword)->second)[0] << endl; homecoming anagrams; } //main.cpp #include <iostream> #include <fstream> #include <string> #include <vector> #include "anagrams.h" using namespace std; int main() { string filename = "c:/dictionary.txt"; dictionary dictionary = createdictionary(filename); vector<vector<string> > anagram = anagrams("llohe", dictionary, 5); homecoming 0; }
map::find
returns iterator, not boolean. if key wasn't found, returns map's past-the-end iterator. instead of
if (dict.words.find(it->second))
you want
if (dict.words.find(it->second) != dict.words.end())
or
if (dict.words.count(it->second) != 0)
it more efficient store iterator don't need find key twice:
auto found = dict.words.find(it->second); if (found != dict.words.end()) { cout << found->second[0] << endl; }
c++ vector map iterator
Comments
Post a Comment