C++ 从字符串c ++中提取单个单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39050225/
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-28 14:48:22  来源:igfitidea点击:

extract individual words from string c++

c++stringvector

提问by VVSTITAN

I am trying to make a C++ program that receives user input, and extracts the individual words in the string, e.g. "Hello to Bob" would get "Hello", "to", "Bob". Eventually, I will be pushing these into a string vector. This is the format I tried to use when designing the code:

我正在尝试制作一个接收用户输入的 C++ 程序,并提取字符串中的单个单词,例如“Hello to Bob”会得到“Hello”、“to”、“Bob”。最终,我会将这些推入一个字符串向量中。这是我在设计代码时尝试使用的格式:

//string libraries and all other appropriate libraries have been included above here
string UserInput;
getline(cin,UserInput)
vector<string> words;
string temp=UserInput;
string pushBackVar;//this will eventually be used to pushback words into a vector
for (int i=0;i<UserInput.length();i++)
{
  if(UserInput[i]==32)
  {
    pushBackVar=temp.erase(i,UserInput.length()-i);
    //something like words.pushback(pushBackVar) will go here;
  }  
}

However, this only works for the first space encountered in the string.It does not work if there are any spaces before the word (e.g. if we have "Hello my World", pushBackVar will be "Hello" after the first loop, and then "Hello my" after the second loop, when I want "Hello" and "my".) How do I fix this? Is there any other better way to extract individual words from a string? I hope I haven't confused anyone.

但是,这只适用于字符串中遇到的第一个空格。如果单词前有任何空格,则不起作用(例如,如果我们有“Hello my World”,则在第一个循环之后 pushBackVar 将是“Hello”,然后在第二个循环之后,当我想要“Hello”和“my”时,“Hello my”。)我该如何解决这个问题?还有其他更好的方法可以从字符串中提取单个单词吗?我希望我没有混淆任何人。

回答by macco

See Split a string in C++?

请参见在 C++ 中拆分字符串?

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

using namespace std;

void split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }
}


vector<string> split(const string &s, char delim) {
    vector<string> elems;
    split(s, delim, elems);
    return elems;
}

So in your case just do:

因此,在您的情况下,只需执行以下操作:

words = split(temp,' ');

回答by Cheers and hth. - Alf

#include <algorithm>        // std::(copy)
#include <iostream>         // std::(cin, cout)
#include <iterator>         // std::(istream_iterator, back_inserter)
#include <sstream>          // std::(istringstream)
#include <string>           // std::(string)
#include <vector>           // std::(vector)
using namespace std;

auto main()
    -> int
{
    string user_input;
    getline( cin, user_input );
    vector<string> words;
    {
        istringstream input_as_stream( user_input );
        copy(
            istream_iterator<string>( input_as_stream ),
            istream_iterator<string>(),
            back_inserter( words )
            );
    }

    for( string const& word : words )
    {
        cout << word << '\n';
    }
}

回答by TheArquitect

You can use the operator >> direct to a microbuffer (string) to extract the word. (getline is not needed). Take a look at the function below:

您可以使用运算符 >> 直接到微缓冲区(字符串)来提取单词。(不需要getline)。看看下面的函数:

vector<string> Extract(const string& Text) {
    vector<string> Words;
    stringstream ss(Text);
    string Buf;

    while (ss >> Buf)
        Words.push_back(Buf);

    return Words;
}