C++Primer第五章习题14

题目:读取若干字符串,并找出此若干字符串中连续重复字符串以及其个数。

涉及到: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;
}

我自己也晕了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值