STL学习笔记(十八) count find count_if find_if

 iterator find(pos_beg,pos_end,data) //找到相同的
 iterator find_if(pos_beg,pos_end,cond) //找到符合条件的
 count(pos_beg,pos_end,data) //统计

 count_if(pos_beg,pos_end,data,cond) //符合条件的才统计


#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
using namespace std;


//判断第一个字母是否是大写
bool init_upper(string &str)
{
	return isupper(str[0]);
}

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

int main()
{
	string a[5] = {"abc","good","day","Look","God"};
	string *p = find(a,a+5,"good"); //返回的是迭代器,但对于基本类型就是指针
	cout << (p == a+5?"没找到":"找到")<< "good" << endl;
	p = find_if(a,a+5,init_upper);  
	if(p == a+5) cout << "没有找到大写字母开头的字符串" << endl;
	else cout << "找到了" << *p << endl;
	
	
	cout << count_if(a,a+5,init_upper) << endl;
	cout << count_if(a,a+5,has_o) << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值