C++的容器簡介

來源:互聯網
上載者:User

標籤:display   imap   基於   cout   ast   而且   簡介   unique   分享   

容器,就是存放資料的地方。

C++的STL(模版庫)有兩種容器:順序容器和關聯容器。簡單來說,順序容器就是將單一類型元素聚集起來成為容器,然後根據位置來儲存和訪問這些元素。而關聯容器則是通過鍵(key)儲存和讀取元素。

標準容器類 說明
順序性容器
vector 從後面快速的插入與刪除,直接存取任何元素
deque 從前面或後面快速的插入與刪除,直接存取任何元素
list 雙鏈表,從任何地方快速插入與刪除
關聯容器
set 快速尋找,不允許重複值
multiset 快速尋找,允許重複值
map 一對多映射,基於關鍵字快速尋找,不允許重複值
multimap 一對多映射,基於關鍵字快速尋找,允許重複值
容器適配器
stack 後進先出
queue 先進先出
priority_queue 最高優先順序元素總是第一個出列

所有標準庫共有函數

預設建構函式 提供容器預設初始化的建構函式。
複製建構函式 將容器初始化為現有同類容器副本的建構函式
解構函式 不再需要容器時進行記憶體整理的解構函式
empty 容器中沒有元素時返回true,否則返回false
max_size 返回容器中最大元素個數
size 返回容器中當前元素個數
operator= 將一個容器賦給另一個容器
operator< 如果第一個容器小於第二個容器,返回true,否則返回false,
operator<= 如果第一個容器小於或等於第二個容器,返回true,否則返回false
operator> 如果第一個容器大於第二個容器,返回true,否則返回false
operator>= 如果第一個容器大於或等於第二個容器,返回true,否則返回false
operator== 如果第一個容器等於第二個容器,返回true,否則返回false
operator!= 如果第一個容器不等於第二個容器,返回true,否則返回false
swap 交換兩個容器的元素

其中operator>,operator>=,operator<,operator<=,operator==,operator!=均不適用於priority_queue

順序容器和關聯容器共有函數

 

begin 該函數兩個版本返回iterator或const_iterator,引用容器第一個元素
end 該函數兩個版本返回iterator或const_iterator,引用容器最後一個元素後面一位
rbegin 該函數兩個版本返回reverse_iterator或const_reverse_iterator,引用容器最後一個元素
rend 該函數兩個版本返回reverse_iterator或const_reverse_iterator,引用容器第一個元素前面一位
erase 從容器中清除一個或幾個元素
clear 清除容器中所有元素

①ector

最常用的容器,最初設計是為了替代C語言中的數組,可以自動擴容(初學時候我還一直吐槽為什麼不支援VLA……),支援隨機儲存,在尾端添加元素為O(1),中間為O(n)。但是問題也很大,本身存放資料方式就是數組,在size不夠時候需要把資料複製到更大的地方。

具體操作方法wiki有具體寫:https://zh.wikipedia.org/wiki/Vector_(STL)

②List

列表,實現方法是單鏈表,在任意部位插入刪除的時間複雜度都為O(n),尾部O(1),但是不支援隨機訪問,而且因為每個元素還需要儲存後指標,所以記憶體開銷比vector大了不少。

詳閱:https://zh.wikibooks.org/wiki/C%2B%2B/STL/forward_list

③deque

列表和數組的結合,支援隨機訪問,刪除插入都為O(n),兩端為O(1)(push pop),但是儲存了前後指標,記憶體開銷更大了。

詳閱:https://zh.wikibooks.org/wiki/C%2B%2B/STL/Deque

④ map、set、multimap、multiset

通過KVStore for Redis,可以按照索引值排序,尋找複雜度為O(logn)

map and multimap are associative containers that manage key/value pairs as elements as seen above. The elements of each container will sort automatically using the actual key for sorting criterion. The difference between the two is that maps do not allow duplicates, whereas, multimaps does.

  • map - unique keys
  • multimap - same key can be used many times
  • set - unique key is the value
  • multiset - key is the value, same key can be used many times

map和multimap代碼執行個體:

/* Map example - character distribution  */  #include <iostream>  #include <map>  #include <string>  #include <cctype>   using namespace std;   int main()  {          /* Character counts are stored in a map, so that            * character is the key.           * Count of char a is chars[‘a‘]. */          map<char, long> chars;           cout << "chardist - Count character distributions" << endl;          cout << "Type some text. Press ctrl-D to quit." << endl;          char c;          while (cin.get(c)) {                  // Upper A and lower a are considered the same                   c=tolower(static_cast<unsigned char>(c));                  chars[c]=chars[c]+1; // Could be written as ++chars[c];          }           cout << "Character distribution: " << endl;                      string alphabet("abcdefghijklmnopqrstuvwxyz");          for (string::iterator letter_index=alphabet.begin(); letter_index != alphabet.end(); letter_index++) {                  if (chars[*letter_index] != 0) {                          cout << char(toupper(*letter_index))                               << ":" << chars[*letter_index]                               << "\t" << endl;                   }          }          return 0;  }
View Code

 

參考

STL十大容器, just_kong, http://www.aiuxian.com/article/p-2119514.html

C++ Programming/STL, https://en.wikibooks.org/wiki/C%2B%2B_Programming/STL#Containers

C++的容器簡介

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.