C ++ collection (4) -- sequential container and collection sequence container

Source: Internet
Author: User

C ++ collection (4) -- sequential container and collection sequence container

In the previous blog article ("standard library for Beginners"), we briefly learned about a most common sequence container: vector type. This article will further learn and improve the content of this article, and continue to discuss the sequence container types provided by the standard library. An ordered container aggregates elements of a single type into a container and stores and accesses these elements based on their locations. The standard library defines three sequence container types: vector, list, And deque (double-ended queue Dual-end queue ). Three sequence container adapters are also provided: stack, queue, and priority_queue.

Sequential container Vector Fast Random Access
List Supports fast insert/delete
Deque Dual-end queue
Sequential container Adapter Stack Back-in-first-out Stack
Queue FIFO queue
Priority_queue Queue with priority management

Definition and iterator of ordered containers

  • The element type of the container must meet two conditions: the element supports the value assignment operation, and the element object can be copied. The above is the minimum limit for containers to meet, and other elements need to be met for associated containers. Note that references do not support Value assignment and IO library types do not support Value assignment or copying. Therefore, neither of them can be used as the element of the container.
  • Note: when defining a container, two adjacent> symbols must be separated by spaces. Otherwise, the> operator is used as the right shift operator, resulting in compilation errors.
1 vector< vector<string> > liens; //ok2 vector < vector<string>> lines; //error:>>treated as shift operator
  • The standard library provides some common operations for the iterator, such as unreferencing and auto-incrementing.
  • Relational operators are only applicable to vector and deque containers, because they provide fast random access and allow direct access to container elements based on element locations. The list container iterator neither supports arithmetic operations nor relational operations.
  • The iterator range is the left closed range, that is, [first, last), indicating all elements from the position indicated by first to the position indicated by last. The requirements for iterators are as follows: they point to the elements in the same container or the next position beyond the end; first can reach the last value after repeated auto-increment operations.
  • Some container operations modify the internal status of the container or move the elements in the container. This will invalidate some iterators and cause the same issue as the suspension pointer. What are the differences between the following two sections of code? Why?
     1 vector<int>::iterator mid = iv.begin() + iv.size()/2; 2 vecor<int>::iterator iter = iv.begin(); 3 while (iter != mid) 4     if (*iter == someVal) 5         iv.insert(iter, 2*someVal); 6  7 vector<int>::iterator iter = iv.begin(); 8 while (iter != iv.begin() + iv.size()/2) 9 {10     if (*iter == someVal)11     {    12           iter = iv.insert(iter, 2*someVal);13           iter += 2;14     }        15     else16         ++iter;17 }

     

Sequential container operations

  • Some basic operations, such as begin and end members, insert operations, resize operations, erase operations, assign operations, Relational operators, and so on.

Auto-Growth of vector containers

  • The vector class provides two member functions: capacity and reserve. The former gets the total number of elements that can be stored before the container needs to allocate more storage space, and the latter tells the container how much space should be reserved. Note that capacity is not equal to size.

Container Selection

  • To randomly access elements, use vector or deque.
  • To insert or delete elements in the middle of a container, use list.
  • Use deque to insert or delete a part at the beginning and end.
  • If you only need to insert an element in the middle of the container when reading the input, and then need to access the element immediately, you can read the element into the list container, and then re-sort the container to make it suitable for sequential access, copy to vector.
  • The core of container selection depends on the cost of random memory access list and the cost of copying elements when inserting and deleting elements in vector/deque.

 

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.