C++ 无法填充 C 数组的映射

标签 c++ arrays visual-c++ typedef

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;

上面的代码没有编译,有问题的行是: tryStuff["你好"][0] = s;

这是编译器喊出的内容:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(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.0\vc\include\map(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:\work\toot\tootcode\tootim\tools\doobim\doobim.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 ==========

知道如何让它工作吗? (我不想改变数据结构,这是一个 map : string --> 的 map 数组 )

最佳答案

你不能在容器中存储 C 风格的数组,因为它们是不可赋值的;你不能这样做:

int x[3] = { 0, 1, 2 };
int y[3] = { 3, 4, 5 };

x = y;

但是容器需要能够分配/复制它们存储的元素。

考虑使用 std::vectorboost::array* 而不是原始 C 数组。


* 这可以在 C++ 标准的最新修订版中作为 std::array 找到。

关于C++ 无法填充 C 数组的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8658292/

相关文章:

python - 在我的 Mac 上安装 pycrypto 时出错

arrays - 状态自己改变?

python - 找不到有效的 MSVC 版本

c - 如何从另一个c文件中的void函数返回元素数组

c++ - Visual Studio C++ 2010 如何将图标添加到控制台应用程序

visual-studio-2010 - Visual C++ "Debug Assertion Failed"

c++ - 方程解析库 C++

c++ - C++ 中的 __builtin__functions 有什么用?

c++ - boost C++ 和 Windows CE 6.0

c - 在 C 程序中使用 GMP 整数函数的正确方法是什么?