试图打印unordered_map

时间:2015-12-13 01:43:59

标签: c++ iterator hashmap anagram

我正在编写一个简单的程序来查找字谜。我使用散列表作为键,排序字符串作为键,未分类的字符串作为值。当我尝试打印unordered_map(哈希映射)时,它给了我这个错误。

  

错误1错误C2675:一元'++':'std :: string'没有定义它   运算符或转换为预定义可接受的类型   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”是一个字符串。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

问题是std::unorderd_map::insert()函数需要std::pair<key, value>,而不是key, value

 wordTable.insert(std::make_pair(backToString, tmp));