C++ Primer 学习中。。

简单记录下我的学习过程 (代码为主)

count 、 count_if

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
using namespace std; /***********************
count
count_if
关联容器的等效成员函数
set.count
multiset.count
map.count
multimap.count
************************/
/*********************** std::count:****************************************
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count ( ForwardIterator first, ForwardIterator last, const T& value );
//eg:
template <class InputIterator, class T>
ptrdiff_t count ( InputIterator first, InputIterator last, const T& value )
{
ptrdiff_t ret=0;
while (first != last) if (*first++ == value) ++ret;
return ret;
}
*******************************************************************************/ /*********************** std::count_if:****************************************
template <class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type
count_if ( ForwardIterator first, ForwardIterator last, Predicate pred );
//eg:
template <class InputIterator, class Predicate>
ptrdiff_t count_if ( InputIterator first, InputIterator last, Predicate pred )//pred 为函数or函数对象
{
ptrdiff_t ret=0;
while (first != last) if (pred(*first++)) ++ret;
return ret;
}
*******************************************************************************/ //奇数
bool IsOdd (int i)
{
return i&1;
} int main()
{
int mycount; // counting elements in array:
int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements
mycount = (int) count (myints, myints+8, 10);
cout << "10 appears " << mycount << " times.\n"; // counting elements in container:
vector<int> myvector (myints, myints+8);
mycount = (int) count (myvector.begin(), myvector.end(), 20);//有几个20
cout << "20 appears " << mycount << " times.\n"; /****************
Output:
10 appears 3 times.
20 appears 3 times.
****************/ // vector<int> myvector;
myvector.clear();
for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9 cout<<"\nmyvector: 1 2 3 4 5 6 7 8 9 \n";
// mycount = (int) count_if (myvector.begin(), myvector.end(), IsOdd);
mycount = (int) count_if (myvector.begin(), myvector.end(), bind2nd(modulus<int>(),2));//表示param1 % 2
cout << "myvector contains " << mycount << " odd values.\n";//奇数
// 假设求偶数的个数 not1,1表示一个參数取反
mycount = (int) count_if (myvector.begin(), myvector.end(), not1(bind2nd(modulus<int>(),2)));//表示!(param1 % 2)
cout << "myvector contains " << mycount << " even values.\n";//偶数
/****************
Output:
myvector contains 5 odd values.
****************/
// 函数适配器 函数对象
// bind2nd(op,value);表示绑定第二个数 param1 > 4 这里表示统计大于4的个数
mycount=count_if(myvector.begin(),myvector.end(),bind2nd(greater<int>(),4));
cout<<"有"<<mycount<<"个数大于4"<<endl;
//拓展练习 4 > param2
mycount=count_if(myvector.begin(),myvector.end(),bind1st(greater<int>(),4));
cout<<"有"<<mycount<<"个数小于4"<<endl;
// 4 < param2
mycount=count_if(myvector.begin(),myvector.end(),bind1st(less<int>(),4));
cout<<"有"<<mycount<<"个数大于4"<<endl;
// param1 >= 4
mycount=count_if(myvector.begin(),myvector.end(),bind2nd(greater_equal<int>(),4));
cout<<"有"<<mycount<<"个数大于等于4"<<endl;
// param1 <= 4
mycount=count_if(myvector.begin(),myvector.end(),bind2nd(less_equal<int>(),4));
cout<<"有"<<mycount<<"个数小于等于4"<<endl; /****关联容器****/
multiset<int> ms(myvector.begin(),myvector.end());
ms.insert(myvector.begin(),myvector.begin()+6);
ms.insert(myvector.begin(),myvector.begin()+4);
ms.insert(myvector.begin(),myvector.begin()+2);
ms.insert(1); multiset<int>::iterator ims=ms.begin();
while(ims!=ms.end()){
cout<<*ims++<<" ";
}cout<<endl; //两种方法求1的个数
int cnt=count(ms.begin(),ms.end(),1);//全部容器适用可是比較慢些
cout<<"multiset里有"<<cnt<<"个1."<<endl; cnt=ms.count(1);//关联容器专享 set已经排序能够高速计数
cout<<"multiset里有"<<cnt<<"个1."<<endl; return 0;
}

STL_算法_元素计数(count、count_if)的更多相关文章

  1. cb26a_c++_STL_算法_元素计数

    cb26a_c++_STL_算法_元素计数所有容器都可以使用countcount_if关联容器的等效成员函数,容器自己的成员函数速度较快1.set.count2.multiset.count3.map ...

  2. STL_算法_依据第n个元素排序(nth_element)

    C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) //全部容器适用 nth_element(b,n,e) nth_element(b,n,e,p) 对照:partition() ...

  3. STL_算法_对全部元素排序(sort、stable_sort)

    C++ Primer 学习中. . .   简单记录下我的学习过程 (代码为主) //大部分容器适用.不适用于list容器 sort(b,e) sort(b,e,p) stable_sort(b,e) ...

  4. STL_算法_查找算法(lower_bound、upper_bound、equal_range)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的 ...

  5. STL_算法_中使用的函数对象

    写在前面: STL算法中的 函数对象的功能: (1).都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系, (2).返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往 ...

  6. STL_算法_查找算法(find、find_if)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...

  7. STL_算法_逆转(reverse,reverse_copy)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 reverse(b,e)        //逆转区间数据 reverse_copy(b,e,b2) /** ...

  8. STL_算法_查找算法(binary_search、includes)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n)))     已序区间查找算法 binary_search             //二分查 ...

  9. STL_算法_局部排序(partial_sort、partial_sort_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...

随机推荐

  1. debian 添加永久环境变量方法

    添加临时环境变量方法: export PATH=$PATH:/usr/local/....(你的环境变量路径) 永久添加环境变量,步骤如下: #在~/.bashrc文件末尾添加如下: PATH = $ ...

  2. POJ 3660 Cow Contest(求图的传递性)

    题意: 给定n头牛, 然后有m个比较, 求出有多少头牛能确定自己的排名. 分析: 假设有一头牛a, 有ki头牛强于自己, kj头牛弱于自己, ki + kj == n-1时, 那么这头牛的排名就确定了 ...

  3. GitLab 安装和配置

    一.GitLab 简介 GitLab 是一个利用Ruby on Rails 开发的开源版本控制系统,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目. 它拥有与GitHub类 ...

  4. NYOJ 237 游戏高手的烦恼

    游戏高手的烦恼时间限制:1000 ms | 内存限制:65535 KB难度:5描述有一位传说级游戏高手,在闲暇时间里玩起了一个小游戏,游戏中,一个n*n的方块形区域里有许多敌人,玩家可以使用炸弹炸掉某 ...

  5. Halloween Costumes(区间DP)

    Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning t ...

  6. 什么是Istio

    本文主要是对Istio Prelim 1.0(https://preliminary.istio.io/docs/)的翻译 Istio:一种开放式平台,用于连接,管理和保护微服务. Istio提供了一 ...

  7. POJ 1509 循环同构的最小表示法

    题目大意: 给定一个字符串,可以把一段尾部接到头部,这样找到一个最小的字符串 方案一: 利用循环同构中找最小表示的方法来解决 论文参考http://wenku.baidu.com/view/438ca ...

  8. hdu 2126背包问题

    /*有n件物品,旅客一共有m块大洋.第一个问题,旅客最多可以买多少件物品?请注意,这里是多少件,不是价值最大.所以这个非常好求,将所有的物品按照价值排序,先买便宜的,再买贵的.贪心的思想.这个地方有些 ...

  9. linux rdesktop远程Win7老是提示密码错误问题解决

    最近使用rdesktop远程Win7老是提示密码错误,输了N次,无比确认密码是正确的. 在Win7系统本身登录也是正常的. 但rdesktop远程就是报密码错误. 开始怀疑更新了最新版本问题,但是使用 ...

  10. HDU 6311 最少路径覆盖边集 欧拉路径

    Cover Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...