map<string, vector<string>> reassignment of vector value
- by user2950936
I am trying to write a program that takes lines from an input file, sorts the lines into 'signatures' for the purpose of combining all words that are anagrams of each other. I have to use a map, storing the 'signatures' as the keys and storing all words that match those signatures into a vector of strings. Afterwards I must print all words that are anagrams of each other on the same line. Here is what I have so far:
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <fstream>
using namespace std;
string signature(const string&);
void printMap(const map<string, vector<string>>&);
int main(){
string w1,sig1;
vector<string> data;
map<string, vector<string>> anagrams;
map<string, vector<string>>::iterator it;
ifstream myfile;
myfile.open("words.txt");
while(getline(myfile, w1))
{
sig1=signature(w1);
anagrams[sig1]=data.push_back(w1); //to my understanding this should always work,
} //either by inserting a new element/key or
//by pushing back the new word into the vector<string> data
//variable at index sig1, being told that the assignment operator
//cannot be used in this way with these data types
myfile.close();
printMap(anagrams);
return 0;
}
string signature(const string& w)
{
string sig;
sig=sort(w.begin(), w.end());
return sig;
}
void printMap(const map& m)
{
for(string s : m)
{
for(int i=0;i<m->second.size();i++)
cout << m->second.at();
cout << endl;
}
}
The first explanation is working, didn't know it was that simple! However now my print function is giving me:
prob2.cc: In function âvoid printMap(const std::map<std::basic_string<char>, std::vector<std::basic_string<char> > >&)â:
prob2.cc:43:36: error: cannot bind âstd::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}â lvalue to âstd::basic_ostream<char>&&â
In file included from /opt/centos/devtoolset-1.1/root/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/iostream:40:0,
Tried many variations and they always complain about binding
void printMap(const map<string, vector<string>> &mymap)
{
for(auto &c : mymap)
cout << c.first << endl << c.second << endl;
}