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++中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对象的指针,这种对象类型就叫做容器.简单来说 容器就是包含其他类的对象们的对象,当然这种(容器) ...
随机推荐
- opendir,readdir,closedir
结构体dirent: struct dirent { ino_t d_ino; //inode number off_t d_off; //offset to the next diret unsi ...
- vmware exsi安装部署
本文章参考:https://blog.csdn.net/fishinhouse/article/details/80980051 1.VMware-ESXi-6.5.0镜像下载 网盘链接:https: ...
- 2018-2019-2 网络对抗技术 20165322 Exp7 网络欺诈防范
2018-2019-2 网络对抗技术 20165322 Exp7 网络欺诈防范 目录 实验原理 实验内容与步骤 简单应用SET工具建立冒名网站 ettercap DNS spoof 结合应用两种技术, ...
- BurpSuite intuder里保存所有网页的特定内容:以bugku的cookies欺骗为例题
这题里想读取index.php只能一行一行的读,通过控制line参数的值.如下: 正常的writeup都是写个爬虫,但我觉得burp肯定有自带这种功能,不用重造轮子.经过学习后发现以下步骤可以实现. ...
- 【深度学习】关于Adam
版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_31866177/articl ...
- Are query string keys case sensitive?
Are query string keys case sensitive? @gbjbaanb's answer is incorrect: The RFCs only specify the all ...
- springboot整合vue实现上传下载文件
https://blog.csdn.net/yhhyhhyhhyhh/article/details/89888953 文章目录 springboot整合vue实现上传下载文件 1上传下载文件api文 ...
- JVM 扩展类加载器1
1.创建类 public class MyTest19 { public static void main(String[] args) throws Exception { System.out.p ...
- 程序员体验AI换脸就不要用ZAO了,详解Github周冠军项目Faceswap的变脸攻略
本文链接:https://blog.csdn.net/BEYONDMA/article/details/100594136 上个月笔者曾在<银行家杂志>发文传统银行如何引领开放 ...
- You are using the runtime-only build of Vue where the template compiler is not available. Either pre
在升级脚手架到vue-cli3.0版本的时候出现了这个报错: [Vue warn]: You are using the runtime-only build of Vue where the tem ...