从一个字符串中获取 2-5 个连续的单词短语,我得到了 2 个工作,但在做 3 个时遇到麻烦
getting 2-5 consecutive word phrases from a string I got 2 to work but having trouble doing 3
vector <string> oneWordPhrase;
vector <string> twoWordPhrase;
vector <string> threeWordPhrase;
vector<string>::iterator it1;
vector<string>::iterator it2;
string str="hello my is bob oh hey jay oh";
string split = str;
string word;
stringstream stream(split);
while( getline(stream, word, ' ') )
{
oneWordPhrase.push_back(word);
}//used to split sentence into words
for(it1=oneWordPhrase.begin(); it1!=oneWordPhrase.end(); it1++)
{
if(it1+1 == oneWordPhrase.end())
break;
twoWordPhrase.push_back(*it1 + ' ' + *(it1+1));
}//getting two word phrases
cout<<"two word---------------n";
for(int i=0; i<twoWordPhrase.size(); i++)
cout<<twoWordPhrase[i]<<endl;
for(it1=twoWordPhrase.begin(); it1!=twoWordPhrase.end(); it1++)
{
it2=oneWordPhrase.begin()+2;
threeWordPhrase.push_back(*it1 + ' ' + *it2);
++it2; /* was hoping that I can get each word after "is" but it
didn't allow me. the problem is here */
}//getting three word phrases
cout<<"three word---------------n";
for(int i=0; i<twoWordPhrase.size(); i++)
cout<<threeWordPhrase[i]<<endl;
我让我的两个短语正确打印,即
你好我的
我的是
是鲍勃
鲍勃·
哦 嘿
嘿杰伊
周杰伦哦
但是,我的三个单词短语打印出来
你好我的是
我的是
是鲍勃是
鲍勃哦是
哦,嘿是
嘿杰伊是
周杰伦是
对于三个单词短语,我希望将"hello my is"my is bob"is bob oh"bob oh hey"一直打印到"hey jay oh"。
我将我的it2指向myWordPhrase.begin()+2,并希望它会像在数组中一样增加1,但事实并非如此。
我评论了给我带来问题的代码部分
我很确定如果我能找出 3 个单词短语,我可以做 4 个和 5 个单词短语,所以对 3 个单词的任何帮助将不胜感激!
当你这样做*it2 = oneWordPhrase.begin() + 2
它总是给你一个单词短语向量中的第三个成员。您可以使用计数器而不是迭代器,因为您需要迭代两个向量:
vector <string> oneWordPhrase;
vector <string> twoWordPhrase;
vector <string> threeWordPhrase;
vector<string>::iterator it1;
vector<string>::iterator it2;
string str="hello my is bob oh hey jay oh";
string split = str;
string word;
stringstream stream(split);
while( getline(stream, word, ' ') )
{
oneWordPhrase.push_back(word);
}//used to split sentence into words
for(it1=oneWordPhrase.begin(); it1!=oneWordPhrase.end(); it1++)
{
if(it1+1 == oneWordPhrase.end())
break;
twoWordPhrase.push_back(*it1 + ' ' + *(it1+1));
}//getting two word phrases
cout<<"two word---------------n";
for(int i=0; i<twoWordPhrase.size(); i++)
cout<<twoWordPhrase[i]<<endl;
for(int i=0; i!=twoWordPhrase.size() - 2; i++)
{
threeWordPhrase.push_back( twoWordPhrase[i] + ' ' + oneWordPhrase[i + 2] );
/* was hoping that I can get each word after "is" but it
didn't allow me. the problem is here */
}//getting three word phrases
cout<<"three word---------------n";
for(int i=0; i<twoWordPhrase.size() - 2; i++)
cout<<threeWordPhrase[i]<<endl;
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 编译包含字符串的代码时遇到问题
- 导入库可以跨dll版本工作吗
- 遇到新行时,有没有办法停止istream_iterator
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- 我在循环中的循环中遇到问题,无法正常工作
- Atmel Studio 7:ASF 库提供的 USART 中断处理程序仅在 main() 中遇到无限循环时工作一次?
- 当我的代码工作正常时,为什么在SPOJ中遇到运行时(Sigabrt)错误
- 我在while循环中遇到问题,我只是无法弄清楚为什么它无法按预期工作
- 在打印出动态分配的数组中的前两个数字时遇到问题,其他数字工作正常
- 从一个字符串中获取 2-5 个连续的单词短语,我得到了 2 个工作,但在做 3 个时遇到麻烦
- 在工作中遇到这个"operator ClassName *".这是什么意思?
- 我在让非常基本的命令提示符文本处理器工作时遇到问题。ofstream() 的问题
- 递归解决方案未按预期工作/遇到错误