字符数组作为C ++映射中的值 [英] Character Array as a value in C++ map
本文介绍了字符数组作为C ++映射中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要定义
Map< int,char [5]> myMap;
上述声明被c ++编译器接受,并且不会抛出错误, / p>
int main()
{
char arr [5] =sdf;
map< int,char [5]> myMap;
myMap.insert(pair< int,char [5]>(0,arr));
return 0;
}
我得到错误:
在/usr/include/c++/4.6/bits/stl_algobase.h:65:0中包含的文件中,
来自/usr/include/c++/4.6/位/ char_traits.h:41,
从/usr/include/c++/4.6/ios:41,
从/usr/include/c++/4.6/ostream:40,
从/ usr / include / c ++ / 4.6 / iostream:40,
来自charMap.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h:在构造函数'std :: pair< _T1,_T2> :: pair(const _T1& const _T2&)[with _T1 = int,_T2 = char [5]]':
charMap.cpp:9:42:从这里实例化
/usr/include/c++/4.6/bits/stl_pair.h:104:31:error:array用作初始化器
/usr/include/c++/4.6/bits/stl_pair.h:在构造函数'std :: [...] [with _U1 = int,_U2 = char [5],_T1 = const int,_T2 = char [5]]':'_T1,_T2> :: pair(const std :: pair <_U1,_U2&
charMap.cpp:9:43:从这里实例化
/usr/include/c++/4.6/bits/stl_pair.h:109:39:error:array用作初始化器
重要的是我定义一个固定大小的字符数组,因为它优化了我的网络流操作。有没有办法实现呢?我不想使用 char *
或 std :: string
。
<$ c $ c> struct FiveChar
{
FiveChar(char in [5]){memcpy(data,in,5); }
char& operator [](unsigned int idx){return data [idx]; }
char data [5];
};
int main(void)
{
char arr [5] =sdf;
map< int,FiveChar> myMap;
myMap.insert(pair< int,FiveChar>(0,arr));
return 0;
}
I want to define something like
Map<int, char[5] > myMap;
The above declaration is accepted by c++ compiler and no error is thrown but when I do something like this
int main()
{
char arr[5] ="sdf";
map <int, char[5]> myMap;
myMap.insert(pair<int, char[5]>(0,arr));
return 0;
}
I get error as:
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
It is important for me to define a fixed size character array because it optimizes my network stream operation. Is there any way to achieve it? I do not want to use char *
or std::string
.
解决方案
One way is to wrap the fixed size character array as a struct.
struct FiveChar
{
FiveChar(char in[5]) { memcpy(data, in, 5); }
char& operator[](unsigned int idx) { return data[idx]; }
char data[5];
};
int main(void)
{
char arr[5] = "sdf";
map<int, FiveChar> myMap;
myMap.insert(pair<int, FiveChar>(0, arr));
return 0;
}
这篇关于字符数组作为C ++映射中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文