C ++ container learning, container

Source: Internet
Author: User

C ++ container learning, container

I didn't know how to read containers when I learned C ++ myself. I haven't compiled many C ++ programs. Now I want to write something in C ++, the burst container type is a little unfamiliar, so take this note. (See C ++ primer.

Container: a collection of specific types of objects. (Definition is important)

Ordered containers: aggregates a single element to store and access elements based on the location.

The standard library defines three containers: vector, list, And deque.

Sequential container adapter: stack, queue, priority_queue

Sequential container  
Vector Fast and Random Access
List Supports fast insertion and deletion.
Deque Dual-end queue
Adapter  
Stack LIFO
Queue FIFO
Priority_queue Queue with priority management
Container initialization:

C <T> c; Initialize an empty container

C c (c2); initialize with an existing container. The c2 type and C type must be the same

C c (B, e); Use the iterator range or pointer to initialize. B, e indicates that the object type can be different from C.

C c (n, t); the new container has n elements, each initialized to t. (Only applicable to ordered containers, and requires the element class to provide a constructor for a parameter)

C c (n); the new container has n elements (only applicable to ordered containers, and the default constructor is required)

Type constraints of elements in the container: 1). values must be assigned. 2) The elements must be copied;

When specifying the container: vector <string> lines; note that spaces are left;

Iterator and iterator range:

Shape: vector <int >:: iterator iter; // iter is equivalent to pointer

* Iter References to elements pointed by iter
Iter-> mem Returns the mem member of a specified element.
++ Iter, iter ++, or -- Auto increment/Decrement
= ,! =
Relational operators Applicable only to vector and deque
+,-, + =,-=, And other arithmetic operations Applicable only to vector and deque

Note: The list iterator does not support self-reporting or relational operations. It only provides pre-and post-auto-increment, auto-subtraction, and equal and unequal operations.

Add Element
C. push_back (t) Add t at the end
C. push_front (t) Add t in the header, only applicable to list and deque
C. insert (p, t) Move t in front of iterator p and return the iterator of the newly added element
C. insert (p, n, t) Insert n t before p and return void
C. insert (p, B, e) Insert elements in the range referred to by B and B before p, and return void

Note: Any insert and push operations may invalidate the iterator. When writing a loop to insert elements to a vector or deque, the program must ensure that the iterator is updated after each loop. Therefore, do not store the iterator returned by the end operation.

Size operation:
C. size ()
C. max_size ()
C. empty ()
C. resize (n) Adjust the size. Enter 0 or delete unnecessary elements.
C. resize (n, t) Adjust the size and assign a value to the new element t.
Access element:
C. back () Returns the reference of the last element.
C. front () Reference of the first element
C [n] Subscript access. Applicable only to vector and deque
C. at (n) Returns the subscript. If the subscript is invalid, an exception is thrown. It applies only to vector and deque.
Delete element:
C. erase (p) Delete the element at iterator p and return the iterator pointing to the next element.
C. erase (B, e) Delete all elements in the range marked by the iterator
C. clear ()
C. pop_back () Delete the last element
C. pop_front () Delete the first element, only applicable to list or deque
Assignment operation:

C1 = c2

C1.swap (c2) Exchange content (the original iterator will not expire)
C. assign (B, e)
C. assign (n, t) C. Reset to n tvalue elements.
Self-Growth of vector:

Each time the vector adjusts its capacity to 3/2 of the current capacity.

 

Container differences:

Vector and deque: the storage space is continuous, and elements can be quickly and randomly accessed. The insertion and deletion of a vector at the end is fast, but the insertion and deletion efficiency is low in other locations. deque is similar to that of a vector, however, insertion and deletion at the beginning and end are fast.

List: uses a linked list to organize elements. Therefore, random access is not supported, but the cost of inserting or deleting elements at any location is small.

Note:: When writing code, try to use only the operations that can be provided by the vector and list containers,Use iterator instead of subscript,And avoid random access to elements. In this way, you can easily change the program from using a vector container to using a list container when necessary.

Adapter differences:

Stack can be built on a vector, list, or deque container;

Queue requires its basic container to provide push_front operations, so it can only be built on the list container;

Priority_queue requires random access, so it can be built on vector and deque.

STACK:

You can query whether it is empty (), size (), pop (): Delete the top of the stack, top (): return the top element of the stack, and push (): push into the new element.

Queue and priority queue:

Queue: FIFO

Priority queue: place the new element in front of the element with a lower priority than the new element based on its priority.

Supported functions: empty (); size (); pop (); front (): return the first element of the team, applicable only to the queue; back (): return the team end element, applies only to queues; top (): returns the highest priority element value, applies only to priority queues; push ();

String type

String can be considered as a special container, and its operations are many similar to that of vector containers.

However, the stack operation is not supported in string, that is, the front, back, And pop_back operations cannot be used.

Of course, string also provides functions not available in many other containers, such as append, replace, find, find_first_of, find_first, and compare.


Shouldn't the instrument be used as a chemical reaction container during the experiment? A beaker B beaker C test tube D measuring cylinder D
Who can talk about the C-language queue function? Give a learning link and define the queue template class <queue> header file.
Similar to the stack template class, the queue template class also requires two template parameter element types: The container class.
Type element type required container type select default deque type
Sample Code for defining a queue object:
Queue <int> q1;
Queue <double> q2;
Basic queue operations include:
Queue example: q. push (x); x is connected to the end of the queue
Departure example: q. pop (); the element of the queue is displayed, and the element value is returned.
Example of the first element of the access team: q. front () is an element that is pushed into the queue early
Element at the end of the access team: q. back () is the queue element.
Judge queue null example: q. empty () returns true if the queue is empty
Number of access queue elements: q. size ()

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.