我还没有看到利用字符串流从字符数组读取整数列表的任何适当应用。
元素应该作为一行上以空格分隔的字符串/(char数组)输入,而sstream类则用来执行必需的转换。
这应该是在不使用向量或任何其他额外的STL容器(只有std::string和char数组)的情况下完成的,生成的整数数组的长度应该存储在变量中。
什么是最有效的方式预先形成这样的一个操作?
发布于 2020-04-09 13:42:53
假设我理解你的意思,那么
#include <iostream>
#include <vector>
#include <sstream>
int main() {
std::string apples;
std::getline( std::cin, apples );
std::istringstream iss( apples );
std::vector<int> vec;
int val;
while ( iss >> val ) {
vec.push_back( val );
}
for ( int i : vec ) {
std::cout << i << ',';
}
}发布于 2020-04-09 15:38:10
这更符合我当时的想法。
int* theheap = new int[10];
std::string num;
int val = 0;
int size = 0;
std::cout << "Enter the elements of heap" << std::endl;
std::getline( std::cin, num );
std::istringstream iss(num);
while ( iss >> val ) {
if (val != ' '){
theheap[size] = val;
++size;
}
}https://stackoverflow.com/questions/61121719
复制相似问题