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( ...
随机推荐
- CentOS 7 配置VNCServer
因为一直在用xmanager ,所以CentOS 7 上没过VNCSserver了,最近安装Oracle19C ,xmanager 总是卡死,所以配置VNC. 发现仅仅yum install -y t ...
- Qemu: User mode emulation and Full system emulation
转载: https://wiki.edubuntu.org/UbuntuDevelopment/Ports QEMU QEMU is a processor emulator and supports ...
- Nginx系列1.1:ubuntu16.04编译nginx-rtmp流媒体服务器
1.下载nginx和nginx-rtmp-module nginx官网:nginx.org tar.gz文件 解压缩命令: wget https://nginx.org/download/nginx- ...
- solr8.2 环境搭建 配置中文分词器 ik-analyzer-solr8 详细步骤
一.下载安装Apache Solr 8.2.0 下载地址:http://lucene.apache.org/solr/downloads.html 因为是部署部署在windows系统上,所以下载zip ...
- 【转】Java8中list转map方法总结
https://blog.csdn.net/zlj1217/article/details/81611834 背景在最近的工作开发之中,慢慢习惯了很多Java8中的Stream的用法,很方便而且也可以 ...
- mongodb的基本操作之数据创建索引
在数据量较少时,不使用索引,查询是很快的,但是在数据量较大时,查询将会变得非常缓慢,在mongodb中 查看索引 > db.test_collection.getIndexes() [ { &q ...
- linux加载字体
将解压后的文件夹cp到/usr/share/fonts目录下,然后cd到/usr/share/fonts/ziti目录下执行:mkfontscalemkfontdirfc-cache 在linux,把 ...
- spring replaced method 注入
replaced method注入是spring动态改变bean里方法的实现.需要改变的方法,使用spring内原有其他类(需要继承接口org.springframework.beans ...
- java 常用算法和一些题目
选择排序,复杂度O(n²) package com.example.demo; import org.junit.Test; /** * 选择排序 * @author zhzh.yin * */ pu ...
- Selenium(五)鼠标和键盘事件
1.模拟鼠标找到大分类下的子分类.以网易严选为例. 如果直接找到 坚果炒货 这个元素,然后点击它来实现跳转,是会报错的. 模拟鼠标停留--点击行为: 页面已成功跳转 2.键盘事件 模拟搜索操作: ...