从 stdin 获取数字列表并对它们进行标记

发布于 2024-07-27 18:44:37 字数 798 浏览 1 评论 0原文

我如何从用户那里获取数字列表,然后对它们进行标记。

这就是我所拥有的,但除了第一个数字之外,它没有得到任何东西:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

int main()
{

    string line = "";
    cin >> line;

    stringstream lineStream(line);

    int i;
    vector<int> values;

    while (lineStream >> i)
        values.push_back(i);

    for(int i=0; i<values.size(); i++)
        cout << values[i] << endl;

    system("PAUSE");
    return 0;
}

相关帖子:
C++,从字符串到 stringstream 再到向量
Int Tokenizer

How would I get a list of numbers from the user and then tokenize them.

This is what I have but it doesn't get anything except for the first number:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

int main()
{

    string line = "";
    cin >> line;

    stringstream lineStream(line);

    int i;
    vector<int> values;

    while (lineStream >> i)
        values.push_back(i);

    for(int i=0; i<values.size(); i++)
        cout << values[i] << endl;

    system("PAUSE");
    return 0;
}

Related Posts:
C++, Going from string to stringstream to vector
Int Tokenizer

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

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

发布评论

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

评论(6

恋竹姑娘 2024-08-03 18:44:37

这可能是将 cin 中的值读取到容器中的最简单方法:

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

int main()
{
    std::vector<int> values;
    std::copy(
        std::istream_iterator<int>(std::cin), 
        std::istream_iterator<int>(),
        std::back_inserter(values));

    // For symmetry with the question copy back to std::cout
    std::copy(
        values.begin(),
        values.end(),
        std::ostream_iterator<int>(std::cout,"\n"));

}

Here is probably the easiest way to read values from cin into a container:

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

int main()
{
    std::vector<int> values;
    std::copy(
        std::istream_iterator<int>(std::cin), 
        std::istream_iterator<int>(),
        std::back_inserter(values));

    // For symmetry with the question copy back to std::cout
    std::copy(
        values.begin(),
        values.end(),
        std::ostream_iterator<int>(std::cout,"\n"));

}
千寻… 2024-08-03 18:44:37

我相信cin>> 空格中断,这意味着您只能获取输入的第一个数字。

尝试:

getline(cin, line);

I believe cin >> breaks on whitespace, which means you're only getting the first number entered.

try:

getline(cin, line);
岁月打碎记忆 2024-08-03 18:44:37

就像 Donnie 提到的 cin 在空格上中断一样,所以要克服这个问题,我们可以使用“getline()”,下面的示例效果很好:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

int main()
{

    string line = "";
    ::getline(std::cin,line,'\n');

    std::stringstream lineStream(line);

    int i;
    std::vector<int> values;

    while (lineStream >> i)
        values.push_back(i);

    for(int i=0; i<values.size(); i++)
        cout << values[i] << endl;

    system("PAUSE");
    return 0;
}

Like Donnie mentioned cin breaks on whitespace, so do overcome this we can use a 'getline()', the following example works nicely:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

int main()
{

    string line = "";
    ::getline(std::cin,line,'\n');

    std::stringstream lineStream(line);

    int i;
    std::vector<int> values;

    while (lineStream >> i)
        values.push_back(i);

    for(int i=0; i<values.size(); i++)
        cout << values[i] << endl;

    system("PAUSE");
    return 0;
}
浸婚纱 2024-08-03 18:44:37

在主要的顶部

string line = "";
getline (cin, line );
stringstream lineStream(line);

on top of main

string line = "";
getline (cin, line );
stringstream lineStream(line);
冰火雁神 2024-08-03 18:44:37

是的,and 是 getline 的字符串版本,而不是 istream 版本。

Yep, and is the string version of getline, no the istream one.

私藏温柔 2024-08-03 18:44:37

好的:帕维尔·米纳耶夫有最好的答案。
但所有提到 cin 的人都会在空白处中断。
这是一件好事(因为它也忽略了空格);

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

int main()
{

    int i;
    vector<int> values;

    // prefer to use std::copy() but this works.
    while (std::cin >> i)
    {
        values.push_back(i);
    }

    // prefer to use std::copy but this works.
    for(vector<int>::const_iterator loop = values.begin();loop != values.end();++loop)
    {
        cout << *loop << endl;
    }

    return 0;
}

OK: Pavel Minaev has the best answer.
But all the people mentioning that cin breaks on white space.
That is a good thing (because it also ignores white space);

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

int main()
{

    int i;
    vector<int> values;

    // prefer to use std::copy() but this works.
    while (std::cin >> i)
    {
        values.push_back(i);
    }

    // prefer to use std::copy but this works.
    for(vector<int>::const_iterator loop = values.begin();loop != values.end();++loop)
    {
        cout << *loop << endl;
    }

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文