C + + supplements (iv)--sequential container

Source: Internet
Author: User

A previous blog post ("The first glimpse of the standard library") simply learned about one of the most commonly used sequential containers: the vector type. This article will further study and improve the content, continue to discuss the standard library provides the order of container types. The so-called sequential container that aggregates elements of a single type into containers and stores and accesses these elements based on their location. The standard library defines three sequential container types: Vector,list and deque (double-ended queue double-ended queues). Three sequential container adapters (adaptor) are also available: Stack,queue,priority_queue.

Sequential container Vector Supports fast random-fetch
List Fast Insert/Delete support
Deque Dual-ended queues
Sequential container Adapters Stack LIFO stack
Queue FIFO queue
Priority_queue Queue with priority management

Definition of sequential containers and iterators

    • The container element type must satisfy two conditions: the element supports assignment operations, and the element object can be copied. These are the minimum containers to satisfy, and the associated container needs to satisfy other elements. Note that the reference does not support assignment operations, and the IO library type does not support assignment or replication, so neither can be used as an element of the container.
    • Note: The container definition must be separated by a space of two adjacent > symbols, otherwise the system will use >> as the right shift operator, resulting in compilation errors.
1 vector< vector<string//OK2 vector < vector<string  //error:>>treated as shift operator
  • The standard library provides some common operations for iterators, such as dereference, self-increment, and so on.
  • The relational operators are only available for vector and deque containers, because they provide fast random fetches, and can access container elements directly based on their location. The iterator for the list container does not support arithmetic operations, nor does it support relational operations.
  • The iterator range is a left-closed interval, or [First,last], representing all elements from the position indicated by first to the position indicated by last. The requirements for iterators are that they point to the elements in the same container or to the next position beyond the end, and first do the self-increment operation to reach the last.
  • Some container operations modify the inner state of the container or move the elements inside the container, which invalidates some iterators, leading to the same problem as the dangling pointer. What is the difference between considering the following two paragraphs of code? Why?
    1vector<int>::iterator mid = Iv.begin () + iv.size ()/2;2vecor<int>::iterator iter =Iv.begin ();3  while(ITER! =mid)4     if(*iter = =someval)5Iv.insert (ITER,2*someval);6 7vector<int>::iterator iter =Iv.begin ();8  while(iter! = Iv.begin () + iv.size ()/2)9 {Ten     if(*iter = =someval) One     {     Aiter = Iv.insert (ITER,2*someval); -ITER + =2; -     }         the     Else -++iter; -}

Operations for sequential containers

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

Self-growth of vector containers

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

Selection of containers

    • To randomly fetch elements, use a vector or deque.
    • To insert or delete elements in the middle of a container, you should use list.
    • If you insert or delete the end-to-end section, use Deque.
    • If you only need to insert the element at the middle of the container when reading the input, and then need to access the element, you can read the element into the list container, and then reorder the container so that it is suitable for sequential access, which is copied into the vector.
    • The core of the selection container depends on the cost of randomly visiting the list and the cost of copying elements when inserting deletes in Vector/deque.

C + + supplements (iv)--sequential container

Related Article

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.