C + + Standard Template Library (STL) Rollup __c++

Source: Internet
Author: User
Tags data structures

I. Overview of STL Container

C + + STL (Standard Template Library Standard Template gallery) is a universal class template and algorithm collection, it provides us with some standard data structure implementation such as queues (queue), lists (linked list), and stacks (stack) and so on. When we use C + + programming, we often use the STL container, which is very simple and convenient.

STL containers can be grouped into the following three broad categories:

Sequential structure:

Vectors (Dynamic Array)

Lists (linked list)

Double Ended queues (bidirectional queue)

Common functions:

Add an element at the end push_back ()
Remove the last element Pop_back ()
Inserts an element insert ()

Deletes the specified element erase ()

Container adapters:

Stacks (Stack)

Queues (queue)

Priority queues (priority queue)

Common functions:

add element push ()
remove element Pop ()

Union container:

Bitsets

Maps

Multimaps

Sets

Multisets

Common functions:

Insert element: Insert ()
Delete element: Erase ()

The most difficult part of our programmer's use of complex data structures has been done by the STL. If we want to use a stack that contains int data, we just write the following code:

Stack<int> Mystack;

Next, we simply invoke the push () and pop () functions to manipulate the stack.  With the power of C + + templates, he can specify any type of data, not just int. STL Stack implements the functionality of the stack, regardless of what data type is accommodated.

Ii. Summary of STL container

"C + + STL temperature is known new 001" Vector containers and iterator iterators

Three, STL container comparison

Vector Deque List Set Multiset Map Multimap
Name Vector container Two-way queue container List container Collection Multiple collections Mapping Multiple mappings

Internal number

According to the structure

The array form of contiguous storage (one end of the open Group)

contiguous or segmented contiguous storage arrays (both ends

An array of openings)

Two-way ring linked list Red-black tree (balanced search binary tree) Red and black Trees Red and black Trees Red and black Trees

Header file #include <vector> #include <deque> #include <list> #include <set> #include <set> #include <map> #include <map>
How elements are manipulated Subscript operator: [0] (can be iterator, but will fail when inserting delete operation)

Subscript operator or iterator

Use of the subscript operator is not supported only by using iterators (constantly pushing new values with variable values, equivalent to pointers)

Iterators Iterators Iterators Iterators
Insert Delete operation iterator is invalid Inserting and deleting elements will invalidate the iterator Inserting any element will invalidate the iterator. Deletes the header and tail elements, points to the deleted node iterator invalidation, and removing the intermediate element invalidates all iterators Insert, the iterator will not fail. Delete, point to deleted node iterator invalidation Insert, the iterator will not fail. Delete, point to deleted node iterator invalidation Insert, the iterator will not fail. Delete, point to deleted node iterator invalidation Insert, the iterator will not fail. Delete, point to deleted node iterator invalidation Insert, the iterator will not fail. Delete, point to deleted node iterator invalidation

Vector Deque List Set Multiset Map Multimap
Name Vector container Two-way queue container List container Collection Multiple collections Mapping Multiple mappings

Characteristics

Increase and gain element efficiency

Very high, inserting and deleting the

Efficiency is very low

Increase and gain element efficiency

higher, inserting and deleting the

High efficiency

Increase and gain element efficiency

Very low, inserting and deleting the

Efficiency is high

1. Keys (keywords) and values (data) are equal (that is, the template has only one parameter, keys and values together)

2. Key Unique

3. Element defaults in ascending order

1. Keys and values Equal

2. Keys can be not unique

3. Element defaults in ascending order

1. Key and value separate (template has two parameters, the front is the key after the value)

2. Key Unique

3. Element default keys in ascending order

1. Key and value separate

2. Keys can be not unique

3. Element default keys in ascending order

Defining containers

Vector<string> Book (50); Deque<string> Book (50); List<string> Book; Set<string> Book; Multiset<string> Book; Map<int,string> Book; Multimap<int,string> Book;

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.