C++ 元素計數 count&&count_if
阿新 • • 發佈:2020-11-04
元素計數
- count(b,e,c)
- count_if(b,e,)
- 關聯容器等效成員函式(速度更快)
- set.count
- multiset.count
- map.count
- 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)));