句子变成单词c++字符串

Sentence into word c++ string

本文关键字:c++ 字符串 单词 句子      更新时间:2023-10-16

如何在c++中将字符串类型的句子分解为单词并存储在字符串类型的向量中?示例

String str="my name";

进入

Vector word={" my","name"}

您可以编写一个简单的循环:

std::vector<std::string> words;
std::istringstream is("my name");
std::string word;
while (is >> word) {
    // ...
    words.push_back(word);
    // ...
}

在我看来,这是个好主意,因为除了简单地提取单词外,你很可能还需要对这些单词做其他事情。环的主体可以很容易地延伸。