以字符串向量作为值的哈希映射
hashMap with string vector as values
我正在尝试使用带有字符串向量作为值的C++映射,但不确定我是否使用正确的语法将字符串插入向量中。请查看下面的代码:
我还尝试了以下方法: 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;
}
我得到的错误是:
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>
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 使用 Key 对 C++ 中的哈希映射进行排序. 无法排序
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 如何按值对无序哈希映射进行排序
- 哈希映射如何在 c++ 中实现?
- C++中的哈希映射等效
- 以字符串向量作为值的哈希映射
- 来自并发哈希映射的迭代器是否安全
- 如何在这个哈希映射中实现 AVL 树
- 无法迭代结构"string"的哈希映射:[列表] C++
- 什么是最快的解决方案,用于读/写入哈希映射
- 可以安全地依赖unordered_map中的初始化值(哈希映射)
- 使用按插入方式排序的参数创建哈希映射
- 哈希映射没有可行的重载 '='
- 使用分配器对哈希映射进行高效的序列化和反序列化
- 由于动态数组而导致的哈希映射内存泄漏
- 为boost哈希映射定义自定义哈希函数
- 是我们迭代时更改哈希映射的行为,已定义
- 哈希映射和有序遍历
- 哈希映射错误:与调用 '(const __gnu_cxx::.