无法填充C数组的映射
C++ Unable to populate a map of C arrays
typedef std::map<std::string,int> string2intMap;
typedef string2intMap arrOfMaps[3] ;
//map : string --> array of maps of <std::string,int>
std::map<std::string,arrOfMaps> tryingStuff;
//map : string --> int
string2intMap s;
s["key"]= 100;
tryingStuff["hello"][0] = s;
上面的代码不能编译,问题行是:tryingStuff["hello"][0] = s;
下面是编译器的提示:
c:program files (x86)microsoft visual studio 10.0vcincludemap(215): error C2440: '<function-style-cast>' : cannot convert from 'int' to 'std::map<_Kty,_Ty> [3]'
2> with
2> [
2> _Kty=std::string,
2> _Ty=int
2> ]
2> There are no conversions to array types, although there are conversions to references or pointers to arrays
2> c:program files (x86)microsoft visual studio 10.0vcincludemap(210) : while compiling class template member function 'std::map<_Kty,_Ty> (&std::map<_Kty,arrOfMaps>::operator [](const std::basic_string<_Elem,_Traits,_Ax> &))[3]'
2> with
2> [
2> _Kty=std::string,
2> _Ty=int,
2> _Elem=char,
2> _Traits=std::char_traits<char>,
2> _Ax=std::allocator<char>
2> ]
2> c:worktoottootcodetootimtoolsdoobimdoobim.cpp(95) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
2> with
2> [
2> _Kty=std::string,
2> _Ty=arrOfMaps
2> ]
2>
2>Build FAILED.
2>
2>Time Elapsed 00:00:01.38
========== Build: 1 succeeded, 1 failed, 5 up-to-date, 0 skipped ==========
你知道怎么让它工作吗??我不想改变数据结构,它是
不能将c风格的数组存储在容器中,因为它们不能赋值;你不能这样做:
int x[3] = { 0, 1, 2 };
int y[3] = { 3, 4, 5 };
x = y;
但是容器需要能够赋值/复制它们所存储的元素。
考虑使用std::vector
或boost::array
*来代替原始的C数组。
std::array
。基本上数组是不可复制的。
你想使用的是一个向量…
typedef std::map<std::string,int> string2intMap;
typedef std::vector<string2intMap> arrOfMaps;
//map : string --> array of maps of <std::string,int>
std::map<std::string,arrOfMaps> tryingStuff;
//map : string --> int
string2intMap s;
s["key"]= 100;
tryingStuff["hello"].push_back(s);
相关文章:
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 具有 2 个分量的数组的特征映射到 3 的向量
- 将数组的元素插入映射的时间复杂度是多少?
- 如何初始化字节数组的映射?
- 数组中未映射的内存访问从python传递到c++
- 当 map 是一个整数数组并且由 operator[] 创建时,它是否初始化其映射类型
- 如何为映射和priority_queue编写比较器,其中元素是二维数组
- 如何使用 std::multimap 将整数键映射到两个用作多维数组坐标的整数值(对于井字)?
- 特征:将数组映射到矩阵大小未知的矩阵
- 一维数组映射方式的性能差异问题
- C++ - 数组中的最后一个值在映射中重复
- 如何映射数组元素以选择二进制树节点
- 如何在c++中访问映射数组
- 如何创建以字符作为键值并将字符串数组作为映射值的映射 - 数组必须具有不同的长度
- C++映射数组键自动增加
- 整数宇宙映射数组到固定范围的应用与实现
- map的key可以是数组吗?(映射数组错误)
- 在c++中有效地向映射数组中添加大量类
- C++ 映射数组运算符重载启用设置值
- 你能在c++中有一个映射数组吗?