1

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;
        ^
3
  • 2
    You forgot to #include <vector> Commented Jul 23, 2019 at 3:26
  • Other than the non inclusion of vector, as mentioned above, I did not find any issue running the code on visual studio Commented Jul 23, 2019 at 3:35
  • 3
    If you really wanted a hash map, you want to use std::unordered_map instead of std::map. Commented Jul 23, 2019 at 3:37

1 Answer 1

2

You must include the vector header:

#include <vector>
Sign up to request clarification or add additional context in comments.

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.