c++ STL之 find和count(权哥)

/*
iterator find(pos_beg, pos_end, data)
iterator find_if(pos_beg, pos_end, cond), bool cond(element)
int count(...), int count_if(...)
*/
#include <algorithm>
#include <iostream>
using namespace std;
#include <cctype>
#include <string>
bool init_upper(const string& str)
{return isupper(str[0]);}

bool has_o(const string& str)
{return str.find_first_of("oO")!=string::npos;}

int main()
{
	string a[7]={"abc","good","Day","look","God","OK","bye"};
	string* p = find(a,a+7,"look");
	cout << (p==a+7?"没找到":"找到") << "look" << endl;
	p = find(a,a+7,"book");
	cout << (p==a+7?"没找到":"找到") << "book" << endl;
	p = find_if(a,a+7,init_upper);
	if(p==a+7) cout << "没找到大写字母开头的字符串" << endl;
	else cout << "找到" << *p << endl;
	cout << count_if(a,a+7,init_upper) << endl;
	cout << count_if(a,a+7,has_o) << endl;
}

/*
输出:
找到look
没找到book
找到Day
3
4
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值