顺序容器常用用法

vector

构造
//default (1)	
explicit vector (const allocator_type& alloc = allocator_type());
//fill (2)	
explicit vector (size_type n);
         vector (size_type n, const value_type& val,
                 const allocator_type& alloc = allocator_type());
//range (3)	
template <class InputIterator>
  vector (InputIterator first, InputIterator last,
          const allocator_type& alloc = allocator_type());
//copy (4)	
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
//move (5)	
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
//initializer list (6)	
vector (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());
迭代器
//iterator begin,end
//reverse_iterator rbegin,rend
//const_iterator cbegin,cend
//const_reverse_iterator crbegin,crend
元素访问
reference operator[] (size_type n); //不会进行边界检查
reference at (size_type n);//会进行边界检查
reference front();
reference back();
value_type* data() noexcept;  //返回指向vertor用于存储元素的数组的指针
容量
size_type size() const;
void resize (size_type n, value_type val = value_type()); //resize不会修改原有元素
/*
reserve(n)
确保capacity 至少为n, 没调用reserve时, 容量的扩张规律是:每次size刚超过容量的一半时,容量倍增。调用过reserve后,容量只有在size超过当前容量时,才倍增
*/
void reserve (size_type n);
bool empty() const noexcept;
void shrink_to_fit(); //使capcity等于size
修改
//assign
//range (1)	
template <class InputIterator>
void assign (InputIterator first, InputIterator last);
//fill (2)	
void assign (size_type n, const value_type& val);
//initializer list (3)	
void assign (initializer_list<value_type> il);

//insert
//single element (1)	
iterator insert (const_iterator position, const value_type& val);
//fill (2)	
iterator insert (const_iterator position, size_type n, const value_type& val);
//range (3)	
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);
//move (4)	
iterator insert (const_iterator position, value_type&& val);
//initializer list (5)	
iterator insert (const_iterator position, initializer_list<value_type> il);

//erase
iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);

//末尾插入删除,效率较高
void push_back (const value_type& val);
void pop_back();

void clear() noexcept;

//emplace,可以简单理解为insert,实际是直接在指定位置构造元素
template <class... Args>
iterator emplace (const_iterator position, Args&&... args);
template <class... Args>
void emplace_back (Args&&... args);



deque

构造
//default (1)	
explicit deque (const allocator_type& alloc = allocator_type());
//fill (2)	
explicit deque (size_type n);
         deque (size_type n, const value_type& val,
                const allocator_type& alloc = allocator_type());
//range (3)	
template <class InputIterator>
  deque (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());
//copy (4)	
deque (const deque& x);
deque (const deque& x, const allocator_type& alloc);
//move (5)	
deque (deque&& x);
deque (deque&& x, const allocator_type& alloc);
//initializer list (6)	
deque (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());
迭代器
//iterator begin,end
//reverse_iterator rbegin,rend
//const_iterator cbegin,cend
//const_reverse_iterator crbegin,crend
访问元素
reference operator[] (size_type n); //不会进行边界检查
reference at (size_type n);//会进行边界检查
reference front();
reference back();
容量
size_type size() const;
void resize (size_type n, value_type val = value_type()); //resize不会修改原有元素
bool empty() const noexcept;
void shrink_to_fit(); //使capcity等于size
修改
//assign
//range (1)	
template <class InputIterator>
void assign (InputIterator first, InputIterator last);
//fill (2)	
void assign (size_type n, const value_type& val);
//initializer list (3)	
void assign (initializer_list<value_type> il);

//insert
//single element (1)	
iterator insert (const_iterator position, const value_type& val);
//fill (2)	
iterator insert (const_iterator position, size_type n, const value_type& val);
//range (3)	
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);
//move (4)	
iterator insert (const_iterator position, value_type&& val);
//initializer list (5)	
iterator insert (const_iterator position, initializer_list<value_type> il);

//erase
iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);

//首尾插入删除,效率较高
void push_front (const value_type& val);
void push_back (const value_type& val);
void pop_front();
void pop_back();

void clear() noexcept;

//emplace,可以简单理解为insert,实际是直接在指定位置构造元素
template <class... Args>
iterator emplace (const_iterator position, Args&&... args);
template <class... Args>
void emplace_back (Args&&... args);
template <class... Args>
void emplace_front (Args&&... args);



