C++中的容器(一)

2015-07-20 17:06:42 ? 作者: ? 浏览: 9

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
         
           c; ContainerType
          
            c(num); ContainerType
           
             c(num, val); ContainerType
            
              c(inIterBegin, inIterEnd); //复制构造函数 ContainerType
             
               c(otherLikeContainer); ContainerType
              
                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
                         
                           c; ContainerType
                          
                            c(inIterBegin, inIterEnd); ContainerType
                           
                             c(otherLikeContainer);
                           
                          
                         
          • set/multiset特有构造函数
            ContainerType
                         
                           c; ContainerType
                          
                            c(inIterBegin, inIterEnd); ContainerType
                           
                             c(otherLikeContainer);
                           
                          
                         
          • map特有成员函数
            operator[someKey]
          • map/set特有成员函数
            //返回值为pair
                         
                           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
                          
-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: