C ++问题将数组存储在哈希表中 [英] C++ Problems Store array in hashtable
问题描述
我正在通过使用Q学习算法来帮助代理人获得奖励来开展关于实现C ++程序的研究。
我正在尝试使用Hashtable存储我的州和行动。
我不熟悉C ++编程...
我想要做的是像使用哈希表存储数组。
但我找不到正确的方法存储它...哈希表表示它是数组的错误类型。
using namespace std;
int state [2] = {0,0};
unordered_map< string,int>哈希表;
hashtable.emplace(State1,state);
cout<<< 状态1是:< hashtable [State1]<< ENDL; cin.get();
错误C2664'std: :对< const _Kty,_Ty> :: pair(std :: pair< const _Kty,_Ty&&&&)':不能将参数2从int [2]转换为const int&'myproject c: \程序文件(x86)\microsoft visual studio
14.0 \vc\include\ xmemory0 737
C ++ HashTable可以将数组存储为键和值吗?
如果它没有任何方法来存储数组在表中?它像Python字典功能一样。
谢谢!
p>有一些unordered_map的提示可以帮助你。
#include< iostream>
#include< unordered_map>
#include< string>
#include< vector>
使用命名空间std;
int main(){
//构造函数
unordered_map< string,int> hashTable = {
{a,123},
{b,456},
{c,789}
};
// update
hashTable [a] = 1;
//添加新条目
hashTable [d] = 2;
hashTable.insert({e,111});
//迭代并打印unordered_map
的键和值(const auto& n:hashTable)
cout<<< Key:<< n.first< \tValue:<< n.second < ENDL;
//按键输出值
cout<<< d的值为:< hashTable [d]<< ENDL;
//初始向量
向量< int>状态{1,2,3,4,5,6,7};
states.push_back(8);
//将向量添加到unordered_map
unordered_map< string,vector< int>> HT;
ht [state1] =状态;
ht.insert({c,vector< int> {1,1,1,1}});
cout<<< 关键字在ht中的c的值< ENDL; (auto& v:ht [c])
cout<< v< \t;
cout<<< ENDL;
/ *
*将数组放入unordered_map
* /
int state1 [3] = {0,1,2};
int state2 [2] = {3,4};
int state3 [4] = {5,6,7,8};
//声明地图存储int指针值
unordered_map< string,int *> u_map;
//添加到unordered_map
u_map [a] = state1;
u_map [b] = state2;
u_map.insert({c,state3});
// update
u_map [b] = state3;
//获取数组的指针
auto s1 = u_map [a];
int * s2 = u_map [b];
//访问数组
中的val cout<<< a的值是:<< s2 [0]< ENDL;
size_t size = sizeof(s1)/ sizeof(s1 [0]); (int i = 0; i< = size; ++ i)
{
cout< val< i< 是:< s1 [i]<< ENDL;
}
return 0;
}
输出:
<$密钥:d值:2
密钥:b值:456
密钥:c值:789
密钥:a值:1
d的值是:2
键a的值是:5
val 0是:0
val 1是:1
val 2是:2
更新:
修复使用类模板'vector'需要模板参数
更新将数组添加到地图
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!
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
这篇关于C ++问题将数组存储在哈希表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!