C++ 到达字符串中的第三个单词

C++ 到达字符串中的第三个单词,c++,string,word,C++,String,Word,可能重复: 我问了一个非常类似的问题,但显然我问错了。问题是,我需要在C++中用字符串来达到第三个单词,字符串就是这样的: word2里面可以有空格 我试图一个字符一个字符地读这个字符串,但我发现效率很低。我试过密码 std::istringstream str(array[i]); str >> temp >> temp >> word; array2[i] = word; 因为word2里面有空格,所以它不起作用 你能告诉我怎么做吗?最简单的方法

可能重复:

我问了一个非常类似的问题,但显然我问错了。问题是,我需要在C++中用字符串来达到第三个单词,字符串就是这样的:

word2里面可以有空格


我试图一个字符一个字符地读这个字符串,但我发现效率很低。我试过密码

std::istringstream str(array[i]); 
str >> temp >> temp >> word; 
array2[i] = word; 
因为word2里面有空格,所以它不起作用


你能告诉我怎么做吗?

最简单的方法是:

#include <iostream>
int main()
{
    //input string:
    std::string str = "w o r d 1\tw o r d2\tword3\tword4";
    int wordStartPosition = 0;//The start of each word in the string

    for( int i = 0; i < 2; i++ )//looking for the third one (start counting from 0)
        wordStartPosition = str.find_first_of( '\t', wordStartPosition + 1 );

    //Getting where our word ends:
    int wordEndPosition = str.find_first_of( '\t', wordStartPosition + 1 );
    //Getting the desired word and printing:
    std::string result =  str.substr( wordStartPosition + 1, str.length() - wordEndPosition - 1 );
    std::cout << result;
}
试试下面的例子。 您的第三个单词是std::vector中的第三项

创建一个splitstring函数,用于将大字符串拆分为std::vector对象。使用该std::vector获取第三个字符串

参见下面的示例,尝试在一个空的C++控制台项目中运行。

#include <stdio.h>
#include <vector>
#include <string>

void splitString(std::string str, char token, std::vector<std::string> &words)
{
    std::string word = "";
    for(int i=0; i<str.length(); i++)
    {
        if (str[i] == token)
        {
            if( word.length() == 0 )
                continue;

            words.push_back(word);
            word = "";
            continue;
        }

        word.push_back( str[i] );
    }
}


int main(int argc, char **argv)
{
    std::string stream = "word1\tword2\tword3\tword4\tword5\tword6";

    std::vector<std::string> theWords;
    splitString( stream, '\t', theWords);

    for(int i=0; i<theWords.size(); i++)
    {
        printf("%s\n", theWords[i].c_str() );
    }

    while(true){}
    return 0;
}
#包括
#包括
#包括
void splitString(std::string str、char标记、std::vector和words)
{
std::string word=“”;

对于(int i=0;iSo,到目前为止你尝试了什么?你一定尝试了什么。你在哪里被卡住了?你显然知道单词之间的区别。你是如何搜索的?这是你第三次问或多或少相同的问题…只使用你已经收到的!编辑:或改编一点。你没有向我们展示你尝试过的内容。嘘ow us.你被困在哪里了?看来你对上一个问题找到了一个解决方案,应该对你有所帮助。word2里面可以有空格吗?你应该至少给我们看实际的字符串..我试着一个字符一个字符地读字符串,但我发现效率不高。我试过
code
std::istringstream str(数组[I]);str>>temp>>temp>>word;array2[i]=word;由于word2中的空格,它无法工作。。
word3
#include <stdio.h>
#include <vector>
#include <string>

void splitString(std::string str, char token, std::vector<std::string> &words)
{
    std::string word = "";
    for(int i=0; i<str.length(); i++)
    {
        if (str[i] == token)
        {
            if( word.length() == 0 )
                continue;

            words.push_back(word);
            word = "";
            continue;
        }

        word.push_back( str[i] );
    }
}


int main(int argc, char **argv)
{
    std::string stream = "word1\tword2\tword3\tword4\tword5\tword6";

    std::vector<std::string> theWords;
    splitString( stream, '\t', theWords);

    for(int i=0; i<theWords.size(); i++)
    {
        printf("%s\n", theWords[i].c_str() );
    }

    while(true){}
    return 0;
}