C++ 容器 API 速查表 Cheat Sheet

顺序容器(Sequential Containers)

按照插入顺序存储,保持元素的顺序性。这意味着访问和迭代元素时会按照插入的顺序进行。

Vector

  • 动态数组,支持随机访问。
  • 元素在内存中连续存储,允许高效的索引操作。
  • 插入和删除操作在末尾是高效的,但在中间或开头的操作会比较慢,因为需要移动元素。
函数 描述 示例
vector() 默认构造函数,创建一个空的 std::vector std::vector<int> v;
vector(size_t n) 创建一个包含 n 个默认初始化元素的 std::vector std::vector<int> v(10);
vector(size_t n, const T& value) 创建一个包含 n 个初始化为 value 的元素的 std::vector std::vector<int> v(10, 5);
vector(initializer_list<T> il) 使用初始化列表创建 std::vector std::vector<int> v = {1, 2, 3};
vector(const vector& other) 拷贝构造函数。 std::vector<int> v2(v1);
vector(vector&& other) 移动构造函数。 std::vector<int> v2(std::move(v1));
operator= 拷贝赋值运算符。 v2 = v1;
operator= 移动赋值运算符。 v2 = std::move(v1);
~vector() 析构函数。
size() 返回容器中元素的数量。 size_t sz = v.size();
empty() 如果容器为空,则返回 true bool isEmpty = v.empty();
resize(size_t n) 调整容器的大小为 n v.resize(5);
resize(size_t n, const T& value) 调整容器的大小为 n,并将新元素初始化为 value v.resize(5, 3);
reserve(size_t n) 请求分配足够的容量,以至少容纳 n 个元素。 v.reserve(20);
capacity() 返回当前分配给容器的内存可以容纳的元素数量。 size_t cap = v.capacity();
shrink_to_fit() 减少容器的容量以适应其大小。 v.shrink_to_fit();
operator[] 访问指定位置的元素,不进行边界检查。 int value = v[2];
at(size_t n) 访问指定位置的元素,进行边界检查。 int value = v.at(2);
front() 返回第一个元素的引用。 int& first = v.front();
back() 返回最后一个元素的引用。 int& last = v.back();
data() 返回指向容器中第一个元素的指针。 int* ptr = v.data();
push_back(const T& value) 在容器末尾添加一个元素的副本。 v.push_back(10);
push_back(T&& value) 在容器末尾添加一个元素,通过移动语义。 v.push_back(std::move(value));
pop_back() 移除容器末尾的元素。 v.pop_back();
insert(iterator pos, const T& value) 在指定位置前插入一个元素的副本。 v.insert(v.begin() + 2, 10);
insert(iterator pos, T&& value) 在指定位置前插入一个元素,通过移动语义。 v.insert(v.begin() + 2, std::move(value));
insert(iterator pos, size_t n, const T& value) 在指定位置前插入 nvalue 的副本。 v.insert(v.begin() + 2, 5, 10);
erase(iterator pos) 移除指定位置的元素。 v.erase(v.begin() + 2);
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 v.erase(v.begin() + 2, v.begin() + 4);
clear() 移除所有元素。 v.clear();
swap(vector& other) 交换两个容器的内容。 v1.swap(v2);
begin() 返回指向容器第一个元素的迭代器。 auto it = v.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = v.end();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = v.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = v.rend();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = v.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = v.cend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = v.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = v.crend();
emplace_back(args...) 在容器末尾原地构造一个元素。 v.emplace_back(args...);
emplace(iterator pos, args...) 在指定位置前原地构造一个元素。 v.emplace(v.begin() + 2, args...);

String

  • 专门用于存储和操作字符序列。
  • 提供丰富的字符串操作函数。
  • 实际上是一个 vector<char> 的封装,但有更多的功能。
函数 描述 示例
string() 默认构造函数,创建一个空的 std::string std::string str;
string(const char* s) 使用C字符串 s 初始化 std::string std::string str("hello");
string(const string& other) 拷贝构造函数。 std::string str2(str1);
string(string&& other) 移动构造函数。 std::string str2(std::move(str1));
string(const char* s, size_t n) 使用前 n 个字符初始化 std::string std::string str("hello", 3);
string(size_t n, char c) 创建一个包含 n 个字符 cstd::string std::string str(5, 'a');
operator= 拷贝赋值运算符。 str2 = str1;
operator= 移动赋值运算符。 str2 = std::move(str1);
operator+= 连接赋值运算符。 str += "world";
operator+ 连接运算符。 std::string str3 = str1 + str2;
size() 返回字符串中的字符数。 size_t sz = str.size();
length() 返回字符串中的字符数,与 size() 等效。 size_t len = str.length();
empty() 如果字符串为空,则返回 true bool isEmpty = str.empty();
clear() 清空字符串内容。 str.clear();
operator[] 访问指定位置的字符,不进行边界检查。 char c = str[2];
at(size_t pos) 访问指定位置的字符,进行边界检查。 char c = str.at(2);
front() 返回第一个字符的引用。 char& first = str.front();
back() 返回最后一个字符的引用。 char& last = str.back();
data() 返回指向字符串数据的指针(非空终止)。 const char* p = str.data();
c_str() 返回指向字符串数据的指针(以空字符终止)。 const char* cstr = str.c_str();
push_back(char c) 在字符串末尾添加一个字符。 str.push_back('a');
pop_back() 移除字符串末尾的一个字符。 str.pop_back();
append(const string& str) str 追加到当前字符串末尾。 str1.append(str2);
append(const char* s) 将C字符串 s 追加到当前字符串末尾。 str.append("world");
append(const char* s, size_t n) 将C字符串 s 的前 n 个字符追加到当前字符串末尾。 str.append("world", 3);
insert(size_t pos, const string& str) 在指定位置插入字符串 str str.insert(2, "abc");
insert(size_t pos, const char* s) 在指定位置插入C字符串 s str.insert(2, "abc");
erase(size_t pos = 0, size_t n = npos) 从指定位置开始删除 n 个字符。 str.erase(2, 3);
replace(size_t pos, size_t n, const string& str) str 替换从指定位置开始的 n 个字符。 str.replace(2, 3, "xyz");
substr(size_t pos = 0, size_t n = npos) 返回从指定位置开始的 n 个字符组成的子字符串。 std::string sub = str.substr(2, 3);
copy(char* s, size_t n, size_t pos = 0) 将从指定位置开始的 n 个字符复制到数组 s 中。 str.copy(buffer, 3, 2);
find(const string& str, size_t pos = 0) 查找子字符串 str,返回首次出现的位置。 size_t pos = str.find("abc");
rfind(const string& str, size_t pos = npos) 查找子字符串 str,返回最后一次出现的位置。 size_t pos = str.rfind("abc");
find_first_of(const string& str, size_t pos = 0) 查找字符串 str 中任一字符,返回首次出现的位置。 size_t pos = str.find_first_of("aeiou");
find_last_of(const string& str, size_t pos = npos) 查找字符串 str 中任一字符,返回最后一次出现的位置。 size_t pos = str.find_last_of("aeiou");
find_first_not_of(const string& str, size_t pos = 0) 查找第一个不在字符串 str 中的字符,返回首次出现的位置。 size_t pos = str.find_first_not_of("aeiou");
find_last_not_of(const string& str, size_t pos = npos) 查找最后一个不在字符串 str 中的字符,返回最后一次出现的位置。 size_t pos = str.find_last_not_of("aeiou");
compare(const string& str) 比较两个字符串的大小。 int result = str1.compare(str2);
swap(string& other) 交换两个字符串的内容。 str1.swap(str2);
begin() 返回指向字符串第一个字符的迭代器。 auto it = str.begin();
end() 返回指向字符串最后一个字符之后的迭代器。 auto it = str.end();
rbegin() 返回指向字符串最后一个字符的反向迭代器。 auto it = str.rbegin();
rend() 返回指向字符串第一个字符之前的反向迭代器。 auto it = str.rend();
cbegin() 返回指向字符串第一个字符的常量迭代器。 auto it = str.cbegin();
cend() 返回指向字符串最后一个字符之后的常量迭代器。 auto it = str.cend();
crbegin() 返回指向字符串最后一个字符的常量反向迭代器。 auto it = str.crbegin();
crend() 返回指向字符串第一个字符之前的常量反向迭代器。 auto it = str.crend();

