用迭代器判断字符串相等

写的有点乱  但是重点是你比较这两个字符串是不是相等的时侯  不能用if(*beg == *(++beg))   这样会一直比较两个相同的字符串    

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {

/*找重复单词*/
vector<string>str;
string strr;
while (cin >> strr) {
str.push_back(strr);
}
auto beg = str.begin();
int i = 1 , t=0 , max =1 , maxt=0;
int flag = 0;
while (beg != str.end()-1)
{
string ex1 = *beg;
string ex = *(++beg);
if (ex1 == ex)
{
++i;
++t;
flag = 1;
if (max<i)
{
max = i;
maxt = t;
}
}
else
{
if (max<i)
{
max = i;
maxt = t;
}
i = 1;
++t;
}
}
if (flag == 0)
{
cout << "same";
}
else {
cout << str[maxt] << " " << max;

猜你喜欢

转载自www.cnblogs.com/mikekuok/p/9462646.html