C ++ standard library-container class

Source: Internet
Author: User

Container types can be divided into two categories and container adapters:

(1) sequence container)

The elements in this container are ordered, and each element has an exact position in the container. This position does not depend on the element value, but is related to the time when the elements are put into the container. There are three standard sequence containers: vector, deque, and list. In addition, you can think of strings and arrays as sequential containers.
Vectors
A vector uses dynamic arrays to manage its elements. Like an ordinary array, the corresponding index supports random access. Appending and deleting elements from the end of a vector is fast and efficient, and the efficiency of such operations from the header and the middle is lower, in order to maintain the sequence index of elements, you must perform more operations on Moving Elements.

The following is a simple example of using vector:

 

#include <iostream>#include <vector>using namespace std;int main(){vector<int> col1; //create a vector for elements of type int.//append six elements to the vectorfor(int i = 1; i <= 6; i++){col1.push_back(i);}//print all the elements of the vectorfor(int i = 0; i < col1.size(); i++){cout << col1[i] << ' ';}cout << endl;}

 

Deques

Deque indicates the double-ended queue -- double-ended quene. Deque also uses dynamic arrays to manage its elements, but unlike the vector, deque can grow in two directions at the same time. Therefore, adding elements to the head and tail of deque is fast and efficient, inserting an element in the middle is less efficient because the element needs to be moved.
Here is a simple example of using deque:

#include <iostream>#include <deque>using namespace std;int main(){deque<float> col1; //create a deque for elements of type float//insert six elements at the front of the dequefor (int i = 1; i <= 6; i++){col1.push_front(i*1.1);}//print all the elements of the dequefor (int i = 0; i < col1.size(); i++){cout << col1[i] << endl;}cout << endl;}

Lists

List uses a two-way linked list to manage its elements. This means that the elements in the list cannot be randomly accessed, so it takes much longer to locate an element in the list than to search for a vector or deque. However, there are advantages and disadvantages. Compared with vector and deque, the efficiency of inserting and deleting elements in list is equivalent to that in any position. You do not need to move other elements. You only need to maintain the pointer connection, so it is very fast and efficient.
The following is a simple example of using list:

 

#include <iostream>#include <list>using namespace std;int main(){list<char> col1; //create a list for elements of type char//append a - z characters at the end of listfor (char c = 'a'; c <= 'z'; c++){col1.push_back(c);}//print and remove the elementswhile(!col1.empty()){cout << col1.front() << ' ';col1.pop_front();}cout << endl;}

 

The [] operator is not supported because the list cannot access elements randomly.

Strings

For the base_string <>, string, and wstring classes in C ++, we can also regard them as the container class, similar to the vector. The difference is that their element type has been determined: Char.

Arrays

In principle, static arrays of C or C ++ cannot be classified into STL containers because of their own member functions, such as size () and empty. However, STL allows you to use STL algorithms for these common static arrays. For more information, see the following section.

(2) Associative containers)
This type of container is ordered by values. The position of each element depends on the time when it is placed in the container, but depends on its value. Generally, associated containers are implemented by binary trees.
There are four standard associated container classes: Set, Multiset, map, and multimap.

Set
Elements in a set container are sorted by their own values, and a value can only appear once.

Multisets

A Multiset container must have the same value as a set container.

Maps

The elements in a map container are a key/value pair, where the elements are sorted by key, and the same value Key is not allowed.

Multimaps

In addition to allowing the emergence of the same key-value element, a multimap container is the same as a map container.

For all these associated container classes, the default sorting rules are operators. You can also customize the sorting rules (comparison function or function object) through template parameters ).

(3) Container Adapter)
In addition to the basic container class, the C ++ standard library also provides some container adapter classes to meet some special requirements. These container adapter classes are implemented by the Basic container class.
Stacks

A stack container uses the LIFO (last-in-first-out) policy to manage elements.

Queues

A queue container uses a first-in-first-out (FIFO) policy to manage elements.

Priority Queues
A priority queue container uses a priority policy to manage elements. The element priority is based on a sorting policy specified by the programmer (default operator <). Generally, the next element in the container is the element with the highest priority. If there are more than one element with the highest priority, their order is not defined.

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.