我的代码中有以下内容:
static unsigned char* table;
...
table = something here;
现在我必须分配这个 table
到 std::vector<unsigned char>
类型的变量我做不到。我正在做:
unsigned char* buffer = (unsigned char*) table;
std::vector<unsigned char>::size_type size = strlen((const char*)buffer);
std::vector<unsigned char>static rawTable(buffer, buffer + size);
for(ByteBuffer::iterator it=rawTable.begin();it!=rawTable.end();++it)
std::cout << "Raw Table: "<<*it<< std::endl;
我能够编译代码,但 rawTable 中没有任何值。请帮忙!
我已经验证了变量 table
有值(value)。我很感激这方面的任何帮助。谢谢。
最佳答案
strlen
为您提供字符串的长度,而不是任意内存区域的大小。如果您的 table
中的任何地方都有一个 '\0'
,strlen
会找到它并停止计数。
此外,通过使 rawTable
成为一个 static
变量,它不会在 buffer
或 size
时更新其值永远改变。 static
变量只构造一次。
此外,如果这应该是一个数字数据表,您应该转换为数字非字符类型。否则 cout
可能会将其解释为 ASCII 代码。
关于c++ - 将 Static unsigned char* 分配给 std::vector<unsigned char>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821492/