>n; cout,c++,arrays,string,parsing,C++,Arrays,String,Parsing" /> >n; cout,c++,arrays,string,parsing,C++,Arrays,String,Parsing" />

将包含数字的字符串解析为整数数组 字符串是由数字组成的输入,我想把它转换成C++中的整数数组。 #include <string> #include <iostream> #include <sstream> using std::string; using std::stringstream; using std::cout; using std::endl; int main(int argc,char** argv) { string num="-24 2 90 24 50 76"; stringstream stream(num); while(stream){ int n; stream>>n; cout<<n<<endl; } return 0; } #包括 #包括 #包括 使用std::string; 使用std::stringstream; 使用std::cout; 使用std::endl; int main(int argc,字符**argv){ 字符串num=“-24290245076”; 字符串流(num); while(流){ int n; 流>>n; cout

将包含数字的字符串解析为整数数组 字符串是由数字组成的输入,我想把它转换成C++中的整数数组。 #include <string> #include <iostream> #include <sstream> using std::string; using std::stringstream; using std::cout; using std::endl; int main(int argc,char** argv) { string num="-24 2 90 24 50 76"; stringstream stream(num); while(stream){ int n; stream>>n; cout<<n<<endl; } return 0; } #包括 #包括 #包括 使用std::string; 使用std::stringstream; 使用std::cout; 使用std::endl; int main(int argc,字符**argv){ 字符串num=“-24290245076”; 字符串流(num); while(流){ int n; 流>>n; cout,c++,arrays,string,parsing,C++,Arrays,String,Parsing,成功解析时未设置文件结束条件,解析后必须检查流的状态 第二个76基本上纯粹是偶然的。解析失败会使目标操作数保持不变,因为您没有初始化n,所以它可以是任何对象 快速修复: stream>>n; if (stream) cout<<n<<endl; 但是,您可以在流上使用迭代器并使用以下各项: // Use std::vector's range constructor std::vector<int> values( (std::

成功解析时未设置文件结束条件,解析后必须检查流的状态

第二个
76
基本上纯粹是偶然的。解析失败会使目标操作数保持不变,因为您没有初始化
n
,所以它可以是任何对象

快速修复:

stream>>n;
if (stream)
    cout<<n<<endl;
但是,您可以在流上使用迭代器并使用以下各项:

// Use std::vector's range constructor
std::vector<int> values(
     (std::istream_iterator<int>(stream)), // begin
     (std::istream_iterator<int>()));      // end
//使用std::vector的范围构造函数
std::向量值(
(std::istream_迭代器(流)),//开始
(std::istream_iterator());//结束

我不知道您是否找到了更新问题的答案。如果您没有找到,您可以通过代码轻松找到答案

for (string::iterator it = num.begin(); it != num.end(); ++it) {
    if (*it == ',') {
        *it = ' ';
    }
    else continue;
}

这段代码删除所有冒号并用空格替换。然后您可以正常执行

使用向量处理字符分隔整数列表的另一种方法,可能更简单一些,如下所示:

string str = "50,2,25,38,9,16";
vector<int> ints;
stringstream ss(str);
int n;
char ch;

while(ss >> n) {
    if(ss >> ch)
        ints.push_back(n);
    else
        ints.push_back(n);
}
string str=“50,2,25,38,9,16”;
矢量整数;
stringstream ss(str);
int n;
char ch;
while(ss>>n){
如果(ss>>ch)
内部推回(n);
其他的
内部推回(n);
}

这样,您可以先跳过任何字符分隔符(如果它们存在),然后默认返回到获取整数并将它们添加到列表中(如果它们不存在)(也称为列表的末尾)

不,存储在向量中是一种很好的方法,可以将它们放入数组中。实际上,值不是未定义的;对于数字输入转换,标准保证在出现错误时不会修改目标。因此,他保证看到最后读取的值。重要的一点是他在没有检查输入是否成功的情况下使用了该值。(这在代码中是隐式的,但值得明确说明。)最后,如果您想要int的向量,标准方法是:
std::vector values((std::istream_iterator(stream)),(std::istream_iterator())
。没有循环,没有
推回
;所有事情都发生在构造函数中。@JamesKanze:但它没有定义,因为
n
没有初始化;我对此进行了详细阐述。第二点不错,第三点不错,很高兴你回顾了我的答案。啊,是的。因为他每次都定义了一个新的
n
。我一直在想一些事情e行
int n;while(!stream.eof())
(我们经常看到)。
for (string::iterator it = num.begin(); it != num.end(); ++it) {
    if (*it == ',') {
        *it = ' ';
    }
    else continue;
}
string str = "50,2,25,38,9,16";
vector<int> ints;
stringstream ss(str);
int n;
char ch;

while(ss >> n) {
    if(ss >> ch)
        ints.push_back(n);
    else
        ints.push_back(n);
}