#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include<set>

using namespace std;

//一元谓词
bool isEven(int elementParam)
{
  if (elementParam%2==0)
  {
    return true;
  }
  return false;
}

int main()
{
  vector<int> vec1;
  for (int k=0;k<10;k++)
  {
    vec1.push_back(k);
  }

  vec1.push_back(4);
  vec1.push_back(4);

  for (vector<int>::iterator iter=vec1.begin();iter !=vec1.end();++iter)
  {
    cout << *iter << " ";
  }
  cout << endl;

  int num = count(vec1.begin(),vec1.end(),4);
  cout << "" << num << endl;

  //偶数的个数
  int num2 = count_if(vec1.begin(),vec1.end(),isEven);
  cout << "" << num2 << endl;

  //bind2nd:函数适配器
  //greater:函数对象 ,有两个参数,用于判断第一个参数是否大于第二个参数
  int num3 = count_if(vec1.begin(),vec1.end(),bind2nd(greater<int>(),6));
  cout << "大于4的数:"<<num3<< endl;

  int num4 = count_if(vec1.begin(),vec1.end(),bind2nd(modulus<int>(),2));
  cout << "偶数个数:" << num4 << endl;

  multiset<int> multiset1;
  for (int k=0;k<10;k++)
  {
    multiset1.insert(multiset1.begin(),k);
  }

  multiset1.insert(3);
  multiset1.insert(6);
  multiset1.insert(6);

  multiset<int>::iterator multiset_iter1;
  for (multiset_iter1 = multiset1.begin();multiset_iter1 !=multiset1.end();++multiset_iter1)
  {
    cout << *multiset_iter1 << " ";
  }
  cout << endl;

  int num5 = count(multiset1.begin(),multiset1.end(),3);
  cout << "count:" << num5 << endl;

  //效率高
  int num6 = multiset1.count(6);
  cout << "count:" << num6 << endl;

  system("pause");
  return 0;
}

===================================================

0 1 2 3 4 5 6 7 8 9 4 4
3
7
大于4的数:3
偶数个数:5
0 1 2 3 3 4 5 6 6 6 7 8 9
count:2
count:3
请按任意键继续. . .

c++ 容器中元素计数的更多相关文章

  1. C++11新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)

    总所周知.C++ STL中有个头文件,名为algorithm.即算法的意思. The header<algorithm>defines a collection of functions ...

  2. 遍历并批量删除容器中元素出现ConcurrentModificationException原因及处置

    在以下四种遍历过程中,前两种会抛出ConcurrentModificationException,而后两种方法是正确的. Department类: package com.sitinspring; i ...

  3. C++(五十) — 容器中元素满足的条件

    容器中的内容必须满足三个条件: (1)无参构造函数 (2)拷贝构造函数 (3)重载 = 运算符 #define _CRT_SECURE_NO_WARNINGS #include <iostrea ...

  4. c++ 使用模板按类型统计stl多维容器中元素的数量

    struct ItemCounter{template<typename T1, typename T2, typename = typename std::enable_if<!std: ...

  5. 容器中元素的去重——ans.erase(unique(ans.begin(),ans.end()),ans.end());

    啊,原来unique()函数,只是处理的连续的元素,比如 -1 -1 -1 1 2 -1 2 就处理成了 -1 1 2 -1 2 -1 -1并且返回5,之后eraser(5,7)剩下了 -1 1 2 ...

  6. c++随机排序容器中的元素

    在各种程序语言中都提供了将容器元素随机排序的shuffle方法,c++也不例外. 不过c++将shuffle放在了<algorithm>中而不是像其他语言一样在random里,同时c++1 ...

  7. vector容器中添加和删除元素

    添加元素: 方法一: insert() 插入元素到Vector中 iterator insert( iterator loc, const TYPE &val ); //在指定位置loc前插入 ...

  8. 删除STL容器中的元素

    有关stl容器删除元素的问题,错误的代码如下: std::vector<struct> mFriendList; ... std::vector<struct>::iterat ...

  9. ca12a_c++顺序容器的操作5_访问容器中的数据元素

    ca12a_c++顺序容器的操作5_访问容器中的数据元素访问元素:c.back()..显示最后一个数据c.front() ..显示第一个数据c[n],n就是下标,适合vector与dequec.at( ...

随机推荐

  1. iptables详解说明

    Iptabels是与Linux内核集成的包过滤防火墙系统,几乎所有的linux发行版本都会包含Iptables的功能.如果 Linux 系统连接到因特网或 LAN.服务器或连接 LAN 和因特网的代理 ...

  2. 早上好,我是 Istio 1.1

    1性能增强 虽然Istio1.0的目标是生产可用,但从去年7月份发布以来,在性能和稳定性上并不能让用户满意.社区的Performance and Scalability工作组在Istio v1.1中做 ...

  3. 极光推送出现 超时问题:Connect timeout. Please retry later. Error:7

    检查之后均没有什么太大的问题, 最后发现出现77这种错误码,有一种可能就是系统的ca包没有更新 包名为 ca-certificates 使用命令 yum install ca-certificates ...

  4. Week08_day01 (Hive开窗函数 row_number()的使用 (求出所有薪水前两名的部门))

    数据准备: 7369,SMITH,CLERK,7902,1980-12-17,800,null,20 7499,ALLEN,SALESMAN,7698,1981-02-20,1600,300,30 7 ...

  5. 3.使用webpack配置文件webpack.confg.js配置打包文件的入口和出口

    在项目根目录下新建webpack.config.js文件 webpack.config.js文件配置如下: // Node的路径操作使用的是path模块 const path=require('pat ...

  6. hibernate步骤和配置

    1.引入hibernate的jar包和数据库驱动包 2.src添加hibernate.cfg.xml(hibernate配置文件) 3.数据库编写pojo public class Test { pu ...

  7. BZOJ 2039 / Luogu P1791 [2009国家集训队]employ人员雇佣 (最小割)

    题面 BZOJ传送门 Luogu传送门 分析 考虑如何最小割建图,因为这仍然是二元关系,我们可以通过解方程来确定怎么建图,具体参考论文 <<浅析一类最小割问题 湖南师大附中 彭天翼> ...

  8. Github:VS使用GitHub要点

    1.VS打开[扩展或更新]安装插件[Github Extension for VisualStudio] 2.在团队资源管理中可以Clone线上已经有的库到本地,或者可以新建库同步到线上 3.同步设置 ...

  9. HDU 6134 Battlestation Operational | 2017 Multi-University Training Contest 8

    破结论没听说过,上式推导到第三步的时候有了O(nlogn) 的做法(枚举倍数+1最后前缀和),并且这种做法可以直接应用到向上取整的计算中,详见forever97 但由于d(n)是积性函数,故可O(n) ...

  10. Ubuntu16安装fabric1.4.4环境

    安装流程依照官网地址 https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html 如果需要安装最新的版本,可 ...