0
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’
8
  • you already have a variable called it (vector<string>::iterator it; ). Call your multimap iterator something else... Commented Aug 13, 2015 at 13:47
  • You could rename one of the two things you named it as the earlier comment and answers suggest. But it is better style to limit the scope of each use of it rather than vary the name. for(auto it=m.begin();it<m.end();it++) or for(multimap<string,int>::iterator it=m.begin();it<m.end();it++) instead of declaring it seperately before use. Commented Aug 13, 2015 at 13:50
  • Thanks, I always do make such silly mistakes Commented Aug 13, 2015 at 13:51
  • That's not the only error you're getting, and not even the full error. The compiler tells you which line it's on, and also says 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. Commented Aug 13, 2015 at 13:55
  • For God's sake use typedefs Commented Aug 13, 2015 at 13:58

2 Answers 2

2

You are declaring the same variable (it) twice with different types. Use a different variable name for one of the declarations.

Sign up to request clarification or add additional context in comments.

Comments

2

You have declared it twice. first vector<string>::iterator it; and then with multimap<string,int>::iterator it;. You will need to rename one of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.