Containers in C ++

Source: Internet
Author: User

Containers in C ++
Container and container adapter in C ++

Containers include vector, deque, list, map, multimap, set, and multiset. Container adapters include deque-based stack and queue, and vector-based priority_queue. String also implements the stl interface.

This is because you often need to find the container function interface when writing C ++ programs. C ++ has not introduced new containers and functions. Reference: STL Containers and Container Adaptors

Sequential container

Including vector, deque, list

Common functions
  • Constructor
    ContainerType
        
         
    C; ContainerType
         
          
    C (num); ContainerType
          
           
    C (num, val); ContainerType
           
            
    C (inIterBegin, inIterEnd); // copy the constructor ContainerType
            
              C (otherLikeContainer); ContainerType
             
               C = otherLikeContainer;
             
            
           
          
         
        
  • Value assignment Constructor
    c1 = c2
  • Comparison
    C1 = c2 c1! = C2 c1 <c2 // compare c1 by element one by one <= c2 c1> c2 c1> = c2
  • Capacity
    empty() size() max_size() resize(num, val = default)
  • Iterator and reference
    begin()end()rbegin() rend()front() back()
  • Insert value
    push_back(val) insert(iter, val) insert(iter, num, val)insert(iter, inIterBegin, inIterEnd)
  • Assign values (replace all elements in the container)
    assign(inIterBegin, inIterEnd) assign(num, val)
  • Delete Element
    pop_back() erase(iter) erase(iterBegin, iterEnd) clear()
  • Others
    swap(otherLikeContainer)get_allocator()
    Special Functions
    • Unique to vector
      reserve(num)capacity()
    • List-specific
      Merge (otherList) // merge by size. The two must be ordered merge (otherList, binPred) remove (val) remove_if (unPred) reverse () sort (binPred) splice (iter, otherList) // move all elements in otherList to splice (iter, otherList, otherIterBegin, otherIterEnd) unique (binPred) at iter)
    • Unique to vector and deque
      At (index) // checks the subscript range operator [] (index)
    • Unique to deque and list
      push_front(val)pop_front()
      Container Adapter

      Including stack, queue, priority_queue

      Common functions
      c1 = c2 empty()size() push(val) pop()
      Special Functions
      • Special Functions of queue
        front() back()
      • Stack and priority_queue Functions
        top() == != < <= > >=
        Sequence container and container adapter function table

        See The STL Sequential Containers and Container Adaptors,
        And their Member Functions

        Associated container

        Including map, multimap, set, and multiset

        Common functions
        • Assignment
          c1 = c2
        • Comparison
          == != < <= > >=
        • Capacity
          empty() const size() const max_size()
        • Iterator
          begin() end() rbegin() rend()
        • Insert value
          insert(p, val) insert(start, end)
        • Delete
          erase(someKey) erase(iter) erase(start, end) clear()
        • Search
          Count (someKey) find (someKey) // return iterator lower_bound (someKey) // iterator greater than or equal to someKey upper_bound (someKey) // iterator greater than someKey performance_range (someKey)
        • Others
          Swap (otherLikeContainer) get_allocator () // function object key_comp () value_comp () for key and val comparison ()
          Special Functions

          Actually, there are no special functions, but the interfaces of these functions are slightly different.

          • Map/multimap special Constructor
            ContainerType
                        
                          c; ContainerType
                         
                           c(inIterBegin, inIterEnd); ContainerType
                          
                            c(otherLikeContainer);
                          
                         
                        
          • Set/multiset special Constructor
            ContainerType
                        
                          c; ContainerType
                         
                           c(inIterBegin, inIterEnd); ContainerType
                          
                            c(otherLikeContainer);
                          
                         
                        
          • Map-specific member functions
            operator[someKey]
          • Map/set special member functions
            // The returned value is pair.
                        
                         
            Insert (val)
                        
          • Multimap/multiset special member functions
            // Return iteratorinsert (val)
            Associate container function table

            See The STL Associative Containers and their Member Functions

            Others

            Including string, bitset, and other class containers

            String
            • Constructor
              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) // border check
            • Iterator
              s.begin()s.end()s.rbegin()s.rend()
            • Append and value assignment
              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)
            • Convert to c-string
              S. copy (char_array, size_type_count, size_type_index) s. c_str () // returns the ending char array address. The array belongs to s. Do not change s. data () // return the address of the char array that does not end with. The array belongs to s. Do not change it.
            • Substring
              s.substr(size_type_index)s.substr(size_type_index, size_type_count)
            • Capacity and capacity adjustment
              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)
            • Delete
              S. clear () s. erase () // Delete All characters s. erase (size_type_index) s. erase (size_type_index, size_type_count) s. erase (iterator_position) s. erase (first_iterator, last_iterator)
            • Search
              // All the find statements return the lower mark value. If no value is found, string: npos // is returned to find chars. find (char_value) s. find (char_value, size_type_index) s. rfind (char_value) s. rfind (char_value, size_type_index) // find strings. find (string_value) s. find (string_value, size_type_index) // search for s from the index. rfind (string_value) s. rfind (string_value, size_type_index) // search for cstrings. 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) // returns the subscript 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. 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)
            • Insert value
              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) // in c ++, the function parameters are always count before 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. push_back (char_value)
            • Character/string replacement
              s.replace(size_type_index, size_type_count, string_value)s.replace(iterator_first, iterator_last, string_values.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)
            • Comparison
              // = ,! =, <,> <=, >= Already overloaded /// compare returns an int: s-others.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)
            • Other functions
              S. swap (string_variable) /// swap (string_variable1, substring) // getline (inStream, string_variable) // The string result does not contain delimitergetline (inStream, string_variable, char_delimiter_value)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.