C++ 无符号字符的typedef数组与std::vector之间没有已知的转换
我试图将一个类型定义的无符号字符数组推送到一个向量中,但它不允许我一直说,C++ 无符号字符的typedef数组与std::vector之间没有已知的转换,c++,arrays,vector,C++,Arrays,Vector,我试图将一个类型定义的无符号字符数组推送到一个向量中,但它不允许我一直说,没有从无符号字符*到常量无符号字符的已知转换&[13] 实际的错误消息:error:std::vector::push_back(unsigned char*&)| 类型定义代码: const int BYTE_STRING_LEN = 13; typedef unsigned char byte; typedef byte bytestring[BYTE_STRING_LEN]; 添加到矢量代码: void print
没有从无符号字符*到常量无符号字符的已知转换&[13]
实际的错误消息:error:std::vector::push_back(unsigned char*&)|
类型定义代码:
const int BYTE_STRING_LEN = 13;
typedef unsigned char byte;
typedef byte bytestring[BYTE_STRING_LEN];
添加到矢量代码:
void printNewSolution(bytestring b)
{
// check against known solutions to see if it's unique
static std::vector<bytestring> KnownSolutions;
KnownSolutions.push_back(b);
}
void printNewSolution(通过测试环b)
{
//对照已知的解决方案检查它是否唯一
静态std::向量知识解决方案;
了解解决方案。推回(b);
}
编辑1:
我需要使用原始字符数组,因为我对它们进行逐位运算,每个值必须是1字节。这是怎么回事
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
const int BYTE_STRING_LEN = 13;
typedef unsigned char byte;
struct bytestring
{
bytestring(const byte* val)
{
memcpy(value, val, sizeof(value));
}
bool operator==(const bytestring& other) const
{
return memcmp(value, other.value, sizeof(value)) == 0;
}
byte value[BYTE_STRING_LEN];
};
void printNewSolution(bytestring b)
{
// check against known solutions to see if it's unique
static std::vector<bytestring> KnownSolutions;
if (std::find(KnownSolutions.begin(), KnownSolutions.end(), b) == KnownSolutions.end())
{
std::cout << "new" << std::endl;
KnownSolutions.push_back(b);
}
else
std::cout << "old" << std::endl;
}
因此,如果要确保使用的是无符号字符,可以使用
typedef std::basic_string<byte> bytestring;
typedef std::basic_string bytestring;
在这一点上,其余的仍然有效:
数组不可分配或复制,因此它们不能存储在
std::vector
中。这是一个完美的例子,说明了为什么std::array
或std::string
比原始字符数组更容易使用。我只是将stl数组用作typedef。它更干净,并且允许我使用与我第一次尝试使用c样式数组相同的行数进行操作。
typedef std::basic_string<byte> bytestring;