list

构造
//default (1)	
explicit list (const allocator_type& alloc = allocator_type());
//fill (2)	
explicit list (size_type n);
         list (size_type n, const value_type& val,
                const allocator_type& alloc = allocator_type());
//range (3)	
template <class InputIterator>
  list (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());
//copy (4)	
list (const list& x);
list (const list& x, const allocator_type& alloc);
//move (5)	
list (list&& x);
list (list&& x, const allocator_type& alloc);
//initializer list (6)	
list (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());
迭代器
//iterator begin,end
//reverse_iterator rbegin,rend
//const_iterator cbegin,cend
//const_reverse_iterator crbegin,crend
访问元素
reference front();
reference back();
容量
size_type size() const;
bool empty() const noexcept;
修改
//assign
//range (1)	
template <class InputIterator>
void assign (InputIterator first, InputIterator last);
//fill (2)	
void assign (size_type n, const value_type& val);
//initializer list (3)	
void assign (initializer_list<value_type> il);

//insert
//single element (1)	
iterator insert (const_iterator position, const value_type& val);
//fill (2)	
iterator insert (const_iterator position, size_type n, const value_type& val);
//range (3)	
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);
//move (4)	
iterator insert (const_iterator position, value_type&& val);
//initializer list (5)	
iterator insert (const_iterator position, initializer_list<value_type> il);

//erase
iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);

//首尾插入删除,效率较高
void push_front (const value_type& val);
void push_back (const value_type& val);
void pop_front();
void pop_back();

void clear() noexcept;

//emplace,可以简单理解为insert,实际是直接在指定位置构造元素
template <class... Args>
iterator emplace (const_iterator position, Args&&... args);
template <class... Args>
void emplace_back (Args&&... args);
template <class... Args>
void emplace_front (Args&&... args);
额外
//splice 类似于剪切
//entire list (1)
void splice (const_iterator position, list& x);
void splice (const_iterator position, list&& x);
//single element (2)
void splice (const_iterator position, list& x, const_iterator i);
void splice (const_iterator position, list&& x, const_iterator i);
//element range (3)	
void splice (const_iterator position, list& x,
             const_iterator first, const_iterator last);
void splice (const_iterator position, list&& x,
             const_iterator first, const_iterator last);

//remove 删除值等于x的节点
void remove (const value_type& val);

//remove_if 传入bool fun(const value_type& value)的函数
//将元素作为入参,如果为真则删除该元素
template <class Predicate>
  void remove_if (Predicate pred);

//unique 去重
//(1)	
void unique();
//(2)	传入bool fun(const value_type& value1,const value_type& value2)的函数 ,value1,value2,是list任意元素
template <class BinaryPredicate>
  void unique (BinaryPredicate binary_pred);
  
//merge 按序合并,merge假设两个list有序,按照归并排序合并
//(1)	默认升序,要求能进行大小比较
  void merge (list& x);
  void merge (list&& x);
//(2)	
template <class Compare>
  void merge (list& x, Compare comp);
template <class Compare>
  void merge (list&& x, Compare comp);
  
//sort 排序
(1)	默认升序,要求能进行大小比较
  void sort();
(2)	 传入 bool fun(const value_type& value1,const value_type& value2),当返回false交换
template <class Compare>
  void sort (Compare comp);
  
//reverse 翻转
void reverse() noexcept;



虽然bitset和string不属于顺序容器,但是有许多相似的地方也写到这里了

string

注意点
下方用cs表示c-string,s表示string对象,c表示char,p表示pos,n表示length,
1. string 中指示位置,通常用下标,而不是迭代器比如insert和replace
2. 能大段插入的几种情景,构造,insert,assign,append,repalace,重载基本都是类似的
(s),(s,p,n),(cs),(cs,n),(n,c),(beg,end),list,&&s
3. erase 除了常规的iterater,和ietrator_range, 还能用(pos,len)
4. += + 支持cs,s,c 
构造
//default (1)	
string();
//copy (2)	
string (const string& str);
//substring (3)	
string (const string& str, size_t pos, size_t len = npos);
//from c-string (4)	
string (const char* s);
//from buffer (5)	
string (const char* s, size_t n);
//fill (6)	
string (size_t n, char c);
//range (7)	
template <class InputIterator>
  string  (InputIterator first, InputIterator last);
