文章目录
- 9.5 顺序容器的所有操作
- 9.5.1 vector
- 9.5.2 string
- 9.5.3 deque
- 9.5.4 list
- 9.5.5 forward_list
- 9.5.6 array
9.5 顺序容器的所有操作
9.5.1 vector
vector 将元素连续存储,每个元素紧挨着前一个元素存储。
vector 预留一些空间作为备用,用来保存更多的新元素。当需求大小大于当前容量时,才会调用 reserve 来改变其容量,每次将容量增加至原来的 2 倍。
vector 的扩张操作通常比 list 和 deque 还要快。
| 成员 | 说明 |
|---|---|
| 成员类型 | |
| value_type | 元素类型 T |
| allocator_type | 分配器类型 |
| reference / const_reference | value_type& / const value_type& |
| pointer / const_pointer | 指针 / 常量指针 |
| iterator / const_iterator | 迭代器 / 常量迭代器 |
| reverse_iterator / const_reverse_iterator | 反向迭代器 / 常量反向迭代器 |
| size_type / difference_type | 元素个数的类型 / 迭代器距离的类型 |
| 获取迭代器 | |
| iterator begin() / rbegin() noexcept; const_iterator begin() / rbegin() const noexcept; | 返回指向首元素 / 尾元素的迭代器 |
| iterator end() / rend() noexcept; const_iterator end() / rend() const noexcept; | 返回指向尾后元素 / 首前元素的迭代器 |
| const_iterator cbegin() / crbegin() const noexcept; | 返回指向首元素 / 尾元素的常量迭代器 |
| const_iterator cend() / crend() const noexcept; | 返回指向尾后元素 / 首前元素的常量迭代器 |
| 构造函数 | |
| explicit vector (const allocator_type& alloc = allocator_type()); | 默认构造函数 |
| explicit vector (size_type n); | 指定容器大小为 n |
| vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type()); | 指定容器大小为 n,每个值都为 val |
| template vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); | 顺序构建与给定范围一样元素的容器 |
| vector (const vector& x); vector (vector&& x); | 复制 vector x |
| vector (const vector& x, const allocator_type& alloc); vector (vector&& x, const allocator_type& alloc); | 复制 vector x,并指定分配器 alloc |
| vector (const vector& x, const allocator_type& alloc); vector (initializer_list il, const allocator_type& alloc = allocator_type()); | 使用列表构建元素 |
| 赋值运算符 | |
| vector& operator= (const vector& x); vector& operator= (vector&& x); | 以 vector x 的内容进行赋值 |
| vector& operator= (initializer_list il); | 用列表元素进行赋值 |
| 容量函数 | |
| size_type size() const noexcept; | 返回 vector 大小 |
| size_type max_size() const noexcept; | 返回所能容纳的最大元素数量 |
| void resize (size_type n); void resize (size_type n, const value_type& val); | 调整容器大小,使其包含 n 个元素 若 n 大于当前大小,则进行扩容,指定扩展部分的值为 val 若 n 小于当前大小,删除尾部多出的元素 |
| size_type capacity() const noexcept; | 返回目前分配的存储空间的大小 |
| bool empty() const noexcept; | 判断 vector 是否为空 |
| void reserve (size_type n); | 使其容量增加到 n |
| void shrink_to_fit(); | 要求容器降低容量以适应其大小 |
| 获取元素 | |
| reference operator[] (size_type n); const_reference operator[] (size_type n) const; | 返回第 n 个位置的元素引用, 越界行为未定义 |
| reference at (size_type n); const_reference at (size_type n) const; | 返回第 n 个位置的元素引用, 越界会抛出异常 |
| reference front(); const_reference front() const; | 返回第一个元素的引用 |
| reference back(); const_reference back() const; | 返回最后一个元素的引用 |
| value_type* data() noexcept; const value_type* data() const noexcept; | 返回一个指针,该指针指向内部用于存储元素的内存数组 |
| 修改容器 | |
| template void assign (InputIterator first, InputIterator last); void assign (size_type n, const value_type& val); void assign (initializer_list il); | 赋值操作,分别对 vector 进行 范围 / n 个 val 值 / 列表 赋值 |
| void push_back (const value_type& val); void push_back (value_type&& val); | 在末尾添加元素 val |
| void pop_back(); | 删除末尾元素 |
| iterator insert (const_iterator position, const value_type& val); iterator insert (const_iterator position, value_type&& val); iterator insert (const_iterator position, size_type n, const value_type& val); template iterator insert (const_iterator position, InputIterator first, InputIterator last); iterator insert (const_iterator position, initializer_list il); | 在迭代器 position 位置之前,分别插入 一个 val 值 / n 个 val 值 / 范围 / 列表值 |
| iterator erase (const_iterator position); iterator erase (const_iterator first, const_iterator last); | 移除迭代器所给定的位置 / 范围 |
| void swap (vector& x); | 与 vector x 交换内容 |
| void clear() noexcept; | 清除所有的内容 |
| template iterator emplace (const_iterator position, Args&&… args); | 在迭代器 position 之前插入元素,该元素由参数 args 通过构造函数进行构造 |
| template void emplace_back (Args&&… args); | 在末尾插入元素,该元素由参数 args 通过构造函数进行构造 |
| 获取分配器器 | |
| allocator_type get_allocator() const noexcept; | 返回分配器对象的副本 |
| 重载的非成员函数 | 说明 |
|---|---|
| template bool operator _op (const vector& lhs, const vector& rhs); | 重载 6 个关系运算符, _ op 可以是 =,==,!= |
| template void swap (vector& x, vector& y); | 交换 x 与 y 的内容 |
9.5.2 string
| 成员 | 说明 |
|---|---|
| 成员类型 | |
| value_type | char |
| traits_type | char_traits |
| allocator_type | allocator |
| reference / const_reference | char& / const char& |
| pointer / const_pointer | char* / const char* |
| iterator / const_iterator | 迭代器 / 常量迭代器 |
| reverse_iterator / const_reverse_iterator | 反向迭代器 / 常量反向迭代器 |
| size_type / difference_type | ptrdiff_t / size_t |
| npos | static const size_t npos = -1; |
| 获取迭代器 | |
| iterator begin() / rbegin() noexcept; const_iterator begin() / rbegin() const noexcept; | 返回指向首元素 / 尾元素的迭代器 |
| iterator end() / rend() noexcept; const_iterator end() / rend() const noexcept; | 返回指向尾后元素 / 首前元素的迭代器 |
| const_iterator cbegin() / crbegin() const noexcept; | 返回指向首元素 / 尾元素的常量迭代器 |
| const_iterator cend() / crend() const noexcept; | 返回指向尾后元素 / 首前元素的常量迭代器 |
| 构造函数 | |
| string(); | 默认构造函数 |
| string (const string& str); | 拷贝构造函数 |
| string (const string& str, size_t pos, size_t len = npos); | 复制从字符位置开始的 str 部分,该部分将跨越 len 字符 |
| string (const char* s); | 复制由 s (C 字符串)指出的空终止字符序列 |
| string (const char* s, size_t n); | 复制由 s (C 字符串)指出的前 n 个字符序列 |
| string (size_t n, char c); | 构造连续 n 个 c 的string |
| template string (InputIterator first, InputIterator last); | 以相同的顺序复制范围中的字符序列 |
| string (initializer_list il); | 以相同的顺序复制每个字符 |
| string (string&& str) noexcept; | 移动构造函数 |
| 赋值运算符 | |
| string& operator= (const string& str); | 分配新值,内容由 str 给出 |
| string& operator= (const char* s); | 分配新值,内容由 s 指出的空终止字符序列 给出 |
| string& operator= (char c); | 分配新值,内容为单个字符 c |
| string& operator= (initializer_list il); | 分配新值,内容由列表 il 给出 |
| string& operator= (string&& str) noexcept; | 分配新值,内容由 str 给出 |
| 容量函数 | |
| size_t size() const noexcept; | 按字节返回字符串的长度 |
| size_t length() const noexcept; | 按字节返回字符串的长度 |
| size_t max_size() const noexcept; | 返回字符串可以达到的最大长度 |
| void resize (size_type n); void resize (size_type n, char c); | 将字符串调整为 n 字符的长度 若 n 大于当前长度,则在末尾插入 c 字符,默认为空字符 若 n 小于当前长度,则截断后面多出的字符 |
| size_t capacity() const noexcept; | 返回目前分配的存储空间的大小 |
| bool empty() const noexcept; | 判断 string 是否为空 |
| void reserve (size_t n = 0); | 使其容量增加到 n |
| void shrink_to_fit(); | 要求字符串降低容量以适应其大小 |
| 获取元素 | |
| char& operator[] (size_t pos); const char& operator[] (size_t pos) const; | 返回第 pos 个字符的引用, 越界行为未定义 |
| char& at (size_t pos); const char& at (size_t pos) const; | 返回第 pos 个字符的引用, 越界会抛出异常 |
| char& front(); const char& front() const; | 返回第一个元素的引用 |
| char& back(); const char& back() const; | 返回最后一个元素的引用 |
| const char* data() const noexcept; | 返回指向字符串对象当前用于存储符合其值的字符的内部数组。 功能同 c_str() |
| 修改字符串 | |
| string& operator+= (const string& str); string& operator+= (const char* s); string& operator+= (char c); string& operator+= (initializer_list il); | 在其末尾附加字符 / 字符串 |
| string& append (const string& str); string& append (const string& str, size_t subpos, size_t sublen); string& append (const char* s); string& append (const char* s, size_t n); string& append (size_t n, char c); template string& append (InputIterator first, InputIterator last); string& append (initializer_list il); | 在其末尾附加字符 / 字符串 |
| void push_back (char c); | 将字符 c 附在字符串的末尾,将其长度增加一个 |
| string& assign (const string& str); string& assign (const string& str, size_t subpos, size_t sublen); string& assign (const char* s); string& assign (const char* s, size_t n); string& assign (size_t n, char c); template string& assign (InputIterator first, InputIterator last); string& assign (initializer_list il); string& assign (string&& str) noexcept; | 为字符串分配新值,以取代其当前内容 |
| string& insert (size_t pos, const string& str); string& insert (size_t pos, const string& str, size_t subpos, size_t sublen); string& insert (size_t pos, const char* s); string& insert (size_t pos, const char* s, size_t n); string& insert (size_t pos, size_t n, char c); iterator insert (const_iterator p, size_t n, char c); iterator insert (const_iterator p, char c); template iterator insert (iterator p, InputIterator first, InputIterator last); string& insert (const_iterator p, initializer_list il); | 在 pos(或 p)指示的字符之前,插入字符 / 字符串 |
| string& erase (size_t pos = 0, size_t len = npos); iterator erase (const_iterator p); iterator erase (const_iterator first, const_iterator last); | 擦除字符串的一部分 |
| string& replace (size_t pos, size_t len, const string& str); string& replace (const_iterator i1, const_iterator i2, const string& str); string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); string& replace (size_t pos, size_t len, const char* s); string& replace (const_iterator i1, const_iterator i2, const char* s); string& replace (size_t pos, size_t len, const char* s, size_t n); string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); string& replace (size_t pos, size_t len, size_t n, char c); string& replace (const_iterator i1, const_iterator i2, size_t n, char c); template string& replace (const_iterator i1, const_iterator i2, InputIterator first, InputIterator last); string& replace (const_iterator i1, const_iterator i2, initializer_list il); | 以新内容替换从字符 pos 开始的字符部分,并按新内容跨度字符 |
| void clear() noexcept; | 擦除字符串的内容,变为空串 |
| void swap (string& str); | 与 str 交换内容 |
| void pop_back(); | 擦除最后一个字符, 若字符串为空,则行为未定义 |
| string 操作 | |
| const char* c_str() const noexcept; | 返回指向字符串对象当前用于存储符合其值的字符的内部数组。 功能同 data() |
| size_t copy (char* s, size_t len, size_t pos = 0) const; | 从 s 中拷贝指定位置 pos 开始的所有字符 |
| size_t find (const string& str, size_t pos = 0) const noexcept; size_t find (const char* s, size_t pos = 0) const; size_t find (const char* s, size_t pos, size_type n) const; size_t find (char c, size_t pos = 0) const noexcept; | 顺序搜索首次出现的字符 / 字符串: pos:指定位置开始 n:匹配字符序列的长度 |
| size_t rfind (const string& str, size_t pos = npos) const noexcept; size_t rfind (const char* s, size_t pos = npos) const; size_t rfind (const char* s, size_t pos, size_t n) const; size_t rfind (char c, size_t pos = npos) const noexcept; | 逆序搜索首次出现的字符 / 字符串: pos:指定位置开始 n:匹配字符序列的长度 |
| size_t find_first_of (const string& str, size_t pos = 0) const noexcept; size_t find_first_of (const char* s, size_t pos = 0) const; size_t find_first_of (const char* s, size_t pos, size_t n) const; size_t find_first_of (char c, size_t pos = 0) const noexcept; | 返回字符串中出现的第一个包含在 str / s / c 中的字符位置: pos:指定位置开始 n:匹配字符序列的长度 |
| size_t find_last_of (const string& str, size_t pos = npos) const noexcept; size_t find_last_of (const char* s, size_t pos = npos) const; size_t find_last_of (const char* s, size_t pos, size_t n) const; size_t find_last_of (char c, size_t pos = npos) const noexcept; | 返回字符串中出现的最后一个包含在 str / s / c 中的字符位置: pos:指定位置开始 n:匹配字符序列的长度 |
| size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept; size_t find_first_not_of (const char* s, size_t pos = 0) const; size_t find_first_not_of (const char* s, size_t pos, size_t n) const; size_t find_first_not_of (char c, size_t pos = 0) const noexcept; | 返回字符串中出现的第一个不包含在 str / s / c 中的字符位置: pos:指定位置开始 n:匹配字符序列的长度 |
| size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept; size_t find_last_not_of (const char* s, size_t pos = npos) const; size_t find_last_not_of (const char* s, size_t pos, size_t n) const; size_t find_last_not_of (char c, size_t pos = npos) const noexcept; | 返回字符串中出现的最后一个不包含在 str / s / c 中的字符位置: pos:指定位置开始 n:匹配字符序列的长度 |
| string substr (size_t pos = 0, size_t len = npos) const; | 返回起始位置为 pos,长度为 len 的子串。 若超出范围,则返回 pos 后所有字符组成的 string |
| int compare (const string& str) const noexcept; int compare (size_t pos, size_t len, const string& str) const; int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const; int compare (const char* s) const; int compare (size_t pos, size_t len, const char* s) const; int compare (size_t pos, size_t len, const char* s, size_t n) const; | 如果相等则返回 0,比给定字符串大则返回 1,比给定字符串小则返回 -1 |
| 获取分配器 | |
| allocator_type get_allocator() const noexcept; | 返回分配器对象的副本 |
| 非成员函数 | 说明 |
|---|---|
| 重载函数 | |
| string operator+ (const string& lhs, const string& rhs); string operator+ (string&& lhs, string&& rhs); string operator+ (string&& lhs, const string& rhs); string operator+ (const string& lhs, string&& rhs); string operator+ (const string& lhs, const char* rhs); string operator+ (string&& lhs, const char* rhs); string operator+ (const char* lhs, const string& rhs); string operator+ (const char* lhs, string&& rhs); string operator+ (const string& lhs, char rhs); string operator+ (string&& lhs, char rhs); string operator+ (char lhs, const string& rhs); string operator+ (char lhs, string&& rhs); | 重载 + 运算符,连接字符串 / 字符 |
| bool operator_op (const string& lhs, const string& rhs); bool operator_op (const char* lhs, const string& rhs); bool operator_op (const string& lhs, const char* rhs); | 重载 6 个关系运算符, _op 可以是 =,==,!= |
| void swap (string& x, string& y); | 交换 x 和 y 的内容 |
| istream& operator>> (istream& is, string& str); ostream& operator 关注
打赏
热门博文
立即登录/注册
微信扫码登录 |
