C++ 向后推向量中的所有内容<;char>;将它们组合为向量的第一个元素<;字符串>;

C++ 向后推向量中的所有内容<;char>;将它们组合为向量的第一个元素<;字符串>;,c++,string,C++,String,我试图将一个带有空格的字符串解析成几个字符串,并将它们存储到一个列表中,该列表由没有空格的字符串组成。我不知道字符串的输入将持续多长时间,我有以下代码: #include <bits/stdc++.h> #include <sstream> using namespace std; vector<string> myWords; vector<char> myBuffer; int main() { string mySentence

我试图将一个带有空格的字符串解析成几个字符串,并将它们存储到一个列表中,该列表由没有空格的字符串组成。我不知道字符串的输入将持续多长时间,我有以下代码:

#include <bits/stdc++.h>
#include <sstream>

using namespace std;

vector<string> myWords;
vector<char> myBuffer;

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

    int j = 0;
    for (int i = 0; i < mySentence.length(); i++) {
        if (mySentence[i] != ' ') myBuffer.push_back(mySentence[i]);
        else {
            myWords.push_back(myBuffer);
            myBuffer.clear();
            j++;
        }
    }

    return 0;
}
#包括
#包括
使用名称空间std;
向量词;
载体myBuffer;
int main(){
弦长;
getline(cin,mycentence);
int j=0;
for(int i=0;i

我得到的错误是在
myWords.push_back(myBuffer)。我该如何解决这个问题?

问题是,您试图将
std::vector
推送到需要
std::string
的位置。因此,只需将
myBuffer
的类型更改为
std::string

#include <iostream>
#include <string>

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;
    std::string myBuffer;

    for (int i = 0; i < mySentence.length(); i++) {
        if (mySentence[i] != ' ')
            myBuffer.push_back(mySentence[i]);
        else {
            myWords.push_back(myBuffer);
            myBuffer.clear();
        }
    }

    if (!myBuffer.empty()) {
        myWords.push_back(myBuffer);
    }

    // use myWords as needed...

    return 0;
}
或者,让标准库为您处理阅读和推送:

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;

    std::istringstream iss(mySentence);
    std::copy(
        std::istream_iterator<std::string>(iss),
        std::istream_iterator<std::string>(),
        std::back_inserter(myWords)
    );

    // use myWords as needed...

    return 0;
}
#包括
#包括
#包括
#包括
int main(){
std::字符串mycentence;
std::getline(std::cin,mycentence);
向量词;
std::istringstream iss(mycentence);
复制(
std::istream_迭代器(iss),
std::istream_迭代器(),
标准::背面插入器(myWords)
);
//根据需要使用myWords。。。
返回0;
}

问题在于,您试图将
std::vector
推送到需要
std::string
的位置。因此,只需将
myBuffer
的类型更改为
std::string

#include <iostream>
#include <string>

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;
    std::string myBuffer;

    for (int i = 0; i < mySentence.length(); i++) {
        if (mySentence[i] != ' ')
            myBuffer.push_back(mySentence[i]);
        else {
            myWords.push_back(myBuffer);
            myBuffer.clear();
        }
    }

    if (!myBuffer.empty()) {
        myWords.push_back(myBuffer);
    }

    // use myWords as needed...

    return 0;
}
或者,让标准库为您处理阅读和推送:

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;

    std::istringstream iss(mySentence);
    std::copy(
        std::istream_iterator<std::string>(iss),
        std::istream_iterator<std::string>(),
        std::back_inserter(myWords)
    );

    // use myWords as needed...

    return 0;
}
#包括
#包括
#包括
#包括
int main(){
std::字符串mycentence;
std::getline(std::cin,mycentence);
向量词;
std::istringstream iss(mycentence);
复制(
std::istream_迭代器(iss),
std::istream_迭代器(),
标准::背面插入器(myWords)
);
//根据需要使用myWords。。。
返回0;
}

调试输出显示您找到了什么?我不知道你想做什么,还有“错误”。。很高兴知道。
#include
--我正在尝试将一个带空格的字符串解析为多个字符串,并将它们存储到一个列表中--如果使用@tedlynmo,这会简单得多。错误是:
错误:调用'std::vector'时没有匹配的函数。调试输出表明您找到了什么?我不知道你想做什么,还有“错误”。。很高兴知道。
#include
——我正试图将一个带空格的字符串解析为多个字符串,并将它们存储到一个列表中——如果使用@TEDLYNGOM,这会简单得多。错误是:
错误:调用'std::vector'时没有匹配的函数