简体   繁体   English

无符号字符数组,用于键入stl :: map或其他容器

[英]unsigned char array to key in the stl::map or other container

I try to make a map like 我尝试制作一张像

 map<unsigned char[16], string> testMap;

And unsigned char[16] looks not suitable type for key map. 而且unsigned char[16]看起来不适合用于键映射。

but in my key have NULL, so I can't use string for key.. 但是我的键中有NULL,所以我不能使用字符串作为键。

example key: 示例密钥:

 01 bc 03 00 50 03 00 00 89 00 10 00 ff ff ff ff

this kind of key also can use map.find() ? 这种键也可以使用map.find()吗?

Or Is there any other good container for me? 或者还有其他适合我的好容器吗?

THANKS :) 谢谢 :)

Use the std::string constructor that builds the string from a char array (not a C-String). 使用std::string构造函数从char数组(而非C-String)构建字符串。 This takes two parameters - a pointer to the array and a length: 这需要两个参数-指向数组的指针和一个长度:

std::string x("pq\\0rs"); // Two characters because input assumed to be C-String std::string x("pq\\0rs",5); // Two characters because input assumed to be C-String std::string x("pq\\0rs",5); // 5 Characters as the input is now a char array with 5 characters.

This allows for embedded null characters. 这允许嵌入空字符。

Use STL vector instead of unsigned char array 使用STL向量而不是无符号字符数组

NOTE: 注意:

If you are using c++11 and have fixed length of characters,use std::array. 如果您使用的是c ++ 11并且具有固定的字符长度,请使用std :: array。 If you don't have fixed length or are not using C++11 or above go for std::vector 如果您没有固定长度或没有使用C ++ 11或更高版本,请使用std :: vector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM