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. ES5的数组方法

    参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array Array.prot ...

  2. python 连接docker

    发现15年写过的一小段,先mark一下,后续玩儿起来 import docker c = docker.Client(base_url='unix://var/run/docker.sock',ver ...

  3. iframe的操作switch_to_frame使用方法.

    一.frame和iframe区别 Frame与Iframe两者可以实现的功能基本相同,不过Iframe比Frame具有更多的灵活性. frame是整个页面的框架,iframe是内嵌的网页元素,也可以说 ...

  4. 02 requests模块

    requests模块 requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,requests会比urllib更加方便,可以节约我们大 ...

  5. 使用pymysql 操作MySQL数据库

    安装 pip install pymysql 注:连接前要有可使用的账户及有权限.可操作的数据库 先来一个栗子: import pymysql # 连接database conn = pymysql. ...

  6. idea 中使用 出现 svn: E155036

    在idea中使用svn  checkout时  svn出现如上错误. 原因本地的工作副本太旧.command line进入本地工作副本的根目录,执行svn upgrade后 重启idea就可以了.

  7. HDU 3911 区间合并求最大长度的问题

    http://vjudge.net/problem/viewProblem.action?id=21557 题目大意: 每进行一次颜色改变都可以把一段区间内的黑石头变成白石头,白石头变成黑石头,最后问 ...

  8. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  9. angularjs ngRoute的使用简单例子

    很丑的小例子,刚学angularjs,写下来方面以后看. 1.例子的工程目录如下: 2.index.html代码如下: <!DOCTYPE html><html><hea ...

  10. 2018 11.2 PION模拟赛

    期望:100 + 50 + 30 = 180 实际:0 + 50 + 30 =80 期望:100   实际:0 数值有负数,边界应该设为-0x7f       此处 gg /* 期望的分:50+ */ ...