正在尝试打印未定义的地图
Trying to print an unordered_map
我正在编写一个简单的程序来查找字谜。我使用的是一个哈希表,其中已排序的字符串作为键,未排序的字符串为值。当我尝试打印无序映射(hashmap)时,它会给我这个错误。
错误1错误C2675:一元"++":"std::string"未定义此运算符或转换为预定义的可接受类型操作员c:\program files(x86)\microsoft visual studio12.0\vc\include\xhash 672 1
#include <iostream>
#include <list>
#include <cstdlib>
#include <map>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cstring>
void Anagrams(std::vector<std::string> &v){
std::unordered_map<std::string, std::string> wordTable;
char sortedString[256];
for (std::string tmp : v){
strcpy(sortedString, tmp.c_str());
std::sort(sortedString, sortedString + tmp.size());
std::string backToString(sortedString);
wordTable.insert(backToString, tmp);
}
std::cout << "Map contains" << std::endl;
std::cout << "mymap's buckets contain:n";
for (unsigned i = 0; i < wordTable.bucket_count(); ++i) {
std::cout << "bucket #" << i << " contains:";
for (auto local_it = wordTable.begin(i); local_it != wordTable.end(i); ++local_it)
std::cout << " " << local_it->first << ":" << local_it->second;
std::cout << std::endl;
}
}
int main()
{
std::vector<std::string> words{ "debitcard", "badcredit", "cat", "act", "evils", "elvis" };
Anagrams(words);
return 0;
}
出于某种原因,它认为迭代器"local_it"是一个字符串。有人能帮忙吗?
问题是std::unorderd_map::insert()函数采用std::pair<key, value>
,而不是key, value
:
wordTable.insert(std::make_pair(backToString, tmp));
相关文章:
- 编译C++时未定义的引用
- vscode g++链路故障:体系结构x86_64的未定义符号
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 不知道某个东西是否被忽略会引入未定义的行为吗
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- c++11评估顺序(未定义的行为)
- 使用mysql c++连接器的未定义引用
- 从python调用openMP共享库时,未定义opnMP函数
- 在 Mac 上使用 CMAKE 将 FFTW 和 FFTWPP 链接到项目中时未定义的符号
- Cmake 链接问题:未定义对 Button::mousePressEvent(QGraphicsSceneMouseE
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 具有外部"c"和程序集的未定义函数
- 此增量后语句是否会导致未定义的行为?
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 正在尝试打印未定义的地图