尝试将字符串从文件读取到无符号字符向量中
Trying to read strings from a file into an unsigned char vector
我真的是编码新手,所以请耐心等待。 运行以下代码时,我总是收到致命错误:
Debug Assertion Failed!
Program: [program name]
File: [MS VS path]includevector
Line: 1502
Expression: vector subscript out of range
可能是什么原因造成的?
string temp1;
stringstream temp2;
unsigned char temp3;
vector<vector<unsigned char>>vectorname;
for (unsigned int i = 0; i < 5; i++) {
for (unsigned int j = 0; j < 5; j++) {
Datei >> temp1; // copies file into string
temp2 << temp1; //copies string into streamstring
temp2 >> temp3; //copies streamstring into unsigned char
vectorname[i][j] = temp3 //sets the unigned char as value at the i,j, position.
}
}
要在循环中动态增长 2D 向量,您需要添加新的内部向量,并给定新的内部向量,向其添加项目。
下面是一个示例:
#include <vector>
int main()
{
std::vector<std::vector<unsigned char>> vectorname;
for (unsigned int i = 0; i < 5; i++)
{
// add a new vector to the outer std::vector
vectorname.push_back(vector<unsigned char>());
// now add data to the newly added vector. The `back()` returns
// a reference to the last added vector
for (unsigned int j = 0; j < 5; j++) {
vectorname.back().push_back(j);
}
}
相关文章:
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- 在 std::无符号字符的向量处存储 int 的十六进制表示形式
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 从 std::vector<无符号字符>切片中提取 int?
- 将无符号字符的向量存储在数组中会给我 std::bad_alloc
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 库特无符号字符
- 寻找一种更好的方法来表示无符号字符数组
- C++ 无符号字符 *{varName};-> C#
- 如何返回/复制unique_ptr<无符号字符[]>的值?
- 将浮点数转换为无符号字符数组并打印出来
- 在函数中返回无符号字符数组,但不返回指针
- 如何在 C/C++ 中将无符号字符*转换为无符号字符数组?
- 如何返回实际值(在我的例子中是无符号字符数组)而不是来自 C++ 函数的指针?
- 如何将带有十六进制值的 std::string 转换为 std::vector<无符号字符>
- 无符号字符打印其 ASCII 值
- 字符数组到无符号字符 *
- 是否可以在 c++ 中将两位数保存在无符号字符中