List

  • 双向链表,支持在任何位置进行高效的插入和删除操作。
  • 不支持随机访问,只能顺序访问。
  • 每个元素包含一个指向前后元素的指针,存储开销较大。
函数 描述 示例
list() 默认构造函数,创建一个空的 std::list std::list<int> l;
list(size_t n) 创建一个包含 n 个默认初始化元素的 std::list std::list<int> l(10);
list(size_t n, const T& value) 创建一个包含 n 个初始化为 value 的元素的 std::list std::list<int> l(10, 5);
list(initializer_list<T> il) 使用初始化列表创建 std::list std::list<int> l = {1, 2, 3};
list(const list& other) 拷贝构造函数。 std::list<int> l2(l1);
list(list&& other) 移动构造函数。 std::list<int> l2(std::move(l1));
operator= 拷贝赋值运算符。 l2 = l1;
operator= 移动赋值运算符。 l2 = std::move(l1);
~list() 析构函数。
size() 返回容器中元素的数量。 size_t sz = l.size();
empty() 如果容器为空,则返回 true bool isEmpty = l.empty();
resize(size_t n) 调整容器的大小为 n l.resize(5);
resize(size_t n, const T& value) 调整容器的大小为 n,并将新元素初始化为 value l.resize(5, 3);
front() 返回第一个元素的引用。 int& first = l.front();
back() 返回最后一个元素的引用。 int& last = l.back();
push_front(const T& value) 在容器头部添加一个元素的副本。 l.push_front(10);
push_front(T&& value) 在容器头部添加一个元素,通过移动语义。 l.push_front(std::move(value));
push_back(const T& value) 在容器末尾添加一个元素的副本。 l.push_back(10);
push_back(T&& value) 在容器末尾添加一个元素,通过移动语义。 l.push_back(std::move(value));
pop_front() 移除容器头部的元素。 l.pop_front();
pop_back() 移除容器末尾的元素。 l.pop_back();
insert(iterator pos, const T& value) 在指定位置前插入一个元素的副本。 l.insert(l.begin(), 10);
insert(iterator pos, T&& value) 在指定位置前插入一个元素,通过移动语义。 l.insert(l.begin(), std::move(value));
insert(iterator pos, size_t n, const T& value) 在指定位置前插入 nvalue 的副本。 l.insert(l.begin(), 5, 10);
erase(iterator pos) 移除指定位置的元素。 l.erase(l.begin());
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 l.erase(l.begin(), std::next(l.begin(), 2));
clear() 移除所有元素。 l.clear();
swap(list& other) 交换两个容器的内容。 l1.swap(l2);
begin() 返回指向容器第一个元素的迭代器。 auto it = l.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = l.end();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = l.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = l.rend();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = l.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = l.cend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = l.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = l.crend();
emplace_back(args...) 在容器末尾原地构造一个元素。 l.emplace_back(args...);
emplace_front(args...) 在容器头部原地构造一个元素。 l.emplace_front(args...);
emplace(iterator pos, args...) 在指定位置前原地构造一个元素。 l.emplace(l.begin(), args...);
splice(iterator pos, list& other) 在指定位置前将另一个list的内容插入到此list中。 l1.splice(l1.begin(), l2);
splice(iterator pos, list& other, iterator it) 在指定位置前将另一个list的一个元素插入到此list中。 l1.splice(l1.begin(), l2, l2.begin());
remove(const T& value) 移除所有等于value的元素。 l.remove(10);
remove_if(UnaryPredicate p) 移除所有满足谓词p的元素。 l.remove_if([](int x) { return x > 10; });
merge(list& other) 合并两个有序list。 l1.merge(l2);
merge(list& other, Compare comp) 用指定的比较函数合并两个有序list。 l1.merge(l2, comp);
sort() 对list元素进行排序。 l.sort();
sort(Compare comp) 用指定的比较函数对list元素进行排序。 l.sort(comp);
reverse() 反转list中的元素顺序。 l.reverse();

Deque

  • 双端队列,支持在两端进行高效的插入和删除。
  • 允许快速的随机访问。
  • 内存不是连续的,通常实现为一组连续块。
函数 描述 示例
deque() 默认构造函数,创建一个空的 std::deque std::deque<int> d;
deque(size_t n) 创建一个包含 n 个默认初始化元素的 std::deque std::deque<int> d(10);
deque(size_t n, const T& value) 创建一个包含 n 个初始化为 value 的元素的 std::deque std::deque<int> d(10, 5);
deque(initializer_list<T> il) 使用初始化列表创建 std::deque std::deque<int> d = {1, 2, 3};
deque(const deque& other) 拷贝构造函数。 std::deque<int> d2(d1);
deque(deque&& other) 移动构造函数。 std::deque<int> d2(std::move(d1));
operator= 拷贝赋值运算符。 d2 = d1;
operator= 移动赋值运算符。 d2 = std::move(d1);
~deque() 析构函数。
size() 返回容器中元素的数量。 size_t sz = d.size();
empty() 如果容器为空,则返回 true bool isEmpty = d.empty();
resize(size_t n) 调整容器的大小为 n d.resize(5);
resize(size_t n, const T& value) 调整容器的大小为 n,并将新元素初始化为 value d.resize(5, 3);
front() 返回第一个元素的引用。 int& first = d.front();
back() 返回最后一个元素的引用。 int& last = d.back();
push_front(const T& value) 在容器头部添加一个元素的副本。 d.push_front(10);
push_front(T&& value) 在容器头部添加一个元素,通过移动语义。 d.push_front(std::move(value));
push_back(const T& value) 在容器末尾添加一个元素的副本。 d.push_back(10);
push_back(T&& value) 在容器末尾添加一个元素,通过移动语义。 d.push_back(std::move(value));
pop_front() 移除容器头部的元素。 d.pop_front();
pop_back() 移除容器末尾的元素。 d.pop_back();
insert(iterator pos, const T& value) 在指定位置前插入一个元素的副本。 d.insert(d.begin(), 10);
insert(iterator pos, T&& value) 在指定位置前插入一个元素,通过移动语义。 d.insert(d.begin(), std::move(value));
insert(iterator pos, size_t n, const T& value) 在指定位置前插入 nvalue 的副本。 d.insert(d.begin(), 5, 10);
erase(iterator pos) 移除指定位置的元素。 d.erase(d.begin());
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 d.erase(d.begin(), std::next(d.begin(), 2));
clear() 移除所有元素。 d.clear();
swap(deque& other) 交换两个容器的内容。 d1.swap(d2);
begin() 返回指向容器第一个元素的迭代器。 auto it = d.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = d.end();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = d.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = d.rend();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = d.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = d.cend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = d.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = d.crend();
emplace_back(args...) 在容器末尾原地构造一个元素。 d.emplace_back(args...);
emplace_front(args...) 在容器头部原地构造一个元素。 d.emplace_front(args...);
emplace(iterator pos, args...) 在指定位置前原地构造一个元素。 d.emplace(d.begin(), args...);

Array

  • 固定大小的数组,大小在编译时确定。
  • 元素在内存中连续存储,允许高效的索引操作。
  • 一旦创建,大小不可改变。
