網站首頁 編程語言 正文
sort排序
針對含有迭代器的容器,可以用#include<algorithm>
中的sort函數進行排序。
默認排序是從小到大,可以自己寫仿函數,也可以用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()); // 反轉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的二維數組 myVec1.push_back(10); myVec1.pop_back(); }
map
紅黑樹實現是有序容器,按照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'; //有覆蓋數據的危險 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++,避免迭代器實效 } else{ it++; //迭代器遞增 } } printMap(myUnorderMap); }
set
有序容器,會自動排序,默認從小到大
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
隊列
int main() { queue<int> myQue; //默認使用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(); }
創建容器時指定排序規則
針對有序容器使用,如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
相關推薦
- 2023-03-25 Python?self參數詳細介紹_python
- 2022-09-22 k8s 存儲卷之 PV & PVC
- 2022-05-16 SQL?Server中常用截取字符串函數介紹_MsSql
- 2024-03-19 Linux中 find 命令詳解
- 2023-07-09 SQL Server中的NULL值處理:判斷與解決方案
- 2022-03-15 使用swagger-bootstrap-ui ,訪問的時候 404
- 2022-04-25 C++特殊成員函數以及其生成機制詳解_C 語言
- 2022-06-15 django?settings.py配置文件的詳細介紹_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支