歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++中的容器

C++中的容器

日期:2017/3/1 9:30:15   编辑:Linux編程

容器與容器適配器

容器包括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)

Copyright © Linux教程網 All Rights Reserved