函数 描述 示例
array() 默认构造函数,创建一个空的 std::array std::array<int, 5> arr;
array(const array& other) 拷贝构造函数。 std::array<int, 5> arr2(arr1);
array(array&& other) 移动构造函数。 std::array<int, 5> arr2(std::move(arr1));
operator= 拷贝赋值运算符。 arr2 = arr1;
operator= 移动赋值运算符。 arr2 = std::move(arr1);
fill(const T& value) 用指定值填充数组的所有元素。 arr.fill(5);
size() 返回数组中元素的数量。 size_t sz = arr.size();
empty() 如果数组为空,则返回 true bool isEmpty = arr.empty();
operator[] 访问指定位置的元素,不进行边界检查。 int value = arr[2];
at(size_t n) 访问指定位置的元素,进行边界检查。 int value = arr.at(2);
front() 返回第一个元素的引用。 int& first = arr.front();
back() 返回最后一个元素的引用。 int& last = arr.back();
data() 返回指向数组中第一个元素的指针。 int* ptr = arr.data();
begin() 返回指向数组第一个元素的迭代器。 auto it = arr.begin();
end() 返回指向数组最后一个元素之后的迭代器。 auto it = arr.end();
rbegin() 返回指向数组最后一个元素的反向迭代器。 auto it = arr.rbegin();
rend() 返回指向数组第一个元素之前的反向迭代器。 auto it = arr.rend();
cbegin() 返回指向数组第一个元素的常量迭代器。 auto it = arr.cbegin();
cend() 返回指向数组最后一个元素之后的常量迭代器。 auto it = arr.cend();
crbegin() 返回指向数组最后一个元素的常量反向迭代器。 auto it = arr.crbegin();
crend() 返回指向数组第一个元素之前的常量反向迭代器。 auto it = arr.crend();
swap(array& other) 交换两个数组的内容。 arr1.swap(arr2);

Forward List

  • 单向链表,类似于 list,但只能向前遍历。
  • 占用内存较少,适用于只需要单向遍历的场景。
  • 插入和删除操作高效。
