網(wǎng)站首頁 編程語言 正文
sort排序
針對(duì)含有迭代器的容器,可以用#include<algorithm>
中的sort函數(shù)進(jìn)行排序。
默認(rèn)排序是從小到大,可以自己寫仿函數(shù),也可以用greater<int>()
或者less<int>()
。
#include <vector> #include <algorithm> #include <iostream> using namespace std; bool compfunc(const int &a, const int &b) { return a > b; } struct compstru { bool operator()(int a, int b) { return a > b; } }; int main() { vector<int> myVec1 = {1, 4, 9, 2}; sort(myVec1.begin(), myVec1.end(), compstru()); sort(myVec1.begin(), myVec1.end(), compfunc); sort(myVec1.begin(), myVec1.end()); sort(myVec1.begin(), myVec1.end(), less<int>()); sort(myVec1.begin(), myVec1.end(), greater<int>()); }
vector
void printVec(const vector<int> &vec) { for (auto i : vec) { cout << i << " "; } cout << endl; } int main() { vector<int> myVec1 = {1, 4, 9, 2}; reverse(myVec1.begin(), myVec1.end()); // 反轉(zhuǎn)vector printVec(myVec1); if(find(myVec1.begin(), myVec1.end(), 4)!=myVec1.end()){ //查找4是不是在vector里面 cout<<"找到了"<<endl; } int row = 5; int col = 10; vector<vector<int>> myVec2(row, vector<int>(col, 0)); // 初始化全0的二維數(shù)組 myVec1.push_back(10); myVec1.pop_back(); }
map
紅黑樹實(shí)現(xiàn)是有序容器,按照key值從小到大排序,插入pair<type1,type2>(data1,data2)
void printMap(const map<int, char> &myMap) { for (auto it : myMap) { cout << it.second << " "; } cout << endl; } int main() { map<int, char> myMap = {{3, 'c'}, {2, 'b'}, {1, 'a'}}; printMap(myMap); // a b c myMap.insert({4, 'd'}); printMap(myMap); // a b c d myMap.insert(pair<int, char>(0, 'e')); // e a b c d printMap(myMap); myMap[6] = 'g'; //有覆蓋數(shù)據(jù)的危險(xiǎn) printMap(myMap); // e a b c d g cout << myMap.count(3) << endl; // map的查找,返回1或0 auto it = myMap.find(3); cout << it->second << endl; // c if (myMap.find(3) != myMap.end()) { cout << myMap[3] << endl; // c } }
unordered_map
無序容器,操作和map類似
新增元素傳送門
有insert和emplace
void printMap(const unordered_map<int, char> &myMap) { for (auto it : myMap) { cout << it.second << " "; } cout << endl; } int main() { unordered_map<int,char> myUnorderMap={{1,'a'},{2,'b'},{3,'d'}}; printMap(myUnorderMap); myUnorderMap.emplace(4,'e'); myUnorderMap.insert({5,'d'}); printMap(myUnorderMap); for(auto it=myUnorderMap.begin();it!=myUnorderMap.end();){ if(it->first==1){ myUnorderMap.erase(it++); //刪除要使用it++,避免迭代器實(shí)效 } else{ it++; //迭代器遞增 } } printMap(myUnorderMap); }
set
有序容器,會(huì)自動(dòng)排序,默認(rèn)從小到大
void printSet(const set<int> &mySet) { for (auto it = mySet.begin(); it != mySet.end(); it++) { cout << *it << " "; } cout << endl; } int main() { set<int> mySet = {1, 1, 2, 3}; printSet(mySet); mySet.insert(0); printSet(mySet); }
queue
隊(duì)列
int main() { queue<int> myQue; //默認(rèn)使用deque作容器適配器 for (int i = 0; i < 3; i++) { myQue.push(i); } int top = myQue.front(); myQue.pop(); }
stack
棧
int main() { stack<int> mystack; for(int i=0;i<4;i++){ mystack.push(i); } int top = mystack.top(); mystack.pop(); }
創(chuàng)建容器時(shí)指定排序規(guī)則
針對(duì)有序容器使用,如map,set。vector和unordered_map則不行
struct compstru { bool operator()(int a, int b) { return a > b; } }; template <class T1, class T2> void printSet(const set<T1, T2> &mySet) { for (auto it = mySet.begin(); it != mySet.end(); it++) { cout << *it << " "; } cout << endl; } template <class T1, class T2, class T3> void printMap(const map<T1, T2, T3> &mySet) { for (auto it = mySet.begin(); it != mySet.end(); it++) { cout << it->second << " "; } cout << endl; } int main() { set<int, compstru> mySet = {1, 1, 2, 3}; printSet<int, compstru>(mySet); // 3 2 1 map<int, char, compstru> myMap = {{1, 'c'}, {2, 'b'}, {3, 'd'}}; printMap<int, char, compstru>(myMap); // d b c }
原文鏈接:https://blog.csdn.net/Fat_Markov/article/details/127335312
相關(guān)推薦
- 2022-12-09 C++筆記-設(shè)置cout輸出數(shù)據(jù)的寬度和填充方式_C 語言
- 2022-05-20 python?列表常用方法超詳細(xì)梳理總結(jié)_python
- 2023-04-11 C#圖片處理如何生成縮略圖的實(shí)現(xiàn)_C#教程
- 2023-02-03 VB十七種可用一行代碼完成判斷的技巧代碼_vb
- 2022-06-19 C語言圖文并茂講解分支語句用法_C 語言
- 2022-07-15 python優(yōu)雅實(shí)現(xiàn)代碼與敏感信息分離的方法_python
- 2021-12-08 C++?中的類型詳細(xì)_C 語言
- 2022-03-30 詳解Python的多任務(wù)進(jìn)程_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支