C++中的容器


容器与容器适配器

容器包括vector, deque, list, map, multimap, set, multiset。容器适配器包括基于deque的stack和queue,基于vector的priority_queue。string也实现了stl的接口。

因为编写C++程序时经常需要查找容器的函数接口,故作此总结。C++新引入的容器与函数未引入。主要参考自:STL Containers and Container Adaptors

序列容器

包括vector,deque,list

共有函数

  • 构造函数
    ContainerType<T> c; 
    ContainerType<T> c(num);    
    ContainerType<T> c(num, val);   
    ContainerType<T> c(inIterBegin, inIterEnd);     
    //复制构造函数
    ContainerType<T> c(otherLikeContainer);     
    ContainerType<T> c = otherLikeContainer;
  • 赋值构造函数
    c1 = c2
  • 比较运算
    c1 == c2 
    c1 != c2 
    c1 < c2 //按元素逐个比较
    c1 <= c2 
    c1 > c2 
    c1 >= c2
  • 容量
    empty() 
    size() 
    max_size() 
    resize(num, val = default)
  • 迭代器和引用
    begin()
    end()
    rbegin() 
    rend()
    front() 
    back()
  • 插入值
    push_back(val) 
    insert(iter, val) 
    insert(iter, num, val)
    insert(iter, inIterBegin, inIterEnd)
  • 赋值(换掉容器内所有元素)
    assign(inIterBegin, inIterEnd) 
    assign(num, val)
  • 删除元素
    pop_back() 
    erase(iter) 
    erase(iterBegin, iterEnd) 
    clear()
  • 其他
    swap(otherLikeContainer)
    get_allocator()

特有函数

  • vector特有
    reserve(num)
    capacity()
  • list特有
    merge(otherList) //按照大小顺序合并,二者必须是有序的
    merge(otherList, binPred) 
    remove(val)
    remove_if(unPred) 
    reverse() 
    sort() 
    sort(binPred) 
    splice(iter, otherList) //将otherList中所有元素移动到iter处
    splice(iter, otherList, otherIterBegin, otherIterEnd) 
    unique() 
    unique(binPred)
  • vector和deque特有
    at(index) //会检查下标范围
    operator[](index)
  • deque和list特有
    push_front(val)
    pop_front()

容器适配器

包括stack,queue,priority_queue

共有函数

c1 = c2 
empty()
size() 
push(val) 
pop()

特有函数

  • queue特有函数
    front() 
    back()
  • stack和priority_queue 特有函数
    top() 
    == 
    != 
    < 
    <= 
    > 
    >=

序列容器与容器适配器函数表格

参见The STL Sequential Containers and Container Adaptors,
and their Member Functions

关联容器

包括map, multimap, set, multiset

共有函数

  • 赋值
    c1 = c2
  • 比较
    == 
    != 
    < 
    <= 
    > 
    >=
  • 容量
    empty() const 
    size() const 
    max_size()
  • 迭代器
    begin() 
    end() 
    rbegin() 
    rend()
  • 插入值
    insert(p, val) 
    insert(start, end)
  • 删除
    erase(someKey) 
    erase(iter) 
    erase(start, end) 
    clear()
  • 查找
    count(someKey) 
    find(someKey)  //返回迭代器
    lower_bound(someKey) //大于等于someKey的迭代器
    upper_bound(someKey) //大于someKey的迭代器
    equal_range(someKey)
  • 其他
    swap(otherLikeContainer) 
    get_allocator() 
    //key和val比较的函数对象
    key_comp() 
    value_comp()

特有函数

其实不存在特有函数,只是这些函数的接口略有不同

  • map/multimap特有构造函数
    ContainerType<keyType, ValueType> c; 
    ContainerType<KeyType, ValueType> c(inIterBegin, inIterEnd); 
    ContainerType<KeyType, ValueType> c(otherLikeContainer);
  • set/multiset特有构造函数
    ContainerType<T> c; 
    ContainerType<T> c(inIterBegin, inIterEnd); 
    ContainerType<T> c(otherLikeContainer);
  • map特有成员函数
    operator[someKey]
  • map/set特有成员函数
    //返回值为pair<iterator, true/false(是否已经含有此值)>
    insert(val)
  • multimap/multiset特有成员函数
    //返回iterator
    insert(val)

关联容器函数表格

参见The STL Associative Containers and their Member Functions

其他

包括string,bitset等类容器