函数 描述 示例
forward_list() 默认构造函数,创建一个空的 std::forward_list std::forward_list<int> fl;
forward_list(size_t n) 创建一个包含 n 个默认初始化元素的 std::forward_list std::forward_list<int> fl(10);
forward_list(size_t n, const T& value) 创建一个包含 n 个初始化为 value 的元素的 std::forward_list std::forward_list<int> fl(10, 5);
forward_list(initializer_list<T> il) 使用初始化列表创建 std::forward_list std::forward_list<int> fl = {1, 2, 3};
forward_list(const forward_list& other) 拷贝构造函数。 std::forward_list<int> fl2(fl1);
forward_list(forward_list&& other) 移动构造函数。 std::forward_list<int> fl2(std::move(fl1));
operator= 拷贝赋值运算符。 fl2 = fl1;
operator= 移动赋值运算符。 fl2 = std::move(fl1);
~forward_list() 析构函数。
assign(size_t n, const T& value) nvalue 赋值整个列表。 fl.assign(10, 5);
assign(initializer_list<T> il) 用初始化列表 il 赋值整个列表。 fl.assign({1, 2, 3});
front() 返回第一个元素的引用。 int& first = fl.front();
empty() 如果容器为空,则返回 true bool isEmpty = fl.empty();
max_size() 返回容器中最大可能的元素数量。 size_t maxSz = fl.max_size();
clear() 移除所有元素。 fl.clear();
insert_after(iterator pos, const T& value) 在指定位置后插入一个元素的副本。 fl.insert_after(fl.before_begin(), 10);
insert_after(iterator pos, T&& value) 在指定位置后插入一个元素,通过移动语义。 fl.insert_after(fl.before_begin(), std::move(value));
insert_after(iterator pos, size_t n, const T& value) 在指定位置后插入 nvalue 的副本。 fl.insert_after(fl.before_begin(), 5, 10);
erase_after(iterator pos) 移除指定位置后的元素。 fl.erase_after(fl.before_begin());
erase_after(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 fl.erase_after(fl.before_begin(), fl.end());
push_front(const T& value) 在容器头部添加一个元素的副本。 fl.push_front(10);
push_front(T&& value) 在容器头部添加一个元素,通过移动语义。 fl.push_front(std::move(value));
pop_front() 移除容器头部的元素。 fl.pop_front();
resize(size_t n) 调整容器的大小为 n fl.resize(5);
resize(size_t n, const T& value) 调整容器的大小为 n,并将新元素初始化为 value fl.resize(5, 3);
swap(forward_list& other) 交换两个容器的内容。 fl1.swap(fl2);
merge(forward_list& other) 合并两个有序列表。 fl1.merge(fl2);
merge(forward_list& other, Compare comp) 用指定的比较函数合并两个有序列表。 fl1.merge(fl2, comp);
splice_after(iterator pos, forward_list& other) 在指定位置后插入另一个列表的内容。 fl1.splice_after(fl1.before_begin(), fl2);
remove(const T& value) 移除所有等于value的元素。 fl.remove(10);
remove_if(UnaryPredicate p) 移除所有满足谓词p的元素。 fl.remove_if([](int x) { return x > 10; });
reverse() 反转列表中的元素顺序。 fl.reverse();
sort() 对列表元素进行排序。 fl.sort();
sort(Compare comp) 用指定的比较函数对列表元素进行排序。 fl.sort(comp);
unique() 移除所有连续的重复元素。 fl.unique();
unique(BinaryPredicate p) 用指定的二元谓词移除所有连续的重复元素。 fl.unique(p);
before_begin() 返回指向列表第一个元素之前的位置的迭代器。 auto it = fl.before_begin();
begin() 返回指向列表第一个元素的迭代器。 auto it = fl.begin();
end() 返回指向列表最后一个元素之后的迭代器。 auto it = fl.end();
cbegin() 返回指向列表第一个元素的常量迭代器。 auto it = fl.cbegin();
cend() 返回指向列表最后一个元素之后的常量迭代器。 auto it = fl.cend();
emplace_after(iterator pos, args...) 在指定位置后原地构造一个元素。 fl.emplace_after(fl.before_begin(), args...);
emplace_front(args...) 在容器头部原地构造一个元素。 fl.emplace_front(args...);

容器适配器(Container Adapters)

容器适配器(Container Adapters)是一种容器类,它们提供了特定用途的接口,通过限制和重新定义基础容器(如 dequevectorlist)的一些操作来实现特定的行为模式。

主要的容器适配器有 stackqueuepriority_queue。它们基于其他顺序容器构建,但提供了不同的接口和行为。

Stack

后进先出(LIFO,Last In First Out)

函数 描述 示例
stack() 默认构造函数,创建一个空的 std::stack std::stack<int> s;
stack(const stack& other) 拷贝构造函数。 std::stack<int> s2(s1);
stack(stack&& other) 移动构造函数。 std::stack<int> s2(std::move(s1));
operator= 拷贝赋值运算符。 s2 = s1;
operator= 移动赋值运算符。 s2 = std::move(s1);
~stack() 析构函数。
empty() 如果栈为空,则返回 true bool isEmpty = s.empty();
size() 返回栈中元素的数量。 size_t sz = s.size();
top() 返回栈顶元素的引用。 int& topElement = s.top();
push(const T& value) 在栈顶添加一个元素的副本。 s.push(10);
push(T&& value) 在栈顶添加一个元素,通过移动语义。 s.push(std::move(value));
emplace(args...) 在栈顶原地构造一个元素。 s.emplace(args...);
pop() 移除栈顶元素。 s.pop();
swap(stack& other) 交换两个栈的内容。 s1.swap(s2);

Queue

先进先出(FIFO,First In First Out)

函数 描述 示例
queue() 默认构造函数,创建一个空的 std::queue std::queue<int> q;
queue(const queue& other) 拷贝构造函数。 std::queue<int> q2(q1);
queue(queue&& other) 移动构造函数。 std::queue<int> q2(std::move(q1));
operator= 拷贝赋值运算符。 q2 = q1;
operator= 移动赋值运算符。 q2 = std::move(q1);
~queue() 析构函数。
empty() 如果队列为空,则返回 true bool isEmpty = q.empty();
size() 返回队列中元素的数量。 size_t sz = q.size();
front() 返回队列头元素的引用。 int& frontElement = q.front();
back() 返回队列尾元素的引用。 int& backElement = q.back();
push(const T& value) 在队列尾添加一个元素的副本。 q.push(10);
push(T&& value) 在队列尾添加一个元素,通过移动语义。 q.push(std::move(value));
emplace(args...) 在队列尾原地构造一个元素。 q.emplace(args...);
pop() 移除队列头元素。 q.pop();
swap(queue& other) 交换两个队列的内容。 q1.swap(q2);

Priority Queue

元素按照优先级排序(默认情况下是最大堆,最大元素在队列前端)

函数 描述 示例
priority_queue() 默认构造函数,创建一个空的 std::priority_queue std::priority_queue<int> pq;
priority_queue(const Compare& comp) 使用给定的比较函数创建一个空的 std::priority_queue std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
priority_queue(const priority_queue& other) 拷贝构造函数。 std::priority_queue<int> pq2(pq1);
priority_queue(priority_queue&& other) 移动构造函数。 std::priority_queue<int> pq2(std::move(pq1));
priority_queue(InputIterator first, InputIterator last) 用范围 [first, last) 的元素创建一个 std::priority_queue std::vector<int> v = {1, 2, 3}; std::priority_queue<int> pq(v.begin(), v.end());
priority_queue(const Compare& comp, const Container& cont) 用给定的比较函数和容器创建一个 std::priority_queue std::vector<int> v = {1, 2, 3}; std::priority_queue<int, std::vector<int>, std::greater<int>> pq(std::greater<int>(), v);
operator= 拷贝赋值运算符。 pq2 = pq1;
operator= 移动赋值运算符。 pq2 = std::move(pq1);
~priority_queue() 析构函数。
empty() 如果队列为空,则返回 true bool isEmpty = pq.empty();
size() 返回队列中元素的数量。 size_t sz = pq.size();
top() 返回队列中具有最高优先级的元素的引用。 int& topElement = pq.top();
push(const T& value) 向队列中添加一个元素的副本。 pq.push(10);
push(T&& value) 向队列中添加一个元素,通过移动语义。 pq.push(std::move(value));
emplace(args...) 在队列中原地构造一个元素。 pq.emplace(args...);
pop() 移除队列中具有最高优先级的元素。 pq.pop();
swap(priority_queue& other) 交换两个队列的内容。 pq1.swap(pq2);

注意:默认使用 std::less<T> 比较函数来创建最大堆。如果需要创建最小堆,可以使用 std::greater<T> 作为比较函数。

关联容器(Associative Containers)

用于存储和管理键值对或具有排序关系的元素集合。它们提供高效的查找、插入和删除操作,通常基于平衡二叉树(如红黑树)实现。

注意:有序集合,默认使用 < 运算符来比较元素。可以通过在模板参数中指定自定义比较函数来改变默认行为。

特点:

  • 自动排序:关联容器中的元素根据键自动排序。
  • 高效查找:提供对元素的高效查找、插入和删除操作,平均时间复杂度为O(log n)。

Set

存储唯一的元素,按特定顺序排列(默认情况下按升序)

函数 描述 示例
set() 默认构造函数,创建一个空的 std::set std::set<int> s;
set(const set& other) 拷贝构造函数。 std::set<int> s2(s1);
set(set&& other) 移动构造函数。 std::set<int> s2(std::move(s1));
set(initializer_list<T> il) 使用初始化列表创建 std::set std::set<int> s = {1, 2, 3};
set(InputIterator first, InputIterator last) 用范围 [first, last) 的元素创建一个 std::set std::vector<int> v = {1, 2, 3}; std::set<int> s(v.begin(), v.end());
operator= 拷贝赋值运算符。 s2 = s1;
operator= 移动赋值运算符。 s2 = std::move(s1);
~set() 析构函数。
begin() 返回指向容器第一个元素的迭代器。 auto it = s.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = s.end();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = s.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = s.cend();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = s.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = s.rend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = s.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = s.crend();
empty() 如果容器为空,则返回 true bool isEmpty = s.empty();
size() 返回容器中元素的数量。 size_t sz = s.size();
max_size() 返回容器中最大可能的元素数量。 size_t maxSz = s.max_size();
clear() 移除所有元素。 s.clear();
insert(const T& value) 插入元素的副本。 s.insert(10);
insert(T&& value) 插入元素,通过移动语义。 s.insert(std::move(value));
insert(initializer_list<T> il) 插入初始化列表中的所有元素。 s.insert({1, 2, 3});
insert(iterator hint, const T& value) 插入元素的副本,使用迭代器 hint 作为插入位置的提示。 s.insert(s.begin(), 10);
insert(iterator hint, T&& value) 插入元素,通过移动语义,使用迭代器 hint 作为插入位置的提示。 s.insert(s.begin(), std::move(value));
erase(iterator pos) 移除指定位置的元素。 s.erase(s.begin());
erase(const T& value) 移除等于 value 的元素。 s.erase(10);
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 s.erase(s.begin(), s.end());
swap(set& other) 交换两个容器的内容。 s1.swap(s2);
count(const T& value) 返回等于 value 的元素数量(对于 set 来说只能是 0 或 1)。 size_t count = s.count(10);
find(const T& value) 查找等于 value 的元素,返回指向该元素的迭代器。 auto it = s.find(10);
lower_bound(const T& value) 返回指向第一个不小于 value 的元素的迭代器。 auto it = s.lower_bound(10);
upper_bound(const T& value) 返回指向第一个大于 value 的元素的迭代器。 auto it = s.upper_bound(10);
equal_range(const T& value) 返回一个范围,包括所有等于 value 的元素。 auto range = s.equal_range(10);
key_comp() 返回一个比较函数对象,用于比较元素的键。 auto comp = s.key_comp();
value_comp() 返回一个比较函数对象,用于比较元素的值。 auto comp = s.value_comp();
emplace(args...) 在容器中原地构造元素。 s.emplace(args...);
emplace_hint(iterator hint, args...) 在容器中原地构造元素,使用迭代器 hint 作为位置提示。 s.emplace_hint(s.begin(), args...);

Multiset

存储允许重复的元素,按特定顺序排列(默认情况下按升序)

函数 描述 示例
multiset() 默认构造函数,创建一个空的 std::multiset std::multiset<int> ms;
multiset(const multiset& other) 拷贝构造函数。 std::multiset<int> ms2(ms1);
multiset(multiset&& other) 移动构造函数。 std::multiset<int> ms2(std::move(ms1));
multiset(initializer_list<T> il) 使用初始化列表创建 std::multiset std::multiset<int> ms = {1, 2, 3};
multiset(InputIterator first, InputIterator last) 用范围 [first, last) 的元素创建一个 std::multiset std::vector<int> v = {1, 2, 3}; std::multiset<int> ms(v.begin(), v.end());
operator= 拷贝赋值运算符。 ms2 = ms1;
operator= 移动赋值运算符。 ms2 = std::move(ms1);
~multiset() 析构函数。
begin() 返回指向容器第一个元素的迭代器。 auto it = ms.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = ms.end();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = ms.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = ms.cend();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = ms.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = ms.rend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = ms.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = ms.crend();
empty() 如果容器为空,则返回 true bool isEmpty = ms.empty();
size() 返回容器中元素的数量。 size_t sz = ms.size();
max_size() 返回容器中最大可能的元素数量。 size_t maxSz = ms.max_size();
clear() 移除所有元素。 ms.clear();
insert(const T& value) 插入元素的副本。 ms.insert(10);
insert(T&& value) 插入元素,通过移动语义。 ms.insert(std::move(value));
insert(initializer_list<T> il) 插入初始化列表中的所有元素。 ms.insert({1, 2, 3});
insert(iterator hint, const T& value) 插入元素的副本,使用迭代器 hint 作为插入位置的提示。 ms.insert(ms.begin(), 10);
insert(iterator hint, T&& value) 插入元素,通过移动语义,使用迭代器 hint 作为插入位置的提示。 ms.insert(ms.begin(), std::move(value));
insert(InputIterator first, InputIterator last) 插入范围 [first, last) 内的元素。 ms.insert(v.begin(), v.end());
erase(iterator pos) 移除指定位置的元素。 ms.erase(ms.begin());
erase(const T& value) 移除等于 value 的元素。 ms.erase(10);
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 ms.erase(ms.begin(), ms.end());
swap(multiset& other) 交换两个容器的内容。 ms1.swap(ms2);
count(const T& value) 返回等于 value 的元素数量。 size_t count = ms.count(10);
find(const T& value) 查找等于 value 的元素,返回指向该元素的迭代器。 auto it = ms.find(10);
lower_bound(const T& value) 返回指向第一个不小于 value 的元素的迭代器。 auto it = ms.lower_bound(10);
upper_bound(const T& value) 返回指向第一个大于 value 的元素的迭代器。 auto it = ms.upper_bound(10);
equal_range(const T& value) 返回一个范围,包括所有等于 value 的元素。 auto range = ms.equal_range(10);
key_comp() 返回一个比较函数对象,用于比较元素的键。 auto comp = ms.key_comp();
value_comp() 返回一个比较函数对象,用于比较元素的值。 auto comp = ms.value_comp();
emplace(args...) 在容器中原地构造元素。 ms.emplace(args...);
emplace_hint(iterator hint, args...) 在容器中原地构造元素,使用迭代器 hint 作为位置提示。 ms.emplace_hint(ms.begin(), args...);

Map

存储键值对,键唯一且按特定顺序排列(默认情况下按键的升序)

函数 描述 示例
map() 默认构造函数,创建一个空的 std::map std::map<int, int> m;
map(const map& other) 拷贝构造函数。 std::map<int, int> m2(m1);
map(map&& other) 移动构造函数。 std::map<int, int> m2(std::move(m1));
map(initializer_list<value_type> il) 使用初始化列表创建 std::map std::map<int, int> m = { {1, 2}, {3, 4}};
map(InputIterator first, InputIterator last) 用范围 [first, last) 的元素创建一个 std::map std::vector<std::pair<int, int>> v = { {1, 2}, {3, 4}}; std::map<int, int> m(v.begin(), v.end());
operator= 拷贝赋值运算符。 m2 = m1;
operator= 移动赋值运算符。 m2 = std::move(m1);
~map() 析构函数。
begin() 返回指向容器第一个元素的迭代器。 auto it = m.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = m.end();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = m.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = m.cend();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = m.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = m.rend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = m.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = m.crend();
empty() 如果容器为空,则返回 true bool isEmpty = m.empty();
size() 返回容器中元素的数量。 size_t sz = m.size();
max_size() 返回容器中最大可能的元素数量。 size_t maxSz = m.max_size();
clear() 移除所有元素。 m.clear();
insert(const value_type& value) 插入元素的副本。 m.insert(std::make_pair(1, 2));
insert(value_type&& value) 插入元素,通过移动语义。 m.insert(std::make_pair(1, 2));
insert(initializer_list<value_type> il) 插入初始化列表中的所有元素。 m.insert({ {1, 2}, {3, 4}});
insert(iterator hint, const value_type& value) 插入元素的副本,使用迭代器 hint 作为插入位置的提示。 m.insert(m.begin(), std::make_pair(1, 2));
insert(iterator hint, value_type&& value) 插入元素,通过移动语义,使用迭代器 hint 作为插入位置的提示。 m.insert(m.begin(), std::make_pair(1, 2));
insert(InputIterator first, InputIterator last) 插入范围 [first, last) 内的元素。 m.insert(v.begin(), v.end());
erase(iterator pos) 移除指定位置的元素。 m.erase(m.begin());
erase(const key_type& key) 移除指定键的元素。 m.erase(1);
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 m.erase(m.begin(), m.end());
swap(map& other) 交换两个容器的内容。 m1.swap(m2);
count(const key_type& key) 返回等于 key 的元素数量(对于 map 来说只能是 0 或 1)。 size_t count = m.count(1);
find(const key_type& key) 查找等于 key 的元素,返回指向该元素的迭代器。 auto it = m.find(1);
lower_bound(const key_type& key) 返回指向第一个不小于 key 的元素的迭代器。 auto it = m.lower_bound(1);
upper_bound(const key_type& key) 返回指向第一个大于 key 的元素的迭代器。 auto it = m.upper_bound(1);
equal_range(const key_type& key) 返回一个范围,包括所有等于 key 的元素。 auto range = m.equal_range(1);
key_comp() 返回一个比较函数对象,用于比较元素的键。 auto comp = m.key_comp();
value_comp() 返回一个比较函数对象,用于比较元素的值。 auto comp = m.value_comp();
emplace(args...) 在容器中原地构造元素。 m.emplace(args...);
emplace_hint(iterator hint, args...) 在容器中原地构造元素,使用迭代器 hint 作为位置提示。 m.emplace_hint(m.begin(), args...);
operator[] 如果键存在,返回键对应的值的引用。如果键不存在,插入该键,并返回该值的引用。 m[1] = 2;
at(const key_type& key) 返回键对应的值的引用,如果键不存在,抛出 out_of_range 异常。 int val = m.at(1);

Multimap

存储键值对,键允许重复且按特定顺序排列(默认情况下按键的升序)

函数 描述 示例
multimap() 默认构造函数,创建一个空的 std::multimap std::multimap<int, int> mm;
multimap(const multimap& other) 拷贝构造函数。 std::multimap<int, int> mm2(mm1);
multimap(multimap&& other) 移动构造函数。 std::multimap<int, int> mm2(std::move(mm1));
multimap(initializer_list<value_type> il) 使用初始化列表创建 std::multimap std::multimap<int, int> mm = { {1, 2}, {3, 4}};
multimap(InputIterator first, InputIterator last) 用范围 [first, last) 的元素创建一个 std::multimap std::vector<std::pair<int, int>> v = { {1, 2}, {3, 4}}; std::multimap<int, int> mm(v.begin(), v.end());
operator= 拷贝赋值运算符。 mm2 = mm1;
operator= 移动赋值运算符。 mm2 = std::move(mm1);
~multimap() 析构函数。
begin() 返回指向容器第一个元素的迭代器。 auto it = mm.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = mm.end();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = mm.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = mm.cend();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = mm.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = mm.rend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = mm.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = mm.crend();
empty() 如果容器为空,则返回 true bool isEmpty = mm.empty();
size() 返回容器中元素的数量。 size_t sz = mm.size();
max_size() 返回容器中最大可能的元素数量。 size_t maxSz = mm.max_size();
clear() 移除所有元素。 mm.clear();
insert(const value_type& value) 插入元素的副本。 mm.insert(std::make_pair(1, 2));
insert(value_type&& value) 插入元素,通过移动语义。 mm.insert(std::make_pair(1, 2));
insert(initializer_list<value_type> il) 插入初始化列表中的所有元素。 mm.insert({ {1, 2}, {3, 4}});
insert(iterator hint, const value_type& value) 插入元素的副本,使用迭代器 hint 作为插入位置的提示。 mm.insert(mm.begin(), std::make_pair(1, 2));
insert(iterator hint, value_type&& value) 插入元素,通过移动语义,使用迭代器 hint 作为插入位置的提示。 mm.insert(mm.begin(), std::make_pair(1, 2));
insert(InputIterator first, InputIterator last) 插入范围 [first, last) 内的元素。 mm.insert(v.begin(), v.end());
erase(iterator pos) 移除指定位置的元素。 mm.erase(mm.begin());
erase(const key_type& key) 移除指定键的所有元素。 mm.erase(1);
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 mm.erase(mm.begin(), mm.end());
swap(multimap& other) 交换两个容器的内容。 mm1.swap(mm2);
count(const key_type& key) 返回等于 key 的元素数量。 size_t count = mm.count(1);
find(const key_type& key) 查找等于 key 的元素,返回指向该元素的迭代器。 auto it = mm.find(1);
lower_bound(const key_type& key) 返回指向第一个不小于 key 的元素的迭代器。 auto it = mm.lower_bound(1);
upper_bound(const key_type& key) 返回指向第一个大于 key 的元素的迭代器。 auto it = mm.upper_bound(1);
equal_range(const key_type& key) 返回一个范围,包括所有等于 key 的元素。 auto range = mm.equal_range(1);
key_comp() 返回一个比较函数对象,用于比较元素的键。 auto comp = mm.key_comp();
value_comp() 返回一个比较函数对象,用于比较元素的值。 auto comp = mm.value_comp();
emplace(args...) 在容器中原地构造元素。 mm.emplace(args...);
emplace_hint(iterator hint, args...) 在容器中原地构造元素,使用迭代器 hint 作为位置提示。 mm.emplace_hint(mm.begin(), args...);

无序容器(Unordered Containers)

元素的顺序是基于哈希值而非元素的比较结果。无序容器通常提供平均O(1)时间复杂度的查找、插入和删除操作。

特点:

  • 无序存储:元素的顺序由哈希值决定,而不是元素本身的比较结果。
  • 哈希表实现:无序容器通常基于哈希表实现,因此具有平均O(1)的查找、插入和删除时间复杂度。
  • 负载因子和再散列:无序容器有负载因子的概念,当负载因子超过某个阈值时,容器会自动再散列以保持操作的效率。

Unordered Set

存储唯一的元素,使用哈希函数来决定元素的存储位置

函数 描述 示例
unordered_set() 默认构造函数,创建一个空的 std::unordered_set std::unordered_set<int> us;
unordered_set(size_type n) 创建一个空的 std::unordered_set,初始 bucket 数量为 n std::unordered_set<int> us(10);
unordered_set(size_type n, const hasher& hf) 创建一个空的 std::unordered_set,初始 bucket 数量为 n,并使用指定的哈希函数 hf std::unordered_set<int> us(10, std::hash<int>());
unordered_set(size_type n, const hasher& hf, const key_equal& eql) 创建一个空的 std::unordered_set,初始 bucket 数量为 n,并使用指定的哈希函数 hf 和相等比较函数 eql std::unordered_set<int> us(10, std::hash<int>(), std::equal_to<int>());
unordered_set(InputIterator first, InputIterator last) 用范围 [first, last) 的元素创建一个 std::unordered_set std::vector<int> v = {1, 2, 3}; std::unordered_set<int> us(v.begin(), v.end());
unordered_set(const unordered_set& other) 拷贝构造函数。 std::unordered_set<int> us2(us1);
unordered_set(unordered_set&& other) 移动构造函数。 std::unordered_set<int> us2(std::move(us1));
unordered_set(initializer_list<value_type> il) 使用初始化列表创建 std::unordered_set std::unordered_set<int> us = {1, 2, 3};
operator= 拷贝赋值运算符。 us2 = us1;
operator= 移动赋值运算符。 us2 = std::move(us1);
~unordered_set() 析构函数。
begin() 返回指向容器第一个元素的迭代器。 auto it = us.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = us.end();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = us.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = us.cend();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = us.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = us.rend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = us.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = us.crend();
empty() 如果容器为空,则返回 true bool isEmpty = us.empty();
size() 返回容器中元素的数量。 size_t sz = us.size();
max_size() 返回容器中最大可能的元素数量。 size_t maxSz = us.max_size();
clear() 移除所有元素。 us.clear();
insert(const value_type& value) 插入元素的副本。 us.insert(10);
insert(value_type&& value) 插入元素,通过移动语义。 us.insert(std::move(value));
insert(initializer_list<value_type> il) 插入初始化列表中的所有元素。 us.insert({1, 2, 3});
insert(InputIterator first, InputIterator last) 插入范围 [first, last) 内的元素。 us.insert(v.begin(), v.end());
erase(iterator pos) 移除指定位置的元素。 us.erase(us.begin());
erase(const key_type& key) 移除指定键的元素。 us.erase(1);
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 us.erase(us.begin(), us.end());
swap(unordered_set& other) 交换两个容器的内容。 us1.swap(us2);
count(const key_type& key) 返回等于 key 的元素数量(对于 unordered_set 来说只能是 0 或 1)。 size_t count = us.count(1);
find(const key_type& key) 查找等于 key 的元素,返回指向该元素的迭代器。 auto it = us.find(1);
equal_range(const key_type& key) 返回一个范围,包括所有等于 key 的元素。 auto range = us.equal_range(1);
bucket_count() 返回 bucket 的数量。 size_t n = us.bucket_count();
max_bucket_count() 返回最大的 bucket 数量。 size_t n = us.max_bucket_count();
bucket_size(size_type n) 返回指定 bucket 中的元素数量。 size_t n = us.bucket_size(0);
bucket(const key_type& key) 返回存储指定键的 bucket 的编号。 size_t n = us.bucket(1);
load_factor() 返回负载因子。 float lf = us.load_factor();
max_load_factor() 返回或设置最大负载因子。 us.max_load_factor(1.0);
rehash(size_type n) 重哈希容器,使得 bucket 数量不少于 n us.rehash(20);
reserve(size_type n) 预留存储,使得容器可以容纳 n 个元素而不进行重哈希。 us.reserve(20);
hash_function() 返回哈希函数。 auto hf = us.hash_function();
key_eq() 返回键比较函数。 auto ke = us.key_eq();
emplace(args...) 在容器中原地构造元素。 us.emplace(args...);
emplace_hint(const_iterator hint, args...) 在容器中原地构造元素,使用迭代器 hint 作为位置提示。 us.emplace_hint(us.begin(), args...);

注意:std::unordered_set 是一个无序集合,使用哈希函数来组织元素。默认情况下,使用 std::hash 来计算键的哈希值,并使用 operator== 比较键的相等性。可以通过在模板参数中指定自定义哈希函数和比较函数来改变默认行为。

Unordered Multiset

存储允许重复的元素,使用哈希函数来决定元素的存储位置

函数 描述 示例
unordered_multiset() 默认构造函数,创建一个空的 std::unordered_multiset std::unordered_multiset<int> ums;
unordered_multiset(size_type n) 创建一个空的 std::unordered_multiset,初始 bucket 数量为 n std::unordered_multiset<int> ums(10);
unordered_multiset(size_type n, const hasher& hf) 创建一个空的 std::unordered_multiset,初始 bucket 数量为 n,并使用指定的哈希函数 hf std::unordered_multiset<int> ums(10, std::hash<int>());
unordered_multiset(size_type n, const hasher& hf, const key_equal& eql) 创建一个空的 std::unordered_multiset,初始 bucket 数量为 n,并使用指定的哈希函数 hf 和相等比较函数 eql std::unordered_multiset<int> ums(10, std::hash<int>(), std::equal_to<int>());
unordered_multiset(InputIterator first, InputIterator last) 用范围 [first, last) 的元素创建一个 std::unordered_multiset std::vector<int> v = {1, 2, 3}; std::unordered_multiset<int> ums(v.begin(), v.end());
unordered_multiset(const unordered_multiset& other) 拷贝构造函数。 std::unordered_multiset<int> ums2(ums1);
unordered_multiset(unordered_multiset&& other) 移动构造函数。 std::unordered_multiset<int> ums2(std::move(ums1));
unordered_multiset(initializer_list<value_type> il) 使用初始化列表创建 std::unordered_multiset std::unordered_multiset<int> ums = {1, 2, 3};
operator= 拷贝赋值运算符。 ums2 = ums1;
operator= 移动赋值运算符。 ums2 = std::move(ums1);
~unordered_multiset() 析构函数。
begin() 返回指向容器第一个元素的迭代器。 auto it = ums.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = ums.end();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = ums.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = ums.cend();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = ums.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = ums.rend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = ums.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = ums.crend();
empty() 如果容器为空,则返回 true bool isEmpty = ums.empty();
size() 返回容器中元素的数量。 size_t sz = ums.size();
max_size() 返回容器中最大可能的元素数量。 size_t maxSz = ums.max_size();
clear() 移除所有元素。 ums.clear();
insert(const value_type& value) 插入元素的副本。 ums.insert(10);
insert(value_type&& value) 插入元素,通过移动语义。 ums.insert(std::move(value));
insert(initializer_list<value_type> il) 插入初始化列表中的所有元素。 ums.insert({1, 2, 3});
insert(InputIterator first, InputIterator last) 插入范围 [first, last) 内的元素。 ums.insert(v.begin(), v.end());
erase(iterator pos) 移除指定位置的元素。 ums.erase(ums.begin());
erase(const key_type& key) 移除所有等于指定键的元素。 ums.erase(1);
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 ums.erase(ums.begin(), ums.end());
swap(unordered_multiset& other) 交换两个容器的内容。 ums1.swap(ums2);
count(const key_type& key) 返回等于 key 的元素数量。 size_t count = ums.count(1);
find(const key_type& key) 查找等于 key 的元素,返回指向该元素的迭代器。 auto it = ums.find(1);
equal_range(const key_type& key) 返回一个范围,包括所有等于 key 的元素。 auto range = ums.equal_range(1);
bucket_count() 返回 bucket 的数量。 size_t n = ums.bucket_count();
max_bucket_count() 返回最大的 bucket 数量。 size_t n = ums.max_bucket_count();
bucket_size(size_type n) 返回指定 bucket 中的元素数量。 size_t n = ums.bucket_size(0);
bucket(const key_type& key) 返回存储指定键的 bucket 的编号。 size_t n = ums.bucket(1);
load_factor() 返回负载因子。 float lf = ums.load_factor();
max_load_factor() 返回或设置最大负载因子。 ums.max_load_factor(1.0);
rehash(size_type n) 重哈希容器,使得 bucket 数量不少于 n ums.rehash(20);
reserve(size_type n) 预留存储,使得容器可以容纳 n 个元素而不进行重哈希。 ums.reserve(20);
hash_function() 返回哈希函数。 auto hf = ums.hash_function();
key_eq() 返回键比较函数。 auto ke = ums.key_eq();
emplace(args...) 在容器中原地构造元素。 ums.emplace(args...);
emplace_hint(const_iterator hint, args...) 在容器中原地构造元素,使用迭代器 hint 作为位置提示。 ums.emplace_hint(ums.begin(), args...);

注意:std::unordered_multiset 是一个无序集合,允许多个元素拥有相同的键,使用哈希函数来组织元素。默认情况下,使用 std::hash 来计算键的哈希值,并使用 operator== 比较键的相等性。可以通过在模板参数中指定自定义哈希函数和比较函数来改变默认行为。

Unordered Map

存储键值对,键唯一,使用哈希函数来决定键的存储位置

函数 描述 示例
unordered_map() 默认构造函数,创建一个空的 std::unordered_map std::unordered_map<int, int> um;
unordered_map(size_type n) 创建一个空的 std::unordered_map,初始 bucket 数量为 n std::unordered_map<int, int> um(10);
unordered_map(size_type n, const hasher& hf) 创建一个空的 std::unordered_map,初始 bucket 数量为 n,并使用指定的哈希函数 hf std::unordered_map<int, int> um(10, std::hash<int>());
unordered_map(size_type n, const hasher& hf, const key_equal& eql) 创建一个空的 std::unordered_map,初始 bucket 数量为 n,并使用指定的哈希函数 hf 和相等比较函数 eql std::unordered_map<int, int> um(10, std::hash<int>(), std::equal_to<int>());
unordered_map(InputIterator first, InputIterator last) 用范围 [first, last) 的元素创建一个 std::unordered_map std::vector<std::pair<int, int>> v = { {1, 2}, {3, 4}}; std::unordered_map<int, int> um(v.begin(), v.end());
unordered_map(const unordered_map& other) 拷贝构造函数。 std::unordered_map<int, int> um2(um1);
unordered_map(unordered_map&& other) 移动构造函数。 std::unordered_map<int, int> um2(std::move(um1));
unordered_map(initializer_list<value_type> il) 使用初始化列表创建 std::unordered_map std::unordered_map<int, int> um = { {1, 2}, {3, 4}};
operator= 拷贝赋值运算符。 um2 = um1;
operator= 移动赋值运算符。 um2 = std::move(um1);
~unordered_map() 析构函数。
begin() 返回指向容器第一个元素的迭代器。 auto it = um.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = um.end();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = um.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = um.cend();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = um.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = um.rend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = um.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = um.crend();
empty() 如果容器为空,则返回 true bool isEmpty = um.empty();
size() 返回容器中元素的数量。 size_t sz = um.size();
max_size() 返回容器中最大可能的元素数量。 size_t maxSz = um.max_size();
clear() 移除所有元素。 um.clear();
insert(const value_type& value) 插入元素的副本。 um.insert(std::make_pair(1, 2));
insert(value_type&& value) 插入元素,通过移动语义。 um.insert(std::make_pair(1, 2));
insert(initializer_list<value_type> il) 插入初始化列表中的所有元素。 um.insert({ {1, 2}, {3, 4}});
insert(iterator hint, const value_type& value) 插入元素的副本,使用迭代器 hint 作为插入位置的提示。 um.insert(um.begin(), std::make_pair(1, 2));
insert(iterator hint, value_type&& value) 插入元素,通过移动语义,使用迭代器 hint 作为插入位置的提示。 um.insert(um.begin(), std::make_pair(1, 2));
insert(InputIterator first, InputIterator last) 插入范围 [first, last) 内的元素。 um.insert(v.begin(), v.end());
erase(iterator pos) 移除指定位置的元素。 um.erase(um.begin());
erase(const key_type& key) 移除指定键的元素。 um.erase(1);
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 um.erase(um.begin(), um.end());
swap(unordered_map& other) 交换两个容器的内容。 um1.swap(um2);
at(const key_type& key) 返回键对应的值的引用,如果键不存在,抛出 out_of_range 异常。 int val = um.at(1);
operator[] 如果键存在,返回键对应的值的引用。如果键不存在,插入该键,并返回该值的引用。 um[1] = 2;
count(const key_type& key) 返回等于 key 的元素数量(对于 unordered_map 来说只能是 0 或 1)。 size_t count = um.count(1);
find(const key_type& key) 查找等于 key 的元素,返回指向该元素的迭代器。 auto it = um.find(1);
equal_range(const key_type& key) 返回一个范围,包括所有等于 key 的元素。 auto range = um.equal_range(1);
bucket_count() 返回 bucket 的数量。 size_t n = um.bucket_count();
max_bucket_count() 返回最大的 bucket 数量。 size_t n = um.max_bucket_count();
bucket_size(size_type n) 返回指定 bucket 中的元素数量。 size_t n = um.bucket_size(0);
bucket(const key_type& key) 返回存储指定键的 bucket 的编号。 size_t n = um.bucket(1);
load_factor() 返回负载因子。 float lf = um.load_factor();
max_load_factor() 返回或设置最大负载因子。 um.max_load_factor(1.0);
rehash(size_type n) 重哈希容器,使得 bucket 数量不少于 n um.rehash(20);
reserve(size_type n) 预留存储,使得容器可以容纳 n 个元素而不进行重哈希。 um.reserve(20);
hash_function() 返回哈希函数。 auto hf = um.hash_function();
key_eq() 返回键比较函数。 auto ke = um.key_eq();
emplace(args...) 在容器中原地构造元素。 um.emplace(args...);
emplace_hint(const_iterator hint, args...) 在容器中原地构造元素,使用迭代器 hint 作为位置提示。 um.emplace_hint(um.begin(), args...);

注意:std::unordered_map 是一个无序的关联容器,使用哈希函数来组织元素。默认情况下,使用 std::hash 来计算键的哈希值,并使用 `operator

Unordered Multimap

存储键值对,键允许重复,使用哈希函数来决定键的存储位置

函数 描述 示例
unordered_multimap() 默认构造函数,创建一个空的 std::unordered_multimap std::unordered_multimap<int, int> umm;
unordered_multimap(size_type n) 创建一个空的 std::unordered_multimap,初始 bucket 数量为 n std::unordered_multimap<int, int> umm(10);
unordered_multimap(size_type n, const hasher& hf) 创建一个空的 std::unordered_multimap,初始 bucket 数量为 n,并使用指定的哈希函数 hf std::unordered_multimap<int, int> umm(10, std::hash<int>());
unordered_multimap(size_type n, const hasher& hf, const key_equal& eql) 创建一个空的 std::unordered_multimap,初始 bucket 数量为 n,并使用指定的哈希函数 hf 和相等比较函数 eql std::unordered_multimap<int, int> umm(10, std::hash<int>(), std::equal_to<int>());
unordered_multimap(InputIterator first, InputIterator last) 用范围 [first, last) 的元素创建一个 std::unordered_multimap std::vector<std::pair<int, int>> v = { {1, 2}, {3, 4}}; std::unordered_multimap<int, int> umm(v.begin(), v.end());
unordered_multimap(const unordered_multimap& other) 拷贝构造函数。 std::unordered_multimap<int, int> umm2(umm1);
unordered_multimap(unordered_multimap&& other) 移动构造函数。 std::unordered_multimap<int, int> umm2(std::move(umm1));
unordered_multimap(initializer_list<value_type> il) 使用初始化列表创建 std::unordered_multimap std::unordered_multimap<int, int> umm = { {1, 2}, {3, 4}};
operator= 拷贝赋值运算符。 umm2 = umm1;
operator= 移动赋值运算符。 umm2 = std::move(umm1);
~unordered_multimap() 析构函数。
begin() 返回指向容器第一个元素的迭代器。 auto it = umm.begin();
end() 返回指向容器最后一个元素之后的迭代器。 auto it = umm.end();
cbegin() 返回指向容器第一个元素的常量迭代器。 auto it = umm.cbegin();
cend() 返回指向容器最后一个元素之后的常量迭代器。 auto it = umm.cend();
rbegin() 返回指向容器最后一个元素的反向迭代器。 auto it = umm.rbegin();
rend() 返回指向容器第一个元素之前的反向迭代器。 auto it = umm.rend();
crbegin() 返回指向容器最后一个元素的常量反向迭代器。 auto it = umm.crbegin();
crend() 返回指向容器第一个元素之前的常量反向迭代器。 auto it = umm.crend();
empty() 如果容器为空,则返回 true bool isEmpty = umm.empty();
size() 返回容器中元素的数量。 size_t sz = umm.size();
max_size() 返回容器中最大可能的元素数量。 size_t maxSz = umm.max_size();
clear() 移除所有元素。 umm.clear();
insert(const value_type& value) 插入元素的副本。 umm.insert(std::make_pair(1, 2));
insert(value_type&& value) 插入元素,通过移动语义。 umm.insert(std::make_pair(1, 2));
insert(initializer_list<value_type> il) 插入初始化列表中的所有元素。 umm.insert({ {1, 2}, {3, 4}});
insert(iterator hint, const value_type& value) 插入元素的副本,使用迭代器 hint 作为插入位置的提示。 umm.insert(umm.begin(), std::make_pair(1, 2));
insert(iterator hint, value_type&& value) 插入元素,通过移动语义,使用迭代器 hint 作为插入位置的提示。 umm.insert(umm.begin(), std::make_pair(1, 2));
insert(InputIterator first, InputIterator last) 插入范围 [first, last) 内的元素。 umm.insert(v.begin(), v.end());
erase(iterator pos) 移除指定位置的元素。 umm.erase(umm.begin());
erase(const key_type& key) 移除指定键的元素。 umm.erase(1);
erase(iterator first, iterator last) 移除指定范围 [first, last) 内的元素。 umm.erase(umm.begin(), umm.end());
swap(unordered_multimap& other) 交换两个容器的内容。 umm1.swap(umm2);
count(const key_type& key) 返回等于 key 的元素数量。 size_t count = umm.count(1);
find(const key_type& key) 查找等于 key 的元素,返回指向该元素的迭代器。 auto it = umm.find(1);
equal_range(const key_type& key) 返回一个范围,包括所有等于 key 的元素。 auto range = umm.equal_range(1);
bucket_count() 返回 bucket 的数量。 size_t n = umm.bucket_count();
max_bucket_count() 返回最大的 bucket 数量。 size_t n = umm.max_bucket_count();
bucket_size(size_type n) 返回指定 bucket 中的元素数量。 size_t n = umm.bucket_size(0);
bucket(const key_type& key) 返回存储指定键的 bucket 的编号。 size_t n = umm.bucket(1);
load_factor() 返回负载因子。 float lf = umm.load_factor();
max_load_factor() 返回或设置最大负载因子。 umm.max_load_factor(1.0);
rehash(size_type n) 重哈希容器,使得 bucket 数量不少于 n umm.rehash(20);
reserve(size_type n) 预留存储,使得容器可以容纳 n 个元素而不进行重哈希。 umm.reserve(20);
hash_function() 返回哈希函数。 auto hf = umm.hash_function();
key_eq() 返回键比较函数。 auto ke = umm.key_eq();
emplace(args...) 在容器中原地构造元素。 umm.emplace(args...);
emplace_hint(const_iterator hint, args...) 在容器中原地构造元素,使用迭代器 hint 作为位置提示。 umm.emplace_hint(umm.begin(), args...);

注意:std::unordered_multimap 是一个无序的关联容器,允许多个元素拥有相同的键,使用哈希函数来组织元素。默认情况下,使用 std::hash 来计算键的哈希值,并使用 operator== 比较键的相等性。可以通过在模板参数中指定自定义哈希函数和比较函数来改变默认行为。

猜你喜欢

转载自blog.csdn.net/Leon_Chenl/article/details/140312972
今日推荐