我想定义类似的东西
Map<int, char[5] > myMap;
上面的声明被 c++ 编译器接受并且没有抛出错误但是当我做这样的事情时
int main()
{
char arr[5] ="sdf";
map <int, char[5]> myMap;
myMap.insert(pair<int, char[5]>(0,arr));
return 0;
}
我得到错误:
In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.6/bits/char_traits.h:41,
from /usr/include/c++/4.6/ios:41,
from /usr/include/c++/4.6/ostream:40,
from /usr/include/c++/4.6/iostream:40,
from charMap.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = char [5]]’:
charMap.cpp:9:42: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:104:31: error: array used as initializer
/usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int, _U2 = char [5], _T1 = const int, _T2 = char [5]]’:
charMap.cpp:9:43: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:109:39: error: array used as initializer
定义一个固定大小的字符数组对我来说很重要,因为它优化了我的网络流操作。有没有办法实现它?我不想使用char *
or std::string
。