在地图C++中使用数组作为键

use*_*047 0 c++ arrays map

基本上,我需要找到所有匹配的字谜到一个单词.我正在做的是使用大小为26的数组来表示单词中的字母.例如:abcdefg = {1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0} aaaaaaa = {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0}

这就是我创建数组的方式.

//stringtemp is a C++ string representing the word.
//letters is a size 26 int array representing all the letters in the string.
for(int i=0;i<stringtemp.length();i++)
{
    letters[stringtemp[i]-65]+=1;
}
Run Code Online (Sandbox Code Playgroud)

这就是我将数组存储在地图中的方式.

dictionary[letters].push_back(stringtemp);
Run Code Online (Sandbox Code Playgroud)

所以,我做错了什么,或者在C++中这是不可能的.在我发现的所有其他答案中,他们建议使用矢量作为关键,但这在我的情况下不起作用(我想.)

Ker*_* SB 8

所有的std::array<T, 26>,std::string并std::vector<T>是完全有效的密钥类型的std::map,因为它们都定义小于比较运营商.注意std::array<T, 26>类似于std::tuple<T, T, ..., T>,并且按字典顺序定义比较,非常类似于字符串比较.

#include <array>
#include <map>

typedef std::array<unsigned int, 26> alphabet;

std::map<alphabet, std::string> dictionary;

dictionary[{{1, 0, ..., 8}}] = "hello";
Run Code Online (Sandbox Code Playgroud)

通过更多的工作,你也可以为所有这些类型键设置std::unordered_map,尽管你必须添加Boost(使用hash_combine)的一些样板代码.