C++ Primer(第五版) 9.5.3节练习

9.47

#include <iostream>
#include <string>

using namespace std;

void find_char(string &s, const string &chars)
{
	string::size_type pos = 0;

	while ((pos = s.find_first_of(chars, pos)) != string::npos) {
		cout << "index: " << pos << ", element: " << s[pos] << endl;
		++pos;
	}
}

void find_not_char(string &s, const string &chars)
{
	string::size_type pos = 0;

	while ((pos = s.find_first_not_of(chars, pos)) != string::npos) {
		cout << "index: " << pos << ", element: " << s[pos] << endl;
		++pos;
	}
}

int main()
{
	string s("ab2c3d7R4E6");

	find_char(s, "0123456789");
	find_not_char(s, "0123456789");	
	
	return 0;
}

9.48    返回npos。s.find(args),args是整体;s.find_frist_of(args), args是字符集合。

9.49

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

string find_word(const vector<string> &svec) 
{
	string chars("bdfghijklpqty");
	string ret;

	for (auto s : svec) {
		auto pos = s.find_first_of(chars);
		if (pos == string::npos && ret.size() < s.size())
			ret = s;			
	}
	
	return ret;
}

int main(int argc, char *argv[])
{
	ifstream input(argv[1]);
	vector<string> text;

	string line, word;
	while (getline(input, line)) {
		istringstream in(line);
		while (in >> word)
			text.push_back(word);
	}	
	
	auto result = find_word(text);
	if (!result.size())
		cout << "The word doesn't exsit!" << endl;
	else
		cout << result << endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值