C++ 打印句子c+的短语时出现错误+; 矢量单字短语; 向量双字短语; 向量三字短语; string str=“你好,我是bob oh hey jay oh”; 向量::迭代器it1; 向量::迭代器it2;
我用str将句子分解成单个单词,并将它们存储到称为oneWordPhrase的向量中。因此,向量的大小为7C++ 打印句子c+的短语时出现错误+; 矢量单字短语; 向量双字短语; 向量三字短语; string str=“你好,我是bob oh hey jay oh”; 向量::迭代器it1; 向量::迭代器it2;,c++,regex,vector,iterator,C++,Regex,Vector,Iterator,我用str将句子分解成单个单词,并将它们存储到称为oneWordPhrase的向量中。因此,向量的大小为7 vector <string> oneWordPhrase; vector <string> twoWordPhrase; vector <string> threeWordPhrase; string str="hello my is bob oh hey jay oh"; vector<string>::iterator it
vector <string> oneWordPhrase;
vector <string> twoWordPhrase;
vector <string> threeWordPhrase;
string str="hello my is bob oh hey jay oh";
vector<string>::iterator it1;
vector<string>::iterator it2;
for(it1=oneWordPhrase.begin();it1!=oneWordPhrase.end();it1++)
{
if(it1+1==oneWordPhrase.end())
break;/*如果到达向量的最后一个元素
别兜圈子了,因为我们到了终点*/
双字短语。向后推(*it1+“”+*(it1+1));
}//获取句子中的每两个连续单词
cout为了回答最初的问题,发布的代码是正确的。正如约瑟夫曼斯菲尔德所怀疑的那样,显示代码(未发布)中有一个错误,如图所示
下面是关于风格和最佳实践的评论,我认为下面是更惯用的C++:
for(int i=0; i<twoWordPhrase.size()-1; i++)
{
it1=twoWordPhrase.begin()+i;
it2=oneWordPhrase.begin()+i+2;
if(it1==twoWordPhrase.end()-1)
break; //signal break but doesnt work
threeWordPhrase.push_back(*it1 + ' ' + *it2);
}
cout<<"three words-----------\n";
for(int i=0; i<threeWordPhrase; i++)
cout<<threeWordPhrase[i]<<endl;
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
typedef向量字符串;
int main(){
字符串s=“一二三四五”;
/*stringstream是一个类似std::cout的流
但是读取/写入字符串
而不是标准的输入/输出*/
溪流ss(s);;
/*vector有一个2参数构造函数
它用两个迭代器之间的所有元素填充向量
istream_迭代器通过流进行迭代,就像
使用运算符>>以便逐字读取*/
字符串字=
字符串(istream_迭代器(ss),istream_迭代器());
字符串三个字;
对于(size_t i=0;i 难道你还没有展示你是如何打印ThreeWordPhase
元素的。@JosephMansfield srsly?@Paranix说真的什么?我想如果你对容器进行索引,而不是做迭代器算术,混合来自不同容器的迭代器,会更清楚。哦,是的……我通过在for循环中固定范围来打印Hey antoine,你能详细解释一下混合整数指示符和迭代器是什么意思吗?我是个笨蛋。你可能从我的代码中可以看出,我使用蛮力来执行必要的操作output@user3239138:例如,您使用相同的变量it1
处理不同的事情:迭代oneWordPhrase
和twoordphrase
。它显然工作得很好,但随着代码的增长,我建议只使用一个变量。在您的情况下,在第二个循环中,您确实不需要it1
,因为您可以用twoordphrase[I]替换*it1
这就是我所说的整数索引。@ USSR323 9138:因为你从C++开始,我用一个(希望)更多的习惯性代码来编辑我的文章,希望它帮助我思考它,我想用一个迭代器在我快要到达末尾时计数。因此,if(IT1= = WordOrthPosithEnter)(1)。如果没有迭代器,我想不出一种方法来实现这一点。没问题,别忘了投票/接受有用的答案;)还要注意,如果你有std::list而不是std::vector,你将不得不同时使用int来保持计数和迭代器,因为在大多数容器上,你不能进行迭代器算术(如end()-1
)。
for(int i=0; i<twoWordPhrase.size()-1; i++)
{
it1=twoWordPhrase.begin()+i;
it2=oneWordPhrase.begin()+i+2;
if(it1==twoWordPhrase.end()-1)
break; //signal break but doesnt work
threeWordPhrase.push_back(*it1 + ' ' + *it2);
}
cout<<"three words-----------\n";
for(int i=0; i<threeWordPhrase; i++)
cout<<threeWordPhrase[i]<<endl;
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;
typedef vector<string> strings;
int main() {
string s = "one two three four five";
/* stringstream is a stream just like std::cout
but reads/writes from/to a string
instead of the standard input/output */
stringstream ss(s);
/* std::vector has a 2 argument constructor
which fills the vector with all elements between two iterators
istream_iterator iterates throuw the stream as if
using operator>> so it reads word by word */
strings words =
strings(istream_iterator<string>(ss), istream_iterator<string>());
strings threeWords;
for(size_t i = 0; i < words.size()-2; ++i)
threeWords.push_back(words[i] + ' ' + words[i+1] + ' ' + words[i+2]);
for(size_t i = 0; i < threeWords.size(); ++i)
cout << i << ": " << threeWords[i] << endl;
return 0;
}