-3

I am currently doing a research about implement a C++ programe by using Q-learning algorithm to help the agent get the reward.

I am trying to use the Hashtable to store my states and Actions. I am not familiar with the C++ programming...

What i am trying to do is like using hashtable to store the Arrays. but i can not find the right way to store it... the hashtable said it is an error type of the array.

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();
Error C2664   'std::pair<const _Kty,_Ty>::pair(std::pair<const _Kty,_Ty> &&)': cannot convert argument 2 from 'int [2]' to 'const int &'  myproject   c:\program files (x86)\microsoft visual studio

14.0\vc\include\xmemory0 737

Does C++ HashTable can Store an Array as Key and Value? If it Doesn't is there any way to store an Array in a table? which like the Python dictionary function....

Thanks!

Junwen Xie
  • 15
  • 4
  • 2
    You are trying to emplace an array of int into an `unordered_map` which holds ints. You need to change your template parameter to `unordered_map`. See [this](http://stackoverflow.com/questions/2582529/using-array-as-map-value-cant-see-the-error) answer. – Paul Rooney May 01 '17 at 06:25
  • Ever wondered what that `` in the definition of your hashtable means? – The Quantum Physicist May 01 '17 at 06:29
  • 1
    It might be worth making a structure to describe your `state` better than a generic two-element array. This makes keying it a lot easier. – tadman May 01 '17 at 06:30

2 Answers2

2

there are some tips of unordered_map which can help u.

#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;
}

output:

Key: d  Value: 2
Key: b  Value: 456
Key: c  Value: 789
Key: a  Value: 1
The value of d is :2
Value of key a is: 5
val 0 is: 0
val 1 is: 1
val 2 is: 2

update:

fix use of class template 'vector' requires template arguments
update for adding array into map
yun xi
  • 21
  • 3
1

if i got this right you want to have "unordered_map< string, std::array>" not "unordered_map< string, int>". so the your code should look something like this:

#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;
}

The output will be:

Key: State3 Value: {3, 30}
Key: State1 Value: {1, 10}
Key: State2 Value: {2, 20}
Iman Kianrostami
  • 482
  • 3
  • 13