题目:读取若干字符串,并找出此若干字符串中连续重复字符串以及其个数。
涉及到:while()、vector()、迭代器等知识。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void main()
{
string My_string, before_string, max_repeatstring;
vector<string> vec_word;//存放每个连续输入的单词
vector<int> vec_num;//存放每个连续输入的次数
int repeat_number = 0, flag = 0, m = 0;
while (cin >> My_string)
{
if (My_string == before_string)
{
++repeat_number; cout << "repeat_number=" << repeat_number << endl;
m++;//此变量是因为最后输入的单词不会在调用else,所以需要记录下本单词的出现次数
}
else
{
vec_num.push_back(repeat_number);//记录下上一个单词的重复次数
repeat_number = 1;
before_string = My_string;
vec_word.push_back(My_string);//此次输入了与上次不同的单词,记录下来
m = 0;
}
if (My_string == "q")break;
}
cout << "m="<<m + 1<<endl;
for (auto i : vec_word)
cout << "vec_word=" << i << endl;;
vec_num.push_back(m + 1);
int a = 0;
vector<int>::iterator it1 = vec_num.begin();
for (it1; it1 != vec_num.end(); ++it1)
{
if (*it1 >a)
{
a = *it1;//找到最大元素
}
}
for (int i = 0; i< vec_num.size(); i++)
{
if (vec_num[i] == a)
{
cout << "单词" << vec_word[i - 1] << "出现的次数为:" << vec_num[i] << "次" << endl;
}
}
}
二 、数组
#include <iostream>
#include <string>
using namespace std;
void PrintArry(int(*cc)[3], int r, int c)
{
for (int i = 0; i<r; i++)
{
for (int j = 0; j<c; j++)
{
cout <<cc[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int cc[3][3] = { { 51, 55, 5 }, { 53, 54, 6 }, { 88, 7, 7 } };
int c[]= { 1, 2, 3, 4 };
int *a[4]; //指针数组
int(*b)[3]; //数组指针
int(*d)[4];
cout << endl << "***************一维数组指针***************" << endl;
d = &c;
for (int i = 0; i < 4; i++)
cout << (*d)[i] << " ";
cout << endl << "***************二维数组指针***************" << endl;
b = cc;
for (int i = 0; i <3; i++)
for (int j = 0;j <3;j++)
{
cout << *(*(b + i) + j) <<" ";
}
cout << endl<<"**************一维指针数组*****************" << endl;
for (int i = 0; i<4; i++)
{
a[i] = &c[i];
}
//输出看下结果
for (int i = 0; i < 4; i++)
{
cout << *a[i] << endl;
}
cout << endl << "**************二维指针数组*****************" << endl;
PrintArry(cc, 3, 3);//等价于PrintArry(&arry[0],3,4);
return 0;
}
我自己也晕了。