C++ 元素计数 count&&count_if

元素计数

  • count(b,e,c)
  • count_if(b,e,)
  • 关联容器等效成员函数(速度更快)
    1. set.count
    2. multiset.count
    3. map.count
    4. multimap.count

预定义的函数对象

/* #include <functional> */
equal_to<type>()		//等于
not_equal_to<type>()	//不等于
less<type>()			//小于
greater<type>()			//大于
less_equal<type>()		//小于等于
greater_equal<type>()	//大于等于
modulus<type>()         //取模
plus<Types>()			//加法
minus<Types>()			//减法
multiplies<Types>()		//乘法
divides<Tpye>()			//除法
negate<Type>()			//取反

预定义的函数适配器

/* #include <functional> */
bind1st(op,value)		//将数值绑定到二元函数的第一个参数,适配成一元函数
bind2nd(op,value)		//将数值绑定到二元函数的第二个参数,适配成一元函数
not1(op) 				//生成一元函数的逻辑反函数
not2(op)				//生成二元函数的逻辑反函数
mem_fun_ref(op)
mem_fun(op)
ptr_fun(op)

count()

count(ivec.begin(),ivec.end(),4);
/*multiset成员函数*/ iset.count(4)

count_if()

/* 一元谓词
bool isEven(int elem){
	return elem%2==0;
}
*/
count_if(ivec.begin(),ivec.end(),isEven);	
count_if(ivec.begin(),ivec.end(),not1(bind2nd(modulus<int>(),2)));
posted @ 2020-11-04 17:39  予之路  阅读(276)  评论(0编辑  收藏  举报