C++ primer5 5.14/5.17/5.19/5.20/5.21

5.14 从标准输入中读取若干string对象并查找连续重复出现的单词,统计出现重复最大次数的单词,比如在how now now now brown cow cow中now连续出现3次

//5.14
#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
int main() 
{
    pair<string, int> max_duplicated; //字符串及其最大重复次数
    int count = 0;
    for (string curstr,prestr; cin>>curstr; prestr=curstr)
    {
        if (curstr == prestr) 
		     ++count;   //出现重复则计数值加1,否则没有重复
        else 
		     count = 0; 
        if (count>max_duplicated.second) //计数来到更大的值,更新
		     max_duplicated = {prestr, count};
		//循环体语句完成后务必执行prestr=curstr
    }
    
    if (max_duplicated.first.empty()) 
	    cout<<"There's no duplicated string."<<endl;
    else 
	    cout<<"the word "<<max_duplicated.first<<" occurred "<<max_duplicated.second+1<<" times. "<<endl;

	system("pause");
    return 0;
}

或者

#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
int main() {
    string before,current,key;
    int cnt=1;
    vector<string> str;
    vector<int> num;
    
    while (cin >> current) 
	{
        if (before.empty()) 
		{   //输入的是第一个词
            before = current;
        }
        else if (current == before) 
		{
            ++cnt;
        }
        else
		{
            str.push_back(before);//注意不是current
            num.push_back(cnt);
            before = current;
            cnt = 1;
        }
    }
    if (cnt != 1) 
	{           //最后一个词不能进else,所以补充
        str.push_back(current);
        num.push_back(cnt);
    }
    
    vector<string>::iterator iter;
    vector<int>::iterator n;
    for (iter=str.begin(),n=num.begin(); iter != str.end(); ++iter,++n) 
	{//输出所有词的出现次数
        cout << *iter << "occurred times" << *n <<endl;
    }

	system("pause");
    return 0;
}

5.17 检验一个vector对象是否是另一个的前缀,挑出长度较短的那个比较元素内容即可,比如0,1,1,2和0,1,1,2,3,5,8返回为真

//5.17
#include <iostream>
#include <vector>
#include <string>
using namespace std;

bool is_prefix(vector<int> const& avec, vector<int> const& bvec)
{
	//挑出返回长度较小者,其长度作为共有长度
	int small_size = avec.size()<bvec.size() ? avec.size() : bvec.size();

	for(unsigned i = 0; i != small_size; ++i) //共有长度比较时出现不相等
        if(avec[i]!=bvec[i])
		   return false;
    return true;                                //共有长度相等(前缀)
}
int main()
{
	int a[4]={0,1,1,2};
	int b[7]={0,1,1,2,3,5,8};
    vector<int> vec1(a,a+4); //数组给vector初试化,提供首尾地址
	vector<int> vec2(b,b+7);

	cout << ( is_prefix(vec1,vec2) ? "yes\n" : "no\n" ); //提供实参

    system("pause");
    return 0;
}

5.19 使用do while循环重复执行下述任务:首先提示用户输入两个string对象,然后挑出较短的那个并输出

#include <iostream>
#include <string>

using namespace std;
int main()
{
    string rsp;
    do {
        cout << "Input two strings: ";
        string str1, str2;
        cin >> str1 >> str2; //输入两个string对象,以空格或回车作为间隔结束当前string输入
        cout << (str1<=str2 ? str1 : str2)  //直接比较两个string对象
             << " is less than the other. " << "\n\n"
             << "More? Enter yes or no: "; //是否继续
        cin >> rsp;                        //输入yes或者no表达是否继续比较
    } while (!rsp.empty() && tolower(rsp[0]) == 'y'); 
    //用户回答不为空并且回答第一个字母转化为小写是y则继续比较,否则结束
	system("pause");
    return 0;
}

5.20 从标准输入中读取string对象的序列直到连续出现两个相同的单词或者所有单词都读完为止(ctrl+Z)。使用while循环依次读取一个单词,当一个单词连续两次出现时使用break终止循环。最终输出连续重复出现的那个单词或者说明没有出现连续重复单词。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string curr, pre;
	while (cin >> curr) //一次读入一个单词,直到ctrl+Z
	{
		if (curr == pre) //终止读入
			break;
		else
			pre = curr;  //继续读入
	}
	if (cin.eof())//读到文件末尾-ctrl+Z则返回true  
		cout << "no word was repeated." << endl;
	else
		cout << curr << " occurs twice in succession." << endl;
	system("pause");
    return 0;
}

5.21 修改上题,使其找到的重复单词必须以大写字母开头

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string curr, pre;
	bool no_twice = true;
	while (cin >> curr) //一次读入一个单词,直到ctrl+Z
	{
		if (isupper(curr[0]) && curr == pre) //第一个字母大写且出现重复终止读入
		{
			cout << curr << ": occurs twice in succession." << endl;
			no_twice = false;
			break;
		}
		else
			pre = curr; 
	}
	if (no_twice) //读到末尾ctrl+Z
		cout << "no word was repeated." << endl;
	system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值