C++ 如何将任意长度的句子拆分为单词并将其存储到变量c++;

C++ 如何将任意长度的句子拆分为单词并将其存储到变量c++;,c++,split,c++98,C++,Split,C++98,我需要一些帮助来制作一个函数来将句子拆分成单词,这个函数应该可以处理不同长度的句子 以下是示例代码: void spilt_sentence(string sentence) {} int main() { std::string sentence1= "Hello everyone"; std::string sentence2= "Hello I am doing stuff"; split_sentence(sentence1); split_sentence(se

我需要一些帮助来制作一个函数来将句子拆分成单词,这个函数应该可以处理不同长度的句子

以下是示例代码:

void spilt_sentence(string sentence)
{}
int main()
{
   std::string sentence1= "Hello everyone";
   std::string sentence2= "Hello I am doing stuff";
   split_sentence(sentence1);
   split_sentence(sentence2);
   return 0;
}
我看到有人使用std::istringstream获取每个空格前的每个单词,但我真的不知道它是如何工作的。当我把std::istringstream ss(句子)放进去时,它会给我错误;在代码中。另外,我正在使用c++98,我用cygwin编译我的程序。有线索吗?多谢各位

编辑:该函数将根据句子中的单词数量创建许多变量

编辑:我实际上正在开发一个LinkedList程序,我在这里尝试的是将句子拆分成单词,然后生成包含每个单词的新节点

下面是实际的代码(注意:我对它做了一点修改,所以它与我的实际代码不完全相同。另外,我没有对节点使用struct),让我们假设句子1是“大家好”,句子2是“你好,我正在做事情”

[FIXED]编译时会弹出一条错误消息,说std::istringstream ss具有默认设置,但类型不完整。我该怎么办


这是使用流的函数,此函数仅适用于向量,您不能将此函数用于数组,但如果您愿意,可以为您修改它。 下面是代码和用法示例

#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;

void split_sentence(const string& str, vector<string>& cont)
{
    istringstream iss(str);
    copy(istream_iterator<string>(iss),
         istream_iterator<string>(),
         back_inserter(cont));


     //checking for punctuation marks and if found, we remove them from the word
     for(int i = 0, sz = cont.size(); i < sz; i++){
        string word = cont.at(i);
        for(int j = 0, len = word.length(); j < len; j++){
            if(ispunct(word[j])){
                cont.at(i) = word.substr(0, word.length() - 1);
            }
        }
     }
}

int main(){

    string sentence = "this is a test sentence for stackoverflow!";
    vector<string> words;

    split_sentence(sentence, words);

    for(int i = 0, sz = words.size(); i < sz; i++){
        cout<<words.at(i) << endl;
    }

    return 0;

}

如果您还想打印标点符号,请删除函数中的double for loop。

这是使用streams的函数,此函数仅适用于向量,您不能将此函数用于数组,但如果您愿意,可以为您修改它。 下面是代码和用法示例

#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;

void split_sentence(const string& str, vector<string>& cont)
{
    istringstream iss(str);
    copy(istream_iterator<string>(iss),
         istream_iterator<string>(),
         back_inserter(cont));


     //checking for punctuation marks and if found, we remove them from the word
     for(int i = 0, sz = cont.size(); i < sz; i++){
        string word = cont.at(i);
        for(int j = 0, len = word.length(); j < len; j++){
            if(ispunct(word[j])){
                cont.at(i) = word.substr(0, word.length() - 1);
            }
        }
     }
}

int main(){

    string sentence = "this is a test sentence for stackoverflow!";
    vector<string> words;

    split_sentence(sentence, words);

    for(int i = 0, sz = words.size(); i < sz; i++){
        cout<<words.at(i) << endl;
    }

    return 0;

}

如果您还想打印标点符号,请删除函数中的双for loop。

嗨,很有趣,也许这个答案可能会引起@IronMan的兴趣,谢谢您的快速回复。我正在看一看。一旦我弄明白了,我会更新代码。也许这是给你的:欢迎来到。从您给出的不完整代码中,不可能精确地找出代码的确切问题。请提供一个例子来演示您所遇到的错误。对不起,我不太擅长用我正在编写的程序来举例。我会稍微修改一下实际程序的代码,然后在帖子中展示出来。嗨,很有趣,也许这个答案会引起@IronMan的兴趣,谢谢你的快速回复。我正在看一看。一旦我弄明白了,我会更新代码。也许这是给你的:欢迎来到。从您给出的不完整代码中,不可能精确地找出代码的确切问题。请提供一个例子来演示您所遇到的错误。对不起,我不太擅长用我正在编写的程序来举例。我将稍微修改一下实际程序的代码,然后在帖子中显示它。
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;

void split_sentence(const string& str, vector<string>& cont)
{
    istringstream iss(str);
    copy(istream_iterator<string>(iss),
         istream_iterator<string>(),
         back_inserter(cont));


     //checking for punctuation marks and if found, we remove them from the word
     for(int i = 0, sz = cont.size(); i < sz; i++){
        string word = cont.at(i);
        for(int j = 0, len = word.length(); j < len; j++){
            if(ispunct(word[j])){
                cont.at(i) = word.substr(0, word.length() - 1);
            }
        }
     }
}

int main(){

    string sentence = "this is a test sentence for stackoverflow!";
    vector<string> words;

    split_sentence(sentence, words);

    for(int i = 0, sz = words.size(); i < sz; i++){
        cout<<words.at(i) << endl;
    }

    return 0;

}
this
is
a
test
sentence
for
stackoverflow