The following is the coding that I have done for the program. I am receiving err
ID: 3621938 • Letter: T
Question
The following is the coding that I have done for the program.
I am receiving errors in the anagram::anagram and the ostream& sections of the coding.
The error in the anagram::anagram section states that on this line:
"if(!containsKey(anagramMap.repn))"
there is no member named repn.
The error in the ostream& section states that "mapIter" and "setIter" are not declared in the proper scopes. They are both iterator problems.
I appreciate any feedback regarding this problem. Thanks.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
class anagram {
private:
map<string, set<string> > anagramMap;
typedef map<string, set<string> >::const_iterator mapIter;
typedef set<string>::const_iterator setIter;
int threshold;
/**
Returns true iff key is a key value in the map m.
@param m the map searched for the specified key
@param key the string searched for as a key within m
*/
bool containsKey(const map<string, set<string> >& m, string key);
/**
Sorts (by simple selection) the vector v in ascending order.
@param v a vector
*/
void sort(vector<string>& sv);
/**
Returns the contents of word re-arraigned in sort order (the
"representation" of the word).
@param word a simple word
*/
string wordToRepresentation(string word);
public:
/**
Constructs sets of anagrams from a list of words read from a file.
@param in the input stream containing the words - one per line
@param limit the minimal number of anagrams in each set
*/
anagram(istream& in, int limit);
/*
* prints the problem in the following form:
*
* Anagram Lists (Each Limited to __ Anagrams):
* list 1
* <blank line>
* list 2
* <blank line>
* ...
* <blank line>
* list n
*
* Each anagram in each list is separated from the others by a space or two.
*/
friend ostream& operator<<(ostream& out, const anagram& anagrams);
};
bool anagram::containsKey(const map<string, set<string> >& m, string key)
{
return m.count(key) != 0;
}
void anagram::sort(vector<string>& sv)
{
for (int i = 0; i < sv.size(); i++)
{
int minindex = i;
for (int j = i+1; j < sv.size(); j++)
if (sv[j] < sv[minindex])
minindex = j;
string temp = sv[i];
sv[i] = sv[minindex];
sv[minindex] = temp;
}
}
string anagram::wordToRepresentation(string word)
{
vector<string> sv;
for (int i = 0; i < word.length(); i++)
sv.push_back(word.substr(i, 1));
sort(sv);
string result;
for (int i = 0; i < word.length(); i++)
result = result + sv[i];
return result;
}
anagram::anagram(istream& in, int limit)
{
threshold = limit;
string word; // word read into string
string repn; // representation
while (getline(in, word))
{
repn = wordToRepresentation(word);
if(!containsKey(anagramMap.repn))
{
set<string> s;
anagramMap[repn] = s;
}
anagramMap[repn].insert(word);
}
}
ostream& operator<<(ostream& out, const anagram& anagrams)
{
out << "Anagram Lists (Each Limited to "
<< anagrams.threshold
<< " Anagrams):" << endl;
mapIter = anagramMap.begin();
setIter = anagramMap.s.begin();
while (mapIter != anagramMap.end())
{
while (setIter != anagramMap.s.end())
{
out << anagramMap.s[setIter] << " ";
setIter++;
}
out << endl << endl;
mapIter++;
}
return out;
}
int main()
{
ifstream words;
words.open("words.dat");
if (words.fail()) {
cout << "error opening words.dat for reading ";
return 1;
}
anagram anagrams(words, 2);
cout << anagrams;
return 0;
}
Explanation / Answer
Dear, #include #include #include #include #include #include using namespace std; class anagram { private: map anagramMap; typedef map::const_iterator mapIter; typedef set::const_iterator setIter; int threshold; /** Returns true iff key is a key value in the map m. @param m the map searched for the specified key @param key the string searched for as a key within m */ bool containsKey(const map& m, string key); /** Sorts (by simple selection) the vector v in ascending order. @param v a vector */ void sort(vector& sv); /** Returns the contents of word re-arraigned in sort order (the "representation" of the word). @param word a simple word */ string wordToRepresentation(string word); public: /** Constructs sets of anagrams from a list of words read from a file. @param in the input stream containing the words - one per line @param limit the minimal number of anagrams in each set */ anagram(istream& in, int limit); /* * prints the problem in the following form: * * Anagram Lists (Each Limited to __ Anagrams): * list 1 * * list 2 * * ... * * list n * * Each anagram in each list is separated from the others by a space or two. */ friend ostream& operatorRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.