c++ 容器中元素计数
#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++ 容器中元素计数的更多相关文章
- C++11新特性应用--介绍几个新增的便利算法(不更改容器中元素顺序的算法)
总所周知.C++ STL中有个头文件,名为algorithm.即算法的意思. The header<algorithm>defines a collection of functions ...
- 遍历并批量删除容器中元素出现ConcurrentModificationException原因及处置
在以下四种遍历过程中,前两种会抛出ConcurrentModificationException,而后两种方法是正确的. Department类: package com.sitinspring; i ...
- C++(五十) — 容器中元素满足的条件
容器中的内容必须满足三个条件: (1)无参构造函数 (2)拷贝构造函数 (3)重载 = 运算符 #define _CRT_SECURE_NO_WARNINGS #include <iostrea ...
- c++ 使用模板按类型统计stl多维容器中元素的数量
struct ItemCounter{template<typename T1, typename T2, typename = typename std::enable_if<!std: ...
- 容器中元素的去重——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 ...
- c++随机排序容器中的元素
在各种程序语言中都提供了将容器元素随机排序的shuffle方法,c++也不例外. 不过c++将shuffle放在了<algorithm>中而不是像其他语言一样在random里,同时c++1 ...
- vector容器中添加和删除元素
添加元素: 方法一: insert() 插入元素到Vector中 iterator insert( iterator loc, const TYPE &val ); //在指定位置loc前插入 ...
- 删除STL容器中的元素
有关stl容器删除元素的问题,错误的代码如下: std::vector<struct> mFriendList; ... std::vector<struct>::iterat ...
- ca12a_c++顺序容器的操作5_访问容器中的数据元素
ca12a_c++顺序容器的操作5_访问容器中的数据元素访问元素:c.back()..显示最后一个数据c.front() ..显示第一个数据c[n],n就是下标,适合vector与dequec.at( ...
随机推荐
- word生成目录的pdf
在很多情况下,需要将Word转换为带目录书签的PDF,方便pdf阅读,所以可以使用word自带的pdf转换,在转换时设置相关即可 注意:待转换Word中应该有目录,可以用Word中的标题来自动生成目录 ...
- Bind Mounts and File System Mount Order
When you use the bind option of the mount command, you must be sure that the file systems are m ...
- NoNodeAvailableException[None of the configured nodes are available:
elasticSearch的错误 NoNodeAvailableException[None of the configured nodes are available: [{#transport#- ...
- Ubuntu系统---C++之Eclipse IDE 编译器安装
Ubuntu系统---C++之Eclipse IDE 编译器安装 Eclipse是一个基于Java的.开放源码的.可扩展的应用开发平台,它为编程人员提供了一流的Java集成开发环境(Integrate ...
- sleep() 和 wait() 有什么区别?(未完成)
sleep() 和 wait() 有什么区别?(未完成)
- RedHat Enterprise Linux 5 配置Samba服务器
1.修改samba的配置文件 # gedit /etc/samba/smb.conf 在/etc/samba/smb.conf配置文件中找到Share Definitions模块添加以下代码: [ro ...
- Java基础 @org.junit.Test-单元测试方法 + 操纵Collection和Map的工具类 : Collections 的sort/binarySearch/max/min等静态方法
单元测试代码: ( 在IDEA中先输入'@Test '然后根据提示进行自动修订即可!!运行时直接运行即可! 若有多个单元测试块的时候,直接把鼠标放在哪里就自动在哪里运行那个单元块) import ...
- linux目录太长怎么办?分享一点小技巧
在linux使用cd的时候,可能会遇到目录比较深的时候,这个时候总是cd一个很长的目录会很麻烦,那有没有什么比较方便的方法呢? 若是在两个目录中来回切换,这个时候可以使用cd - 这个命令,可以完成在 ...
- jpa 如果返回java对象,会自动更新对象值(坑!!)
//上一段示例代码List<Member> memberList = memberDao.findByLoginNameIn(names);for (Member m : memberLi ...
- axios并行请求
有些操作需要在几个异步请求都完成之后再执行,虽然一个Ajax可以放到另一个Ajax完成的回调里面,但这样很容易导致回调地狱,且代码也极其不美观. 幸运的是axios提供了并行请求的方法, 使用方法: ...