STL常用
nth_element(first,nth,last)
first,last 第一个和最后一个迭代器,也可以直接用数组的位置。
将第n个大的元素放到nth位置上,左边元素都小于它,右边元素都大于它.
// nth_element example
#include <iostream> // std::cout
#include <algorithm> // std::nth_element, std::random_shuffle
#include <vector> // std::vector
bool myfunction (int i,int j) { return (i<j); }
int main () {
std::vector<int> myvector;
// set some values:
for (int i=1; i<10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
std::random_shuffle (myvector.begin(), myvector.end());
// using default comparison (operator <):
std::nth_element (myvector.begin(), myvector.begin()+5, myvector.end());
// using function as comp
std::nth_element (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);
// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
//Possible output:
//myvector contains: 3 1 4 2 5 6 9 7 8
std::fill std::fill_n
#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
int main()
{
std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::fill_n(v.begin(), 5, -1);
std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
//Output:
//-1 -1 -1 -1 -1 5 6 7 8 9
std::fill(v.begin(), v.end(), -1);
for (auto elem : v) {
std::cout << elem << " ";
}
std::cout << "\n";
//Output:
//-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
}
std::priority_queue
priority_queue 容器适配器定义了一个元素有序排列的队列。默认队列头部的元素优先级最高。
priority_queue 模板有 3 个参数,其中两个有默认的参数;第一个参数是存储对象的类型,第二个参数是存储元素的底层容器,第三个参数是函数对象,它定义了一个用来决定元素顺序的断言。因此模板类型是:
template <typename T, typename Container=std::vector<T>, typename Compare=std::less<T>> class priority_queue
对 priority_queue 进行操作有一些限制:
- push(const T& obj):将obj的副本放到容器的适当位置,这通常会包含一个排序操作。
- push(T&& obj):将obj放到容器的适当位置,这通常会包含一个排序操作。
- emplace(T constructor a rgs...):通过调用传入参数的构造函数,在序列的适当位置构造一个T对象。为了维持优先顺序,通常需要一个排序操作。
- top():返回优先级队列中第一个元素的引用。
- pop():移除第一个元素。
- size():返回队列中元素的个数。
- empty():如果队列为空的话,返回true。
- swap(priority_queue& other):和参数的元素进行交换,所包含对象的类型必须相同。
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int main()
{
priority_queue <int, vector<int>, greater<int>> pq1;
priority_queue <int, vector<int>, less<int>> pq2;
for (int i=0; i<8; ++i) {
pq1.push(i);
pq2.push(i);
}
cout << "pq1: ";
while (!pq1.empty()) {
cout << pq1.top() << " ";
pq1.pop();
}
cout << endl;
cout << "pq2: ";
while (!pq2.empty()) {
cout << pq2.top() << " ";
pq2.pop();
}
cout <<endl;
return 0;
}
\\pq1: 0 1 2 3 4 5 6 7
\\pq2: 7 6 5 4 3 2 1 0
STL常用的更多相关文章
- C++ STL 常用算术和生成算法
C++ STL 常用算术和生成算法 accumulate() accumulate: 对指定范围内的元素求和,然后结果再加上一个由val指定的初始值. #include<numeric> ...
- C++ STL 常用拷贝和替换算法
C++ STL 常用拷贝和替换算法 copy() 复制 vector<int> vecIntA; vecIntA.push_back(1); vecIntA.push_back(3); v ...
- C++ STL 常用排序算法
C++ STL 常用排序算法 merge() 以下是排序和通用算法:提供元素排序策略 merge: 合并两个有序序列,存放到另一个序列. 例如: vecIntA,vecIntB,vecIntC是用ve ...
- C++ STL 常用查找算法
C++ STL 常用查找算法 adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. ...
- C++ STL 常用遍历算法
C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...
- STL常用结构与方法简明总结
C++常用的数据结构 序列式容器 vector(向量.有序数列),list(双向链表),deque(双端队列) 适配器容器 stack(栈),queue(队列) 关联式容器 map(映射.键值对二叉树 ...
- STL常用序列容器
这里简要的记述一下STL常用容器的实现原理,要点等内容. vector vector是比较常用的stl容器,用法与数组是非类似,其内部实现是连续空间分配,与数组的不同之处在于可弹性增加空间,而arra ...
- C++STL 常用 函数 用法
学完c++快一年了,感觉很有遗憾,因为一直没有感觉到c++的强大之处,当时最大的感觉就是这个东西的输入输出比C语言要简单好写. 后来我发现了qt,opencv,opengl,原来,c++好玩的狠. 在 ...
- C++中STL常用容器的优点和缺点
我们常用到的STL容器有vector.list.deque.map.multimap.set和multiset,它们究竟有何区别,各自的优缺点是什么,为了更好的扬长避短,提高程序性能,在使用之前需要我 ...
- C++ STL常用容器浅析
首先要理解什么是容器,在C++中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对象的指针,这种对象类型就叫做容器.简单来说 容器就是包含其他类的对象们的对象,当然这种(容器) ...
随机推荐
- apache不能解析php之解决办法
记录一下Ubuntu16.04下的apache服务器不能解析php的问题,如图所示: 对于这个Ubuntu16.04最直接最快速的解决办法就是执行如下命令: sudo apt-get install ...
- 深度学习面试题12:LeNet(手写数字识别)
目录 神经网络的卷积.池化.拉伸 LeNet网络结构 LeNet在MNIST数据集上应用 参考资料 LeNet是卷积神经网络的祖师爷LeCun在1998年提出,用于解决手写数字识别的视觉任务.自那时起 ...
- arcpy SearchCursor sql_clause
import arcpy fc = 'c:/data/base.gdb/well' fields = ['WELL_ID', 'WELL_TYPE'] # Use ORDER BY sql claus ...
- Qualcomm Audio HAL 音频通路设置【转】
本文转载自:https://blog.csdn.net/azloong/article/details/79383323 1. 音频框图概述| Front End PCMs | SoC DSP | B ...
- 沃顿商学院的MBA课程
沃顿商学院的MBA课程,分为必修课和选修课两部分 (一)必修课: 1.领导力:团队合作和领导力的基础 2.营销学:营销管理 3.微观经济学:微观经济基础 4.经济学:管理经济学的高级话题 5.统计学: ...
- The Art of Picking Intel Registers Intel寄存器的艺术
https://www.swansontec.com/sregisters.html I wrote this article for an online magazine called Scene ...
- hue 登录访问不了HDFS webhdfs_url 调整
Cannot access: /. Note: you are a Hue admin but not a HDFS superuser, "hdfs" or part of HD ...
- markdown如何在表格内换行?
答:使用<br>即可在表格内换行
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解
笔记 3.SpringBoot2.x整合redis实战讲解 简介:使用springboot-starter整合reids实战 1.官网:https://docs.spring.io/spring-bo ...
- 编写第一个dart程序hello dart
/* 入口方法的两种定义方式 main(){ print('hello dart'); } */ ///这也是一个注释 //表示main方法没有返回值 void main(){ print('hell ...