STL container, stl

Source: Internet
Author: User

STL container, stl


Article from http://blog.csdn.net/zhouyunxuan


Seven sequence container types

1. vector is a type of representation of arrays. It provides an automatic memory management function to dynamically change the length of a vector object and scale down as elements are added or deleted,
It provides Random Access to elements. The time for adding and deleting elements at the end is fixed, but the complexity of inserting and deleting elements in the header or in the middle is linear. In addition to the sequence, the vector can still reverse the container.


2. the deque template class double-ended queue (dual-end queue) supports random access. The time for inserting and deleting elements from the beginning of the deque object is fixed, unlike the linear time of the vector.
Therefore, most operations occur at the beginning and end of the sequence, so deque should be considered. The Design of A deque object is more complex than that of a vector object to ensure that the insertion and deletion operations are fixed at both ends of deque.
Therefore, although both provide random access to elements and linear time insertion and deletion operations in the middle of the sequence, the vector container performs these operations more quickly.


3. list (two-way linked list), except for the first and last elements, each element is connected to the front and back elements, which means that a two-way traversal chain table is supported. The key difference between list and vector is that, the insertion and deletion time of a list at any position in the linked list is fixed (the vector Template provides linear time insertion and deletion except the end, at the end, it provides a fixed period of time for insertion and deletion ). Therefore, vector emphasizes fast access through random access, while list emphasizes fast insertion and deletion of elements. List can also reverse the container. list does not support array representation and random access.


4. forward_list, which implements a single-chain table. In this linked list, each node is linked only to the next node, but not to the previous node. Therefore, forward_list only requires a forward iterator instead of a bidirectional iterator. Therefore, unlike vector and list, forward_list is a container that cannot be reversed. Compared with list, forward_list is simpler and more compact, but has fewer functions.


5. the queue and queue templates have more restrictions than deque. They do not allow random access to queue elements, or even traverse queues. It restricts the use of the basic operations on the definition queue. You can add elements to the end of the queue, delete elements from the first of the queue, and view the values of the first and last of the queue, check whether the number of elements and the test queue are empty.


6. The difference between priority_queue and queue is that the largest element is moved to the beginning of the queue. The internal difference is that the default underlying class is vector. You can modify the comparison method to determine which element is placed at the beginning of the team. The method is to provide a constructor.


7. stack provides a typical stack interface with more restrictions than vector. It not only does not allow random access to stack elements, but even does not allow traversal of stacks. It limits its usage to the basic operations for defining stacks, that is, it can push the push to the top of the stack, pop-up elements from the top of the stack, check the value of the top of the stack, and check whether the number of elements and the test Stack are empty. to use the value in the stack, you must first use top () to retrieve the value and then use pop to delete it from the stack.


Four associated containers



1. set. The value type is the same as the key, and the key is unique. This means that there will not be multiple identical keys in the set.


2. multiset can have multiple identical keys.


3. map stores key-value pairs.


4. multimap and multimap can also be reversed. Sorted containers are associated, but the key and value types are different, and the same key may be associated with multiple values.




Four unrelated containers unordered Container Association is another improvement to the concept of container. Like associated containers, unordered associated containers also associate keys with values and use keys to find values. However, the underlying difference is that the associated containers are tree-based, unordered associated containers are based on data structure hash tables.




1. unordered_set


2. unordered_multiset


3. unordered_map


4. unordered_mutimap
How to Use STL containers in VC ++?

Ordered containers: vector, deque, and list. The containers map and set are associated. The word "how to use" is equivalent to half a book ...... Let's take a look at C ++ Primer.

STL container Problems

We recommend that you download an ebook named <C ++ Templates (Simplified Chinese Version)>. pdf from CSDN. The book provides detailed instructions.

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.