![]() |
#2
rjsp2015-02-02 08:50
|
我把相同字符串的个数及这个字符串记下。代码如下。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
vector<string> vec;
vector<int> count;
string str1="abcL";
int cou = 1;
while(cin>>str)
{
if(str1!=str)
{
if(cou>=2)
{
vec.push_back(str1);
count.push_back(cou);
cou=1;
}
}
else
{
cou++;
}
str1=str;
}
for(int it=0; it!=vec.size();it++)
{
cout<<"vec["<<it<<"] == "<<vec[it]<<" count["<<it<<"] == "<<count[it]<<endl;
}
getchar();
return 0;
}
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
vector<string> vec;
vector<int> count;
string str1="abcL";
int cou = 1;
while(cin>>str)
{
if(str1!=str)
{
if(cou>=2)
{
vec.push_back(str1);
count.push_back(cou);
cou=1;
}
}
else
{
cou++;
}
str1=str;
}
for(int it=0; it!=vec.size();it++)
{
cout<<"vec["<<it<<"] == "<<vec[it]<<" count["<<it<<"] == "<<count[it]<<endl;
}
getchar();
return 0;
}
但我输入 mm mm b b b b 或其它的字符串,但后面几个相同时。 如果后面不回车直接用ctrl+Z结束。再回车用ctrl+Z结束一次
可以正确输出 b 的次数为4次。但如果直接回车后用用ctrl+Z结则没结果。
希望大家帮我看看,谢谢。