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. Informatica抽取SQL Server数据库乱码

    1.首先确认数据库的关系连接所使用的代码页,是否一致 2.如果上述方法不行,在Designer中更改数据类型,将string类型改为nstring类型,中文就没有乱码了 3.SQL Server数据库 ...

  2. linux shell 搭建本地yum 源,通过IOS镜像 Centeros6,7还有redhat

    Centeros: 准备工作: 将系统镜像放到 /opt 下 脚本: #!/bin/bash mkdir /mnt/cdrom mount -o loop /opt/*.ios;# 此处改为你的系统镜 ...

  3. 4 SQL 数据更新

    4 数据更新 4-1 数据的插入(INSERT语句的使用方法) 通过create table语句创建出来的表,可以将其比作一个空空如也的箱子.只有把数据装入到这个箱子后,它才能称为数据库.用来装入数据 ...

  4. JSTL标签判断list是否为空

    jsp页面判断获得action传的list的是否为空或者list.size的长度,就可以用fn这个标签: <c:if test="${list== null || fn:length( ...

  5. xtu read problem training 2 B - In 7-bit

    In 7-bit Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 3 ...

  6. zju 3209 dancing links 求取最小行数

    题目可以将每一个格子都看做是一列,每一个矩形作为1行,将所有格子进行标号,在当前矩形中的格子对应行的标号为列,将这个点加入到十字链表中 最后用dlx求解精确覆盖即可,dance()过程中记得剪枝 #i ...

  7. LibreOJ2302 - 「NOI2017」整数

    Portal Description 有一个整数\(x=0\),对其进行\(n(n\leq10^6)\)次操作: 给出\(a(|a|\leq10^9),b(b\leq30n)\),将\(x\)加上\( ...

  8. BZOJ4552 - [TJOI2016]排序

    Portal Description 给出一个\(1..n(n\leq10^5)\)的排列,进行\(m(m\leq10^5)\)次操作: 升序排列\([L,R]\)中的数. 降序排列\([L,R]\) ...

  9. 【PD】PowerDesigner生成数据字典

    1.首先说明我使用的环境 --------------------------------第一种:不按模板导出导出数据字典----------------------------- 2.打开PDM模型 ...

  10. xhprof安装&&使用[转载]

    编译安装 wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2/extensio ...