想要使用 sstream 解析 int 的字符串输入

发布于 2024-12-21 02:55:43 字数 728 浏览 3 评论 0原文

我是 C++ 编程新手。我已经阅读了如何使用向量在SO问题中完成解析(Int tokenizer)。但是我尝试了以下方法对于数组。我只能从字符串中解析一个数字。如果输入字符串是“11 22 33 等”。

#include<iostream>
#include<iterator>
#include<vector>
#include<sstream>

using namespace std;

int main()
{

int i=0;
string s;
cout<<"enter the string of numbers \n";
cin>>s;
stringstream ss(s);
int j;
int a[10];
while(ss>>j)
{

    a[i]=j;
    i++;
}
for(int k=0;k<10;k++)
{
    cout<<"\t"<<a[k]<<endl;
}

}

如果我输入“11 22 33”

output

11
and some garbage values.

如果我已经初始化了 stringstream ss(“11 22 33”); 则它工作正常。我做错了什么?

I am new to c++ programming. I have read how parsing can be done in SO questions using vector(Int tokenizer).But I have tried the following for array. I am able to parse only one number from string. If input string is "11 22 33 etc".

#include<iostream>
#include<iterator>
#include<vector>
#include<sstream>

using namespace std;

int main()
{

int i=0;
string s;
cout<<"enter the string of numbers \n";
cin>>s;
stringstream ss(s);
int j;
int a[10];
while(ss>>j)
{

    a[i]=j;
    i++;
}
for(int k=0;k<10;k++)
{
    cout<<"\t"<<a[k]<<endl;
}

}

If I give input as "11 22 33"

output

11
and some garbage values.

If i have initialized stringstream ss("11 22 33"); then its working fine. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-12-28 02:55:43

问题是:

cin>>s;

将一个空格分隔的单词读入 s 中。所以只有 11 进入 s。

您想要的是:

std::getline(std::cin, s);

或者您可以直接从 std::cin 读取数字

while(std::cin >> j) // Read a number from the standard input.

The problem is that:

cin>>s;

Reads one space separated word into s. So only 11 goes into s.

What you want is:

std::getline(std::cin, s);

Alternatively you can read numbers directly from std::cin

while(std::cin >> j) // Read a number from the standard input.
鸩远一方 2024-12-28 02:55:43

看起来 cin>>s 停在第一个空白处。试试这个:

cout << "enter the string of numbers" << endl;
int j = -1;
vector<int> a;
while (cin>>j) a.push_back(j);

It seems cin>>s stop at the first whitespace. Try this:

cout << "enter the string of numbers" << endl;
int j = -1;
vector<int> a;
while (cin>>j) a.push_back(j);
开始看清了 2024-12-28 02:55:43

我们可以使用 cin 通过提取运算符 (>>) 获取字符串,就像处理基本数据类型变量一样

cin >> mystring;

但是,正如前面所说,cin 提取一旦发现任何空格字符就会停止读取,因此在这种情况下,每次提取只能获取一个单词。

来自 http://www.cplusplus.com/doc/tutorial/basic_io/

所以你必须使用获取线()

string s;
cout<<"enter the string of numbers \n";
getline(cin, s);

We can use cin to get strings with the extraction operator (>>) as we do with fundamental data type variables

cin >> mystring;

However, as it has been said, cin extraction stops reading as soon as if finds any blank space character, so in this case we will be able to get just one word for each extraction.

From http://www.cplusplus.com/doc/tutorial/basic_io/

So you have to use getline()

string s;
cout<<"enter the string of numbers \n";
getline(cin, s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文