C++ stl汇总

简介: C++ stl汇总

vector
https://blog.csdn.net/weixin_42172261/article/details/86604772


map
https://blog.csdn.net/weixin_42172261/article/details/86621807


set
https://blog.csdn.net/weixin_42172261/article/details/86607669


迭代器
https://blog.csdn.net/weixin_42172261/article/details/86606300


stack
https://blog.csdn.net/weixin_42172261/article/details/88924120


queue
https://blog.csdn.net/weixin_42172261/article/details/88924295


priority_queue

#include <iostream>
#include <queue> 
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main(){
   
    //从大到小排列 
    priority_queue<int, vector<int>, less<int> > q;
    srand((unsigned)time(NULL));
    for (int i=1; i<=5; i++){
   
        int t = rand() % 20;
        q.push(t);
    }
    while (!q.empty()){
   
        int t = q.top();
        q.pop();
        cout<<t<<endl;
    }
    return 0;
}
//从小到大排列
priority_queue<int, vector<int>, greater<int> > q;
#include <iostream>
#include <queue> 
#include <ctime>
#include <cstdlib>
using namespace std;
//重载小于号,a和b越大的优先级越低,越排在后面 
struct Node{
   
    int a, b;
    bool operator<(const Node& t) const{
   
        if (a!=t.a)    return a>t.a;
        else    return b>t.b;
    }
};

int main(){
   
    priority_queue<Node> q;
    srand((unsigned)time(NULL));
    for (int i=1; i<=5; i++){
   
        Node t;
        t.a = rand() % 20;
        t.b = rand() % 20;
        q.push(t);
    }
    while (!q.empty()){
   
        Node t = q.top();
        q.pop();
        cout<<t.a<<" "<<t.b<<endl;
    }
    return 0;
}
//重载小于号,a和b越小的优先级越低,越排在后面 
struct Node{
   
    int a, b;
    bool operator<(const Node& t) const{
   
        if (a!=t.a)    return a<t.a;
        else    return b<t.b;
    }
};
相关文章
|
4天前
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
6天前
|
算法 安全 Linux
|
1月前
|
设计模式 算法 Java
【c++】STL之stack和queue详解
【c++】STL之stack和queue详解
28 1
|
2月前
|
设计模式 存储 C++
【C++/STL】:stack/queue的使用及底层剖析&&双端队列&&容器适配器
【C++/STL】:stack/queue的使用及底层剖析&&双端队列&&容器适配器
43 2
|
2月前
|
编译器 C++ 容器
【C++/STL】:list容器的深度剖析及模拟实现
【C++/STL】:list容器的深度剖析及模拟实现
24 2
|
2月前
|
编译器 C语言 C++
C++ STL中list迭代器的实现
C++ STL中list迭代器的实现
C++ STL中list迭代器的实现
|
1月前
|
存储 算法 C++
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
39 0
|
1月前
|
存储 算法 数据处理
【C++】STL简介
**STL是C++标准库的关键部分,源于Alexander Stepanov的泛型编程研究。它提供了数据结构(如vector、list)和算法,是高效、通用的软件框架。STL始于惠普,后由SGI发展,现已成为C++1998标准的一部分并不断进化。它包括容器、迭代器、算法、仿函数、配接器和分配器六大组件,带来高效性、通用性和可扩展性,但也存在性能开销和学习难度。学习STL涉及理解底层数据结构、用法、实现和实践。推荐[cplusplus.com](https://cplusplus.com)作为学习资源。**
|
1月前
|
存储 算法 程序员
C++基础知识(八:STL标准库(Vectors和list))
C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), lists(链表), 和 stacks(栈)等. STL容器的提供是为了让开发者可以更高效率的去开发,同时我们应该也需要知道他们的底层实现,这样在出现错误的时候我们才知道一些原因,才可以更好的去解决问题。
|
1月前
|
算法 前端开发 C++
C++基础知识(八:STL标准库 deque )
deque在C++的STL(Standard Template Library)中是一个非常强大的容器,它的全称是“Double-Ended Queue”,即双端队列。deque结合了数组和链表的优点,提供了在两端进行高效插入和删除操作的能力,同时保持了随机访问的特性。