C++ 逐字输入字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18318980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:53:59  来源:igfitidea点击:

taking input of a string word by word

c++

提问by aandis

I just started learning C++. I was just playing around with it and came across a problem which involved taking input of a string word by word, each word separated by a whitespace. What I mean is, suppose I have

我刚开始学习 C++。我只是在玩弄它并遇到了一个问题,该问题涉及逐字输入字符串,每个单词由空格分隔。我的意思是,假设我有

   name  place animal 

as the input. I want to read the first word, do some operations on it. Then read the second word, do some operations on that, and then read the next word, so on.

作为输入。我想读取第一个单词,对其进行一些操作。然后读第二个单词,做一些操作,然后读下一个单词,依此类推。

I tried storing the entire string at first with getline like this

我首先尝试用这样的 getline 存储整个字符串

    #include<iostream>
    using namespace std;
    int main()
    {
     string t;
     getline(cin,t);
     cout << t; //just to confirm the input is read correctly
    }

But then how do I perform operation on each word and move on to the next word?

但是,我如何对每个单词执行操作并移动到下一个单词?

Also, while googling around about C++ I saw at many places, instead of using "using namespace std" people prefer to write "std::" with everything. Why's that? I think they do the same thing. Then why take the trouble of writing it again and again?

此外,在谷歌搜索 C++ 时,我在很多地方看到,而不是使用“使用命名空间 std”,人们更喜欢用“std::”写所有东西。为什么?我认为他们做同样的事情。那又何必费心写一遍又一遍呢?

回答by jrok

Put the line in a stringstream and extract word by word back:

将该行放入字符串流中并逐字提取:

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    string t;
    getline(cin,t);

    istringstream iss(t);
    string word;
    while(iss >> word) {
        /* do stuff with word */
    }
}

Of course, you can just skip the getline part and read word by word from cindirectly.

当然,您可以跳过 getline 部分,cin直接逐字阅读。

And here you can read why is using namespace stdconsidered bad practice.

在这里你可以阅读为什么被using namespace std认为是不好的做法。

回答by goelakash

(This is for the benefit of others who may refer)

(这是为了其他可能参考的人的利益)

You can simply use cinand a chararray. The cin input is delimited by the first whitespace it encounters.

您可以简单地使用cinchar数组。cin 输入由它遇到的第一个空格分隔。

#include<iostream>
using namespace std;

main()
{
    char word[50];
    cin>>word;
    while(word){
        //Do stuff with word[]
        cin>>word;
    }
}

回答by Maddy Byahoo

getline is storing the entire line at once, which is not what you want. A simple fix is to have three variables and use cin to get them all. C++ will parse automatically at the spaces.

getline 一次存储整行,这不是您想要的。一个简单的解决方法是拥有三个变量并使用 cin 来获取它们。C++ 将在空格处自动解析。

#include <iostream>
using namespace std;

int main() {
    string a, b, c;
    cin >> a >> b >> c;
    //now you have your three words
    return 0;
}

I don't know what particular "operation" you're talking about, so I can't help you there, but if it's changing characters, read up on string and indices. The C++ documentation is great. As for using namespace std; versus std:: and other libraries, there's already been a lot said. Try thesequestionson StackOverflow to start.

我不知道你在谈论什么特定的“操作”,所以我无法帮助你,但如果它正在改变字符,请阅读字符串和索引。C++ 文档很棒。至于使用命名空间 std; 与 std:: 和其他库相比,已经说了很多。在 StackOverflow 上尝试这些问题开始。