{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/cpp-common-containers/",
  "atlas": "https://www.pystone.net/?node=cpp-common-containers#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/cpp-common-containers.md",
  "context": "https://www.pystone.net/notes/cpp-common-containers.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:cpp-common-containers",
  "slug": "cpp-common-containers",
  "title": "C++常用容器",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "C++常用容器",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "8d9721a4b9e8eeb386b6d8f0fa5eeb9b89ffd26b633c0d769ea9e61b4f4eb461",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "C++常用容器",
      "anchor": "c常用容器",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#c%E5%B8%B8%E7%94%A8%E5%AE%B9%E5%99%A8"
    },
    {
      "depth": 2,
      "text": "Set",
      "anchor": "set",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#set"
    },
    {
      "depth": 3,
      "text": "关联容器与顺序容器",
      "anchor": "关联容器与顺序容器",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#%E5%85%B3%E8%81%94%E5%AE%B9%E5%99%A8%E4%B8%8E%E9%A1%BA%E5%BA%8F%E5%AE%B9%E5%99%A8"
    },
    {
      "depth": 3,
      "text": "set分类",
      "anchor": "set分类",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#set%E5%88%86%E7%B1%BB"
    },
    {
      "depth": 3,
      "text": "特点",
      "anchor": "特点",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#%E7%89%B9%E7%82%B9"
    },
    {
      "depth": 3,
      "text": "用法",
      "anchor": "用法",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#%E7%94%A8%E6%B3%95"
    },
    {
      "depth": 2,
      "text": "map",
      "anchor": "map",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#map"
    },
    {
      "depth": 3,
      "text": "pair",
      "anchor": "pair",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#pair"
    },
    {
      "depth": 3,
      "text": "map",
      "anchor": "map-1",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#map-1"
    },
    {
      "depth": 4,
      "text": "插入方法：",
      "anchor": "插入方法",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#%E6%8F%92%E5%85%A5%E6%96%B9%E6%B3%95"
    },
    {
      "depth": 4,
      "text": "查找和读取",
      "anchor": "查找和读取",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#%E6%9F%A5%E6%89%BE%E5%92%8C%E8%AF%BB%E5%8F%96"
    },
    {
      "depth": 4,
      "text": "删除",
      "anchor": "删除",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#%E5%88%A0%E9%99%A4"
    },
    {
      "depth": 4,
      "text": "基本操作函数总结",
      "anchor": "基本操作函数总结",
      "citation": "https://www.pystone.net/notes/cpp-common-containers/#%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C%E5%87%BD%E6%95%B0%E6%80%BB%E7%BB%93"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:programming-languages-and-runtimes",
      "title": "编程语言与运行时",
      "url": "https://www.pystone.net/notes/programming-languages-and-runtimes/",
      "atlas": "https://www.pystone.net/?node=programming-languages-and-runtimes#knowledge-atlas",
      "label": "编程语言与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "C++中的“C++常用容器”导航项",
      "citation": "https://www.pystone.net/notes/programming-languages-and-runtimes/#c"
    }
  ],
  "contentMarkdown": "# C++常用容器\n\n> 创建时间：2020/1/16 22:12\n\n* stack\n  * Set\n    * 关联容器与顺序容器\n    * set分类\n    * 特点\n    * 用法\n  * map\n    * pair\n    * map\n      * 插入方法：\n      * 查找和读取\n      * 删除\n      * 基本操作函数总结\n\ntemplate <class T, class Container = deque<T> > class stack;\n第一个是T，表示栈中存放的数据的类型，比如int，double，或者结构体之类。\n第二个参数指明底层实现的容器类型，也就是指明这个栈的内部实现方式，比如vector，deque，list。如果不指明它，默认使用deque(双端队列)。一般情况下不需要指定这一项参数。\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n## Set\n\n### 关联容器与顺序容器\n\n  * 顺序容器（sequence container）包括vector、deque、list、forward_list、array、string，所有顺序容器都提供了快速顺序访问元素的能力。\n  * 顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的\n  * 关联容器（associative container）包括set、map\n  * 关联容器中的元素是按关键字来保存和访问的\n\n关联容器 **不支持** 顺序容器的 **位置相关的操作** 。原因是关联容器中元素是根据关键字存储的，这些操作对关联容器没有意义。\n\n### set分类\n\n  1. 按关键字有序保存元素：set；multiset(关键字可重复出现t)；\n  2. 无序集合：unordered_set(用哈希函数组织的set)；unordered_multiset(哈希组织的set，关键字可以重复出现)。\n\n### 特点\n\n  * set中元素 **值唯一，自动排序** ，元素的值不能直接被改变。\n  * set内部采用 **红黑树** （RB树）(Red-Black Tree)，一种非常高效的平衡检索二叉树，插入和删除操作效率较高。 **O(log n)**\n\n### 用法\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n## map\n\n### pair\n\n#include<utility>\n\n  * pair<T1, T2> p;\n  * pair<int, string> p1(0, \"Hello\");\n  * pair<int, string> p2 = make_pair(1, \"World\");\n  * p.first\n  * p.second\n\n### map\n\n> map內部的实现自建一颗红黑树，这颗树具有对数据自动排序的功能\n\n  * map<k, v> m;\n  * map<k, v> m(m2);\n  * map<k, v> m(b, e); //两个迭代器\n\nmap的value_type是存储元素的键以及值的pair类型，键为const。\n\n#### 插入方法：\n\n  1. 使用下标\n在map中使用下标访问不存在的元素将导致在map容器中添加一个新的元素。\n如果已经有该key，会覆盖以前该关键字对应的值\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n  2. 使用insert函数\n\n  * m.insert(e)\n  * m.insert(beg, end)\n  * m.insert(iter, e)\n当map中有这个关键字时，insert操作是不能在插入数据的\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n#### 查找和读取\n\n  1. 用下标的方法读取map中元素时，若map中不存在该元素，则会在map中插入。\n  2. count(k) ： 只是查找该元素是否存在\n  3. find(k) ： 想取得key对应的值\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n#### 删除\n\n  * m.erase(k); //key\n  * m.erase(p); //迭代器p\n  * m.erase(b, e); //迭代器b、e\n\n#### 基本操作函数总结\n\nC++ maps是一种关联式容器，包含“关键字/值”对\nbegin() 返回指向map头部的迭代器\nclear(） 删除所有元素\ncount() 返回指定元素出现的次数\nempty() 如果map为空则返回true\nend() 返回指向map末尾的迭代器\nequal_range() 返回特殊条目的迭代器对\nerase() 删除一个元素\nfind() 查找一个元素\nget_allocator() 返回map的配置器\ninsert() 插入元素\nkey_comp() 返回比较元素key的函数\nlower_bound() 返回键值>=给定元素的第一个位置\nmax_size() 返回可以容纳的最大元素个数\nrbegin() 返回一个指向map尾部的逆向迭代器\nrend() 返回一个指向map头部的逆向迭代器\nsize() 返回map中元素的个数\nswap() 交换两个map\nupper_bound() 返回键值>给定元素的第一个位置\nvalue_comp() 返回比较元素value的函数\n"
}