//initializer list (8)	
string (initializer_list<char> il);
//move (9)	
string (string&& str) noexcept;
迭代器
//iterator begin,end
//reverse_iterator rbegin,rend
//const_iterator cbegin,cend
//const_reverse_iterator crbegin,crend
元素访问
reference operator[] (size_type n); //不会进行边界检查
reference at (size_type n);//会进行边界检查
reference front();
reference back();
const char* data() const noexcept;
容量
size_type size() const;
size_t length() const noexcept;
void resize (size_type n, value_type val = value_type()); //resize不会修改原有元素
/*
reserve(n)
确保capacity 至少为n, 没调用reserve时, 容量的扩张规律是:每次size刚超过容量的一半时,容量倍增。调用过reserve后,容量只有在size超过当前容量时,才倍增
*/
void reserve (size_type n);
bool empty() const noexcept;
void shrink_to_fit(); //使capcity等于size
修改
//assign
//string (1)	
string& assign (const string& str);
//substring (2)	
string& assign (const string& str, size_t subpos, size_t sublen);
//c-string (3)	
string& assign (const char* s);
//buffer (4)	
string& assign (const char* s, size_t n);
//fill (5)	
string& assign (size_t n, char c);
//range (6)	
template <class InputIterator>
   string& assign (InputIterator first, InputIterator last);
//initializer list(7)	
string& assign (initializer_list<char> il);
//move (8)	
string& assign (string&& str) noexcept;

// insert
//string (1)	
 string& insert (size_t pos, const string& str);
//substring (2)	
 string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
//c-string (3)	
 string& insert (size_t pos, const char* s);
//buffer (4)	
 string& insert (size_t pos, const char* s, size_t n);
//fill (5)	
 string& insert (size_t pos,   size_t n, char c);
iterator insert (const_iterator p, size_t n, char c);
//single character (6)	
iterator insert (const_iterator p, char c);
//range (7)	
template <class InputIterator>
iterator insert (iterator p, InputIterator first, InputIterator last);
//initializer list (8)	
 string& insert (const_iterator p, initializer_list<char> il);

//append
//string (1)	
string& append (const string& str);
//substring (2)	
string& append (const string& str, size_t subpos, size_t sublen);
//c-string (3)	
string& append (const char* s);
//buffer (4)	
string& append (const char* s, size_t n);
//fill (5)	
string& append (size_t n, char c);
//range (6)	
template <class InputIterator>
   string& append (InputIterator first, InputIterator last);
//initializer list(7)	
string& append (initializer_list<char> il);

//erase
//sequence (1)	
 string& erase (size_t pos = 0, size_t len = npos);
//character (2)	
iterator erase (const_iterator p);
//range (3)	
iterator erase (const_iterator first, const_iterator last);

//replace 替换,被替换范围支持pos,n 和 it1,it2 两种表示
//string (1)	
string& replace (size_t pos,        size_t len,        const string& str);
string& replace (const_iterator i1, const_iterator i2, const string& str);
//substring (2)	
string& replace (size_t pos,        size_t len,        const string& str,
                 size_t subpos, size_t sublen);
//c-string (3)	
string& replace (size_t pos,        size_t len,        const char* s);
string& replace (const_iterator i1, const_iterator i2, const char* s);
//buffer (4)	
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);
//fill (5)	
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);
//range (6)	
template <class InputIterator>
  string& replace (const_iterator i1, const_iterator i2,
                   InputIterator first, InputIterator last);
//initializer list (7)	
string& replace (const_iterator i1, const_iterator i2, initializer_list<char> il);


void push_back (char c);
void pop_back();
查找
//find 从pos开始find,如果找到返回下标,没找到返回-1
//string (1)	
size_t find (const string& str, size_t pos = 0) const noexcept;
//c-string (2)	
size_t find (const char* s, size_t pos = 0) const;
//buffer (3)	
size_t find (const char* s, size_t pos, size_type n) const;
//character (4)	
size_t find (char c, size_t pos = 0) const noexcept;

//find_first_of 从前往后,返回模式串表示的字符集合中任意一个字符被匹配中的位置
//匹配的字符而不是字符串
//string (1)	
size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
//c-string (2)	
size_t find_first_of (const char* s, size_t pos = 0) const;
//buffer (3)	
size_t find_first_of (const char* s, size_t pos, size_t n) const;
//character (4)	
size_t find_first_of (char c, size_t pos = 0) const noexcept;

