自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

CPP 做算法題時常用的容器技巧

開發(fā) 前端 算法
整理了一下 CPP 做算法題時常用的容器技巧。遇到一門新語言時,我首先都會思考如何實現(xiàn)下屬功能。

[[413003]]

整理了一下 CPP 做算法題時常用的容器技巧。遇到一門新語言時,我首先都會思考如何實現(xiàn)下屬功能。關(guān)于算法的筆記放在 GitHub[1] 上。

定長數(shù)組

  • 數(shù)組

變長數(shù)組

  • vector

哈希表

  • 有序字典

優(yōu)先隊列

  • 優(yōu)先隊列重載

排序

  • 排序返回索引

定長數(shù)組

要求:

  • 聲明與取數(shù)

數(shù)組

  1. int dx[4] = {0, 1, 0, -1}; 
  2. int a[N]; 
  3.  
  4. dx[0]; 

變長數(shù)組

要求:

  • 取值:取頭取尾取索引
  • 追加
  • 掐頭去尾

vector

  1. vector<int> ans(n);  // 初始長度 n ,默認(rèn)值為 0 
  2.  
  3. // 取值:取頭取尾取索引 
  4. ans.front(); 
  5. ans.back(); 
  6. ans[i] += 1; 
  7.  
  8. // 追加 
  9. // 為什么 std::vector 不支持 push_front? - Milo Yip的回答 - 知乎 
  10. // https://www.zhihu.com/question/51555037/answer/126373709 
  11. ans.push_back(5);  // O(1) 
  12.  
  13. // 去尾 
  14. ans.pop_back(); 

哈希表

要求:

  • 鍵值已存在
  • 有序字典
  1. map<intint> S; 
  2. // 鍵值已存在 
  3. if (S.count(5)) 
  4.     // S[5] 被定義過 
  5. else 
  6.     // S[5] 未被定義過 

有序字典

  • map 有序,基于紅黑樹
  • unordered_map 無序,基于映射,效率可能更高

優(yōu)先隊列

要求:

  • 空尺看存彈
  • 重載存儲對象
  • 重載比較函數(shù)

  1. // 默認(rèn)是大根堆 
  2. priority_queue<int> heap; 
  3.  
  4. // 改為小根堆 
  5. priority_queue<int, vetor<int>, greater<int> > min_heap; 
  6.  
  7. // 空尺看存彈 
  8. heap.empty(); 
  9. heap.size(); 
  10. heap.top(); 
  11. heap.push(5); 
  12. heap.pop(); 

優(yōu)先隊列重載

  1. // 重載比較函數(shù) 
  2. struct cmp { 
  3.     template<typename T, typename U> 
  4.     bool operator()(T const& left, U const &right) { 
  5.         if (left.second < right.secondreturn true
  6.         return false
  7.     } 
  8. }; 
  9.  
  10. int main() { 
  11.     unordered_map<intint> mp; 
  12.     mp[3] = 4; 
  13.     mp[2] = 44; 
  14.     mp[12] = 432; 
  15.     // 重載存儲對象 pair<intint
  16.     priority_queue<pair<intint>, vector<pair<intint>>, cmp>  pq(mp.begin(), mp.end());  //完成pq的初始化 
  17.  
  18. // 版權(quán)聲明:本文為CSDN博主「leagalhigh」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。 
  19. // 原文鏈接:https://blog.csdn.net/u014257954/article/details/78623215 

排序

要求:

  • 重載排序規(guī)則
  • 排序返回索引

  1. vector<int> ans; 
  2. sort(ans.begin(), ans.end());  // 默認(rèn)從小到大 
  3.  
  4. vector<pair<intint>> res; 
  5. sort(res.begin(), res.begin());  // 默認(rèn)比較第一個元素 

排序返回索引

  1. vector<int> data = {5, 16, 4, 7};    
  2. vector<intindex(data.size(), 0); 
  3. for (int i = 0 ; i != index.size() ; i++) { 
  4.     index[i] = i; 
  5. sort(index.begin(), index.end(), 
  6.     [&](const int& a, const int& b) { 
  7.         return (data[a] < data[b]); 
  8.     } 
  9. ); 
  10. for (int i = 0 ; i != index.size() ; i++) { 
  11.     cout << index[i] << endl; 
  12. // 版權(quán)聲明:本文為CSDN博主「liangbaqiang」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。 
  13. // 原文鏈接:https://blog.csdn.net/qq_36523492/article/details/114122256 

參考資料

[1]算法的筆記: https://github.com/PiperLiu/ACMOI_Journey

 

責(zé)任編輯:姜華 來源: Piper蛋窩
相關(guān)推薦

2022-06-29 16:37:15

算法JS數(shù)組

2020-10-20 14:10:51

Python代碼字符串

2010-08-31 16:01:18

CSS

2010-05-10 14:51:56

Unix系統(tǒng)

2016-10-21 14:35:52

Pythonwebget方法

2016-10-20 20:21:09

Python爬蟲技巧

2019-05-22 15:36:22

Linux容器鏡像

2020-06-04 10:49:53

Pandas字符串技巧

2011-07-01 16:05:22

SEO

2011-07-11 10:24:09

PHP

2011-06-16 12:43:22

jQuery

2021-01-31 23:56:49

JavaScript開發(fā)代碼

2010-05-21 18:20:43

MySQL常用技巧

2010-10-08 09:42:23

JavaScript方

2016-09-30 15:03:13

推薦系統(tǒng)算法

2024-01-04 17:00:59

2010-09-07 10:20:21

CSS

2011-01-19 09:07:20

Thunderbird

2023-04-27 09:13:20

排序算法數(shù)據(jù)結(jié)構(gòu)

2018-12-07 10:30:50

盤點CSS前端
點贊
收藏

51CTO技術(shù)棧公眾號