2017-05-01 8 views
-2

私は現在、エージェントが報酬を得るのを助けるためにQ-learningアルゴリズムを使用してC++プログラムを実装する方法について研究しています。C++の問題ハッシュテーブルに配列を格納

私は状態とアクションを保存するためにハッシュテーブルを使用しようとしています。 私はC++プログラミングに慣れていません...

私がしようとしているのは、配列を格納するためにハッシュテーブルを使用するようなものです。 しかし、私はそれを格納する正しい方法を見つけることができません...ハッシュテーブルは、配列のエラーの種類だと述べた。

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の\ 737

\ xmemory0を含んC++のHashTableは、キーと値として配列を格納することができますか? テーブルに配列を格納する方法がない場合は、これはPython辞書関数のようです....

ありがとう!

+2

。テンプレートパラメータを 'unordered_map'に変更する必要があります。 [this](http://stackoverflow.com/questions/2582529/using-array-as-map-value-cant-see-the-error)答えを参照してください。 –

+0

あなたのハッシュテーブルの定義にある「」が何を意味しているのだろうか? –

+1

一般的な2要素配列よりも、あなたの '状態'を表現する構造体を作る価値があるかもしれません。これにより、キー入力がより簡単になります。 – tadman

答えて

2

uを助けることができるunordered_mapのヒントがいくつかあります。

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

出力:

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 

更新:

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

申し訳ありませんが、クラステンプレート "std :: vector"のエラー引数リストが見つかりません –

+0

unordered_mapは定義されている配列をサポートできません。 –

+0

@ JunwenXie私はこのエラーを修正した上記のコードを更新します –

0

私はあなたが持ちたい、この権利 "unordered_map <文字列、のstd ::配列を>"をない得た場合」 unordered_map < string、int> "。ので、あなたのコードは次のようになります。

#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型を保持している `unordered_map`にintの配列を据え付けるしようとしている

Key: State3 Value: {3, 30} 
Key: State1 Value: {1, 10} 
Key: State2 Value: {2, 20} 
関連する問題