I am trying to use the C++ map with string vector as values and not sure if i am using the correct syntax to insert the string in the vector. Please look at the code below:
I have also tried the following: hashMap.insert(sortedWord).push_back(words[i]);
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
void groupAnagrams(string words[])
{
int i=0;
map <string, vector<string> > hashMap;
for(i=0;i<10;i++) {
cout<<words[i]<<endl;
string sortedWord = words[i];
sort(sortedWord.begin(),sortedWord.end());
cout<<"Sorted: "<<sortedWord<<endl;
hashMap[sortedWord].push_back(words[i]);
}
return;
}
int main()
{
string words[10] = {"weed","act","cat","tac","tea","eat","ate","bat","mat","tab"};
groupAnagrams(words);
return 1;
}
The errors I am getting are:
groupAnagrams.cpp:23:22: error: implicit instantiation of undefined template 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >'
hashMap[sortedWord].push_back(words[i]);
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iosfwd:200:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
^
In file included from groupAnagrams.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/iostream:38:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/ios:216:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/__locale:15:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/string:477:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/string_view:176:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/__string:56:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:642:
/Library/Developer/CommandLineTools/usr/include/c++/v1/utility:321:9: error: implicit instantiation of undefined template 'std::__1::vector<std::__1::basic_string<char>,
std::__1::allocator<std::__1::basic_string<char> > >'
_T2 second;
^
#include <vector>std::unordered_mapinstead ofstd::map.