正在尝试打印未定义的地图

Trying to print an unordered_map

本文关键字:未定义 地图 打印      更新时间:2023-10-16

我正在编写一个简单的程序来查找字谜。我使用的是一个哈希表,其中已排序的字符串作为键,未排序的字符串为值。当我尝试打印无序映射(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));