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

幽灵般的

我需要一些帮助使函数将句子拆分为单词,并且此函数应适用于具有不同长度的句子。

这是示例代码:

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(sentence);时,它给了我错误。在代码中。另外,我正在使用c ++ 98,并使用cygwin编译程序。有线索吗?谢谢。

编辑:该函数将创建多个变量,具体取决于句子中有多少个单词。

编辑:我实际上是在一个LinkedList程序上工作,我在这里想要做的是将句子拆分成单词,然后生成包含每个单词的新节点。

这是实际的代码(请注意:我对它进行了一些修改,因此它与我的实际代码并不完全相同。另外,我没有在Node上使用struct),假设句子1是“大家好”,句子2是“你好”我在做东西”。

The expected output will be:
linkedlist1:
"hello"<->"everyone"
linkedlist2:
"hello"<->"I"<->"am"<->"doing"<->"stuff"

在LinkedList.cpp中:

void LinkedList::add(std::string sentence)
{
   //breaks down the sentence into words
   std::istringstream ss(sentence);
   do
   {
       std::string word;
       ss >> word;

       //store them in nodes in a linkedlist
       Node* new_tail = new Node(word);
       if (size == 0)
       {
           head = new_tail;
           tail = new_tail;
       }
       else
       {
           new_tail->set_previous(tail);
           tail->set_next(new_tail);
           tail = new_tail;
       }
       new_tail = NULL;
       size++;

   }
   while(ss);
}

[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;

}

这是输出

this
is
a
test
sentence
for
stackoverflow

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将句子拆分为单词并将其制成列表(Python)

从给定的文本中搜索单词并将其拆分为变量

将一个句子拆分成两个单词,并将其值存储在HashMap中作为键

如何将句子分为单词

PHP如何将句子中的单词存储到字符串中而不会爆炸

如何阅读每个单词并将其存储到变量中以供使用?

如何拆分字符串并将其存储在变量中?

如何将句子拆分为单词以及标点符号和空格?[迅速]

如何使用 Java 中的拆分方法将字符串句子拆分为单词?

如何将“ls”的结果存储在变量中,并将 cd 存储到该变量中

将字符串拆分为单个单词并将其放入向量中

如何从txt文件中获取数据。将其拆分为两个变量,并将其存储和修改为我在开始时用于存储数据的数组

RegEx将单词和符号中的句子拆分为例外

如何将函数值存储在变量中并将其显示在textarea上

如何将JSON输出转换为c#对象并将其存储到数组中

在c#中按长度变量将字符串拆分为更小的单词

如何将大型查询拆分为小型查询并将其作为单个查询追加

如何将字符串字典转换为字典并将其拆分为单独的列

如何将数组拆分为块并找到块的总和并将输出作为数组存储在pyspark中

如何从json接收数据并将其存储到变量中?

将单词拆分为变量

如何将字符串中的每个单词存储到特定变量中?

如何将所有环境变量导出到JSON并将其存储在Bash中的变量中?

如何拆分逗号分隔的字符串并将其存储到变量 VB Net

将熊猫df中的句子拆分为单词行并为每个句子编号

将 Pandas 中的句子(字符串)拆分为带有句子编号的单独单词行

检测句子中的 AND、OR 并将其拆分

如何将文件中的单词存储在单独的变量中?

从文件中读取,拆分并将其存储在变量中