C++25——STL

  • a pair class:表达两个东西之间的关系

  • 容器

    • vector:可扩展的数字,在一头固定增长

    • deque:可扩展的数字,往两头增长

    • list:内部是双向链表

    • set:集合,没有重复的,无序的

    • map:映射(ket,value)

  • 基本函数的模板(排序、搜索等)

以上所有的内容都在std里(使用using namespace std;

所有标识符都是小写

#include<iostream>
#include<vector>
using namespace std;

int main(){
    vector<int> x;	//可以不用设置初始vector大小
    for(int a=0;a<1000;a++)
        x.push_back(a);
    vector<int>::iterator p;
    for(p=x.begin();p<x.end();p++)
        cout<<*p<<" ";
    return 0;
}

STL:模板&重载

 

比较符号是比较两个vector,而不是仅仅比较两个vector里的内容

V.swap(v2)是交换两个vector,而不是仅仅交换两个vector里的内容

V.at(index)V[index]都可以访问对应索引的值

V.push_back(e):在V中存放e

V.pop_back():取出V中最后存入的数

v.insert(pos,e):在pos处插入e

V.erase(pos):删掉pos处的数

V.find(first,last,item):在first和last之间找出item,并且给出索引值

 list可以两边存放数值。

#include<iostream>
using namespace std;
#include<list>
#include<string>
int main(){
    list<string> s;
    s.push_back("Hello");
    s.push_back("world");
    s.push_front("tide");
    s.push_front("cirmson");
    s.push_front("alabama");
    lis<string>::iterator p;
    for(p=s.begin();p!=s.end();p++)	//此处不可以使用p<s.end()
        cout<<*p<<" ";
    cout<<endl;
}
#include<map>
#include<string>
map<string,float> price;
price["snapple"]=0.75;
price["coke"]=0.50;
string item;
double total=0;
while(cin>>item)
    total+=price[item];

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,priority_queue是一个容器适配器,用于实现优先级队列。默认情况下,priority_queue是一个大顶堆,也就是说,优先级最高的元素会被放在队列的前面。但是,我们也可以通过自定义排序规则来创建小顶堆。 在C++中,可以通过指定第三个模板参数Compare来自定义排序规则。比如,可以使用std::greater<T>来创建小顶堆,其中T是存储在priority_queue中的元素类型。例如,可以这样定义一个小顶堆的priority_queue: ```cpp std::priority_queue<int, std::vector<int>, std::greater<int>> pq; ``` 这样定义的priority_queue会根据元素的值从小到大进行排序,优先级最高的元素会被放在队列的前面。 另外,priority_queue还提供了一些成员函数来操作队列,比如empty()、size()、top()、push()、pop()等。你可以使用这些成员函数来判断队列是否为空、获取队列的大小、访问队列的第一个元素、向队列中插入元素以及移除队列中的元素。 总结起来,如果你想创建一个小顶堆的priority_queue,可以通过指定std::greater<T>作为第三个模板参数来实现。然后,你可以使用priority_queue提供的成员函数来操作队列。 #### 引用[.reference_title] - *1* [C++ STL——Queue容器、priority_queue](https://blog.csdn.net/LiuXF93/article/details/121119026)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [c++priority_queue详解](https://blog.csdn.net/qq_43679351/article/details/124825229)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值