//find_last_of 从后往前,返回模式串表示的字符集合中任意一个字符被匹配中的位置
//匹配的字符而不是字符串
//一个常用的场景就是将路径中的目录和文件名分割开,c++ ref中的例子
//string (1)	
size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
//c-string (2)	
size_t find_last_of (const char* s, size_t pos = npos) const;
//buffer (3)	
size_t find_last_of (const char* s, size_t pos, size_t n) const;
//character (4)	
size_t find_last_of (char c, size_t pos = npos) const noexcept;

//find_first_not_of
//string (1)	
size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept;
//c-string (2)	
size_t find_first_not_of (const char* s, size_t pos = 0) const;
//buffer (3)	
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
//character (4)	
size_t find_first_not_of (char c, size_t pos = 0) const noexcept;

//find_last_not_of
//string (1)	
size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept;
//c-string (2)	
size_t find_last_not_of (const char* s, size_t pos = npos) const;
//buffer (3)	
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
//character (4)	
size_t find_last_not_of (char c, size_t pos = npos) const noexcept;
常用
string substr (size_t pos = 0, size_t len = npos) const;

重载关系运算符  ! = < >的有效关系组合

static const size_t npos = -1;

//转换成c-string
const char* c_str() const noexcept;

类型转换(常用非成员函数)
//类型转换,转成string,支持任意数字类型作为入参
to_string()

//类型转换,转成数字
//stox, x可以换成i,l,ul,ll,ull,f,d,ld
//以stoi为例子,将string转换成int
int stoi (const string&  str, size_t* idx = 0, int base = 10);
//idx 是一个出参,表示数字之后,下一个字符的位置
//base表示string描述的数字是什么进制的,如果是0,会根据string假设进制,比如0xf,假设16
//注意,base默认值不是0,而是10

bitset

构造
//bitset 可以用:数字,string(某一段pos+n,映射01),c-string(前一段len,映射01)
//模板参数只接受数字,表示bitset的长度
//default (1)	
constexpr bitset() noexcept;
//integer value (2)	
constexpr bitset (unsigned long long val) noexcept;
//string (3)	
template <class charT, class traits, class Alloc>
  explicit bitset (const basic_string<charT,traits,Alloc>& str,
    typename basic_string<charT,traits,Alloc>::size_type pos = 0,
    typename basic_string<charT,traits,Alloc>::size_type n =
      basic_string<charT,traits,Alloc>::npos,
    charT zero = charT('0'), charT one = charT('1'));
//C-string (4)	
template <class charT>
  explicit bitset (const charT* str,
    typename basic_string<charT>::size_type n = basic_string<charT>::npos,
    charT zero = charT('0'), charT one = charT('1'))
运算符重载
属于成员函数:
能使用,假设成两个整数进行位运算的可用的符号
具有赋值属性的符号 返回引用 &=,|=,^=,<<=,>>=,
<<, >> , ~ 返回对象
属于非成员函数:
&,|,^

位访问及修改
//访问位
reference operator[] (size_t pos);

//位设置
//all bits (1)	
bitset& set() noexcept;
//single bit (2)	
bitset& set (size_t pos, bool val = true);

//翻转,会对自己生效
//all bits (1)	
bitset& flip() noexcept;
//single bit (2)	注意,位置从1开始,而不是0,只有这个是从1开始的
bitset& flip (size_t pos);
//
位统计
//统计位1的位数
size_t count() const noexcept;

//返回bitset位数
constexpr size_t size() noexcept;
位测试
//判断第n位是否是1,从0开始
bool test (size_t pos) const;

//判断是否存在bit为1的位置
bool any() const noexcept;

//判断所有位是否为1
bool all() const noexcept;
转换类型
//转换成string
template <class charT = char,
          class traits = char_traits<charT>,
          class Alloc = allocator<charT>>
  basic_string<charT,traits,Alloc> to_string (charT zero = charT('0'),
                                              charT one  = charT('1')) const//转换成ul或ull,以数值的形式比如 1111 -> 15
unsigned long to_ulong() const;
posted @   木瓜粉  阅读(27)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示