string

  • 构造函数
    string s;
    string s(c_string_value);
    string s(char_array, size_type_count);
    string s(string_value);
    string s(string_value, size_type_index);
    string s(string_value, size_type_index, size_type_count);
    string s(size_type_count, char_value);
    string s(input_iterator_start, input_iterator_end);
  • 取char
    s[i]
    s.at(i) //边界检查
  • 迭代器
    s.begin()
    s.end()
    s.rbegin()
    s.rend()
  • append与赋值
    operator+=
    s.append(string_value)
    s.append(c_string_value)
    s.append(size_type_count, char_value)
    s.append(c_string_value, size_type_count)
    s.append(c_string_value, size_type_index, size_type_count)
    s.append(first_input_iterator, last_input_iterator)
    //
    operator=
    s.assign(string_value)
    s.assign(c_string_value)
    s.assign(size_type_count, char_value)
    s.assign(c_string_value, size_type_count)
    s.assign(c_string_value, size_type_index, size_type_count)
    s.assign(start_input_iterator, end_input_iterator)
  • 转换为c-string
    s.copy(char_array, size_type_count, size_type_index)
    s.c_str() //返回以\0结束的char数组地址,数组归s所有,不要更改
    s.data() //返回不以\0结束的char数组地址,数组归s所有,不要更改
  • 子串
    s.substr(size_type_index)
    s.substr(size_type_index, size_type_count)
  • 容量及调整容量
    s.empty()
    s.capacity()
    s.length()
    s.size()
    s.max_size()
    s.reserve(size_type_value)
    s.resize(size_type_value, char_value)
    s.resize(size_type_value)
  • 删除
    s.clear()
    s.erase() //删除所有字符
    s.erase(size_type_index)
    s.erase(size_type_index, size_type_count)
    s.erase(iterator_position)
    s.erase(first_iterator, last_iterator)
  • 查找
    //所有的find均返回下标值,若找不到,返回string::npos
    //
    //查找char
    s.find(char_value) 
    s.find(char_value, size_type_index) 
    s.rfind(char_value) 
    s.rfind(char_value, size_type_index)
    //
    //查找string
    s.find(string_value) 
    s.find(string_value, size_type_index) //从index处开始查找
    //从后向前查找
    s.rfind(string_value) 
    s.rfind(string_value, size_type_index)
    //查找cstring
    s.find(c_string_value, size_type_index, size_type_count) 
    s.rfind(c_string_value, size_type_index, size_type_count)
    //
    s.find_first_of(char_value) 
    s.find_first_of(char_value, size_type_index) 
    s.find_first_not_of(char_value) 
    s.find_first_not_of(char_value, size_type_index)
    //
    //查找在/不在string中的char,返回下标
    s.find_first_of(string_value) 
    s.find_first_of(string_value, size_type_index) 
    s.find_first_not_of(string_value) 
    s.find_first_not_of(string_value, size_type_index)
    //
    s.find_first_of(c_string_value, size_type_index, size_type_count) 
    s.find_first_not_of(string_value, size_type_index, size_type_count)
    //
    s.find_last_of(char_value) 
    s.find_last_of(char_value, size_type_index) 
    s.find_last_not_of(char_value) 
    s.find_last_not_of(char_value, size_type_index)
    //
    s.find_last_of(string_value) 
    s.find_last_of(string_value, size_type_index) 
    s.find_last_not_of(string_value) 
    s.find_last_not_of(string_value, size_type_index)
    //
    s.find_last_of(c_string_value, size_type_index, size_type_count) 
    s.find_last_not_of(string_value, size_type_index, size_type_count)
  • 插入值
    s.insert(size_type_index, string_variable) 
    s.insert(size_type_index, c_string_value)
    s.insert(size_type_index1, string_variable,  size_type_index2, size_type_count)
    s.insert(size_type_index, c_string_value, size_type_count)
    s.insert(size_type_index, size_type_count, char_value)//c++中函数形参总是count在val之前
    s.insert(iterator_position, size_type_count, char_value)
    s.insert(iterator_position, char_value)
    s.insert(iterator_position, input_iterator_first, input_iterator_last)
    //
    s.push_back(char_value)
  • 字符/字符串替换
    s.replace(size_type_index, size_type_count, string_value)
    s.replace(iterator_first, iterator_last, string_value
    s.replace(size_type_index1, size_type_count1, string_value, 
            size_type_index2, size_type_count2)
    s.replace(size_type_index, size_type_count, c_string_value)
    s.replace(iterator_first, iterator_last, c_string_value)
    s.replace(size_type_index, size_type_count1, 
            c_string_value, size_type_count2)
    s.replace(iterator_first, iterator_last, 
            c_string_value, size_type_count)
    s.replace(size_type_index, size_type_count1, 
            size_type_count2, char_value)
    s.replace(iterator_first, iterator_last, 
            size_type_count, char_value)
    s.replace(iterator_first, iterator_last, 
            input_iterator_start, input_iterator_end)
  • 比较
    //==, !=, <, > <=, >=已经重载
    //
    //compare返回值为int:s-other
    s.compare(string_value)
    s.compare(size_type_index, size_type_count, string_value)
    s.compare(size_type_index1, size_type_count1, string_value, 
            size_type_index2, size_type_count2)
    //
    s.compare(c_string_value)
    s.compare(size_type_index, size_type_count, c_string_value)
    s.compare(size_type_index, size_type_count1, 
            c_string_value, size_type_count2)
  • 其他函数
    s.swap(string_variable)
    //
    //以下三个非成员函数
    swap(string_variable1, string_variable2)
    //
    getline(inStream, string_variable)
    // string结果不包含delimiter
    getline(inStream, string_variable, char_delimiter_value)

本文永久更新链接地址

相关内容