C++;哈希表中存储数组的问题 我目前正在研究如何利用Q-学习算法来实现一个C++程序,以帮助Agent获得奖励。

C++;哈希表中存储数组的问题 我目前正在研究如何利用Q-学习算法来实现一个C++程序,以帮助Agent获得奖励。,c++,arrays,dictionary,hashtable,q-learning,C++,Arrays,Dictionary,Hashtable,Q Learning,我正在尝试使用哈希表来存储我的状态和操作。 我不熟悉C++编程……/P> 我试图做的是使用哈希表存储数组。 但是我找不到正确的方法来储存它。。。哈希表说它是数组的一种错误类型 using namespace std; int state[2] = {0,0}; unordered_map<string, int> hashtable; hashtable.emplace("State1", state); cout << "the State of State1 is :

我正在尝试使用哈希表来存储我的状态和操作。 我不熟悉C++编程……/P> 我试图做的是使用哈希表存储数组。 但是我找不到正确的方法来储存它。。。哈希表说它是数组的一种错误类型

using namespace std;
int state[2] = {0,0};
unordered_map<string, int> hashtable;
hashtable.emplace("State1", state);
cout << "the State of State1 is :" << hashtable["State1"] << endl; cin.get();
使用名称空间std;
int状态[2]={0,0};
无序映射哈希表;
hashtable.emplace(“State1”,state);

有一些无序地图的提示可以帮助你

#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;

int main() {
    // constructor
    unordered_map<string, int> hashTable = {
            {"a", 123},
            {"b", 456},
            {"c", 789}
    };

    // update
    hashTable["a"] = 1;
    // add new entry
    hashTable["d"] = 2;
    hashTable.insert({"e", 111});
    // Iterate and print keys and values of unordered_map
    for (const auto& n : hashTable )
        cout << "Key: " << n.first << "\tValue: " << n.second << endl;
    // Output values by key
    cout << "The value of d is :" << hashTable["d"] << endl;

    // init vector
    vector<int> states{1,2,3,4,5,6,7};
    states.push_back(8);

    // add vector into unordered_map
    unordered_map<string, vector<int>> ht;
    ht["state1"] = states;
    ht.insert({"c", vector<int>{1,1,1,1}});

    cout << "Values which key is 'c' in ht" << endl;
    for(auto& v : ht["c"])
        cout << v << "\t";
    cout << endl;

    /*
     * put array into unordered_map
     */
    int state1[3] = {0, 1, 2};
    int state2[2] = {3, 4};
    int state3[4] = {5, 6, 7, 8};

    // declare map to store int pointer value
    unordered_map<string, int*> u_map;

    // add into unordered_map
    u_map["a"] = state1;
    u_map["b"] = state2;
    u_map.insert({"c", state3});

    // update
    u_map["b"] = state3;

    // get pointer of array
    auto s1 = u_map["a"];
    int* s2 = u_map["b"];

    // accesses val in array
    cout << "Value of key a is: "<< s2[0] << endl;

    size_t size = sizeof(s1)/sizeof(s1[0]);
    for (int i = 0; i <= size ; ++i) {
        cout << "val " << i << " is: "<< s1[i] << endl;
    }
    return 0;
}
更新:

fix use of class template 'vector' requires template arguments
update for adding array into map

如果我没弄错的话,你想要的是“无序映射”而不是“无序映射”。因此,您的代码应该如下所示:

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;

int main() {
    unordered_map<string, std::array<int,2>> hashTable = {
        {"State1", {1,10}},
        {"State2", {2,20}},
        {"State3", {3,30}},
    };
    for (const auto& n : hashTable )
        cout << "Key: " << n.first << "\tValue: {" << n.second[0] << ", "     << n.second[1] << "}" << endl;
    return 0;
}

您正试图将一个int数组放入一个保存int的
无序的\u映射
。您需要将模板参数更改为
unordered\u map
。请参阅答案。是否想过哈希表定义中的
是什么意思?与一般的两元素数组相比,创建一个结构来更好地描述
状态可能是值得的。很抱歉,我发现类模板“std::vector”的错误参数列表丢失了Nordered\u map无法支持和定义的数组?@JunwenXie我更新了上面的代码,修复了此问题error@JunwenXie我还添加了你喜欢的代码。谢谢你的帮助。还有一个问题。。。有没有可能出现无序的地图
fix use of class template 'vector' requires template arguments
update for adding array into map
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;

int main() {
    unordered_map<string, std::array<int,2>> hashTable = {
        {"State1", {1,10}},
        {"State2", {2,20}},
        {"State3", {3,30}},
    };
    for (const auto& n : hashTable )
        cout << "Key: " << n.first << "\tValue: {" << n.second[0] << ", "     << n.second[1] << "}" << endl;
    return 0;
}
Key: State3 Value: {3, 30}
Key: State1 Value: {1, 10}
Key: State2 Value: {2, 20}