尝试打印 unordered_map
Trying to print an unordered_map
我正在编写一个简单的程序来查找字谜。我正在使用散列 table,其中已排序的字符串作为键,未排序的字符串作为值。当我尝试打印 unordered_map(散列映射)时,它给了我这个错误。
Error 1 error C2675: unary '++' : 'std::string' does not define this
operator or a conversion to a type acceptable to the predefined
operator c:\program files (x86)\microsoft visual studio
12.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));
我正在编写一个简单的程序来查找字谜。我正在使用散列 table,其中已排序的字符串作为键,未排序的字符串作为值。当我尝试打印 unordered_map(散列映射)时,它给了我这个错误。
Error 1 error C2675: unary '++' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 12.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));