vector<vector<string>> groupAnagrams(vector<string>& s) {
vector<vector<string>> res;
multimap<string,int> m;
vector<string>::iterator it;
for(it=s.begin();it!=s.end();it++){
sort((*it).begin(),(*it).end());
}
int i=0;
for(it=s.begin();it!=s.end();it++){
m.insert(pair<string,int>(*(it),i));
i++;
}
//sort each string
multimap<string,int>::iterator it;
sort(m.begin(),m.end());
//sort all strings
string st=s[0];
for(it=m.begin();it<m.end();it++){
vector<string> temp;
temp.push_back(s);
if((*it)->first == st){
temp.push_back((*it)->first);
}
res.push_back(temp);
if((*it)->first != st){
st=(*it)->first;
}
}
return res;
}
The code is for grouping anagrams. I used multimap for mapping the word to the initial index. sorted the individual word first and then sorted the entire set of words.The error i'm getting is
conflicting declaration ‘std::multimap<std::basic_string<char>, int>::iterator it’
vector<string>::iterator it;). Call your multimap iterator something else...itas the earlier comment and answers suggest. But it is better style to limit the scope of each use ofitrather than vary the name.for(auto it=m.begin();it<m.end();it++)orfor(multimap<string,int>::iterator it=m.begin();it<m.end();it++)instead of declaringitseperately before use.previous declaration as ‘std::vector<std::basic_string<char> >::iterator it’and tells you which line that previous declaration is on. So in short, just read the error messages properly.