将数字的字符串数组转换为整数数组时,元素将变为 0
When converting a string array of numbers to an array of integers the elements are turned to 0s
当我将信息转换为整数并打印出数组时,输出仅为 0s。info 中的每个元素都是作为字符串输入的数字,该字符串取自文本文件。例如:513497628 19 4 16 4 7 14 18 15 10 6 6 1 7 17 88 10 79。我使用 strtok 从行中删除空格并将数字输入信息。当我打印出信息时,所有数字都在那里。如何让这些正确转换?
string info[1000]
int student[18];
for (int i=1; i<18; i++){
//cout << info[i] << endl;
stringstream convert(info[i]);
convert << student[n];
cout << student[n] << endl;
n++;
}
字符串流是我最喜欢的工具。它们会自动转换数据类型(大多数时候(,就像 cin 和 cout 一样。下面是一个字符串流的示例。它们包含在库中
string info = "513497628 19 4 16 4 7 14 18 15 10 6 6 1 7 17 88 10 79";
int student[18];
stringstream ss(info);
for (int i=0; i< 18; i++){
ss >> student[i];
cout << student[i] << endl;;
}
这是一个回复 https://repl.it/J5nQ
相关文章:
- 数组元素打印的递归方法
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 为什么使用数组元素查找最大数字的程序不起作用?
- 数组元素更改值?
- 如何访问宏中定义的数组元素
- 将数组元素递增 1
- C++函数,它将数组、谓词和运算符作为参数,并将运算符应用于满足谓词的数组元素
- 存储指向动态数组元素的指针
- C++通过别名指针以静默方式将错误的类型分配给数组元素
- 在 std::map 中插入数组元素
- 使用指针访问数组元素时出现意外结果
- c++ 使用动态分配运算符反向数组元素
- 为什么在 c++ 中分配 char 数组元素时,分配的字符会被销毁?
- 缺少数组元素问题
- 如何在类中制作 2D 数组元素,然后在其构造函数中指定其维度?
- 创建一个函数来转换数组元素的类型并返回数组的地址
- 删除在结构 c++ 中声明的数组元素
- Getter 和 Setter 用于类 C++ 中的数组元素
- 尝试递归获取数组元素的总和
- 使用SWIG将numpy数组元素(int)传递给c++int