C + + Container summary order container and sequential container adapter

Source: Internet
Author: User

Container

A container is a collection of objects of a specific type, a container's type is divided into sequential containers, container adapters, and associative containers, a sequential container aggregates a single type element into a container, and then stores and accesses these elements based on location.

The order of the elements in a sequential container is independent of the element value, but is determined by the order in which the elements are added to the container. The standard library defines three sequential container types: Vector,list and deque. They differ in the way in which elements are accessed, and the cost of operating the operations associated with adding or removing elements.

Adapters are based on the operations provided by the original container type, and are adapted to the underlying container type by defining a new operating interface. Sequential container adapters include stack,queue and priority_queue types.




The standard library also defines several associative containers whose elements are sorted by key.

The standard library imposes a common interface for the operations defined by the container type. The difference between these container types is what they provide, but if two containers provide the same operation, their interfaces (the number of function names and the number of arguments) are the same. The Operation collection hierarchy for the container type is as follows

1. Some operations apply to all container types;

2. Some operations apply only to a subset of sequential or associative container types;

3. Some operations apply only to a subset of the sequence or associated container type types;


Sequential Containerinitialization of sequential containers and definitions

Use the sequential container to first include the header file:

#include <vector> #include <list> #include <deque>

definition of sequential containers

Vector<int> vec;//All containers are the types of elements that are stored in a container in the template list<int> lis;//<>, which can be a common data type or a class type that you define </span> deque<int> items;//The container type defines a default constructor for creating an empty container object of the specified type, with the default constructor without parameters


initialization of sequential containers


Vector<int> vec;//defines a container that contains an int type element vector<int> VEC1 (10);//container containing 10 int elements vector<int> VEC2 (10,1);// Each container containing 10 int elements is initialized to 1 and applies only to the sequential container vector<int> vec3 (VEC2);//copy elements from VEC2 to VEC3, only for sequential containers


You cannot assign an element in a container directly to another container, but you can use the pass-through pair of iterators to implement the function indirectly

Vector<string> svec;list<string> IList (SVEC);//container different, cannot copy List<string>slist (Svec.begin (), Svec.end ());//Use an iterator that does not require the same container type

Note: In addition to reference types, all built-in or composite types (including the container type itself) can be used as element types, and the IO library type is not available as a container type;

When you use a class type as an element type, if you specify the size of the container, you must provide a single initializer constructor.

vector< <vector<string> >lines;

Operations for sequential containers

type aliases for sequential containers







begin and end members

All containers provide the begin and end operators, which return iterators, BEGIN returns the first element pointed to by the iterator, end returns the element next to the end of the container to which the iterator points, it points to a nonexistent element, and when the container is empty, the values returned by the two operators are equal.




adding elements to the container

The container supports the following actions to add new elements:


Any insert and push operations can cause an iterator to fail, and when writing a program, the program must ensure that the iterator is updated after each insert operation. The iterator to avoid storing the end operation return.



Relational Operators

All containers support relational operators to compare elements in a container, and when compared, the container type must be the same type as the element in the container. A container comparison is based on a comparison of elements within a container, that is, the comparison operation of its inner element type definition. If the container's element type does not support an operator, the container cannot do a comparison operation.


Container size Operation



Accessing elements



Delete Element



Assignment and Swap

When using an assignment operator, the first two container lengths can be unequal, but the two containers have the length of the right operand after assigning the value.

= and assign invalidate all iterators in the left-hand operand container, and swap does not.

Assign can implement assignment of elements of different types but are compatible with each other.

Swap implements the function of exchanging all elements within two containers, the type of container to be exchanged must match, the operands must be of the same type, and the type of elements stored must be the same.




Capacity and Reserve

Since the vector elements are stored in a continuous fashion, when new elements are added, but the container has no space to accommodate the new elements, the memory space is reallocated back, the storage elements of the space are copied, the new elements are inserted, and the old storage space is finally revoked. In order for the vector to achieve fast memory allocation, the actual allocated capacity is more than the current space required.

The vector provides two member functions capacity and reserve to interact with the container's memory allocation.

The capacity operation gets the total number of elements that can be stored before the container needs to allocate more storage space, usually larger than size.

The reserve operation tells the container how many elements of the storage space should be reserved.


Container Adapter

An adapter is a mechanism that makes one thing behave like another, and an adapter enables an existing container type to work in a different type of abstraction. For example, stack enables any sequential container to be implemented as a stack.

Use adapter must contain header file

#include <stack> #include <queue>


common operations and types of container adapters



initialization of the adapter

all adapters have two constructors 1. Default constructor: Common empty Object 2. Constructor with one container parameter: a copy of the parameter container as the underlying value

DEQUE<INT>DEQ;STACK<INT>STK (DEQ);


overriding the underlying container type

The default stack and queue are based on the Deque container implementation, while the priority_queue is implemented on the vector container.

If you want to overwrite its associated container type, you can specify a sequential container as the second type argument of the adapter:

stack< string,vector<string> >str_stk;

for a given adapter, the associated container must meet certain constraints

Stack can be associated with all sequential containers;

Queue requires the associated container to support Push_front, so it can only be built on list and deque;

Priority_queue requires random access, and is therefore only available on vectors and deque;


Stack Adapter

The operation of the stack is as follows:


All container adapters define their operations based on the operations supported by their underlying container type, for example, the stack is built on the deque container, so the stack function is implemented using the operations provided by Deque.


queues and Priority queues

priority_queue allows the user to prioritize the elements stored in the queue, rather than placing the new element directly at the end of the queue, rather than in front of the element with a lower priority.







iterators and iterator actions

An iterator is a data type that examines the inner elements of a container and traverses the elements, and the standard library defines an iterator type for each standard container, such as a vector iterator:

Vector<int>::iterator ITER

All iterators have the same interface, for example iterators can use the dereference operator (* operator) to access the element that the iterator points to

*iter=0//set the value of ITER to 0.

the following table iterators provide operations for all standard library container types



Deque and vectors also provide additional operator



Both vectors and deque provide fast random access to their elements, and the list supports only self-increment and equal inequality operations.

Const_iterator

The type can only be used to read elements inside a container but cannot change its value.


comparison of the various containers

Vector: Provides random access to the element, except for the end of the container, the insert (or delete) operation at any other location in the container requires the movement of all elements to the right of the element to be inserted (or deleted).

List: Represents a discontinuous area of memory that can efficiently insert and delete an element at any location, but does not support random access

Deque: Inserting and deleting elements at both ends of the deque queue is very fast, and Deque has the following properties: 1. Insertion or deletion in the middle of the container is inefficient; 2. Can be efficiently inserted and removed at its first, as if at the end of the container 3. Support random access to all elements; 4. Inserting an element in the header or tail of the container does not invalidate any iterators, while a header or trailing delete causes an iterator to the element to be deleted, and the insert and delete operation at any other location invalidates all iterators that point to that container element.








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Container summary order container and sequential container adapter

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.