Summary of C + + STL structure

Source: Internet
Author: User

1. What is STL

Its full name is the Stand Template Library, the standard templates libraries, mainly to write some structure and algorithms into the template, so that can be implemented for any type of objects are able to operate, and do not need to go through some of the algorithm and structure.

It consists of the following three concepts:

Container--a structure that accommodates a variety of data types, is a class template

Iterator-A pointer to an object within a container, similar to a pointer, used primarily to point to an element

Algorithm-operation function of the element inside the container is a function template, such as a sort algorithm such as Sort,find, to find the algorithm, which is independent of the element type in the container.

2. Containers

The container for storing data can be divided into three main categories, sequential containers, associative containers, container adapters:

Sequential container--vector, list, deque

Associative container--map, Multimap, set, Multiset

Container adapter--stack, queue, priority_queue

2.1 Sequential Containers

The sequential container is unordered, and the inserted element position is independent of the value.

Vector container, header file <vector>, is a dynamic data, elements in memory is stored continuously, you can arbitrarily take the elements in the vector.

Deque, header file <deque>, is a two-way queue in which elements are stored continuously in memory, which can take any element in the container, unlike in vectors, it has a head pointer and a tail pointer, such as

List, header file <list>, is a doubly linked list, element memory is not contiguous storage, easy to add delete elements, not arbitrarily access elements.

2.2 Associative containers

The associative containers are ordered, the elements are inserted at the same time as the new elements are sorted, and the sorting depends on the member function overloads of the < symbol in the element class, and all containers are implemented with the balanced binary tree, and the lookup time is O (LGN).

Set/multiset, header file <set>, also known as the collection, the only difference is that set does not allow multiple elements to be equal, whereas multiset can hold multiple equal elements, as is the definition of two elements equal:

If element A is not less than element B and not greater than B, then A is equal to B. Therefore, it is still necessary to overload the < symbolic function of the element class inside the container to allow the container function to size the elements.

Map/multimap, header file <map>, also known as a dictionary, there are two member variables within the element: first and second, they correspond one by one, the container is sorted by first, first is the value of Key,second for first. Multimap allows for the presence of the same first element, while map requires that all element first values are unique.

2.3 Container Adapter

Stack, header file <stack>, stack, adherence to advanced post-out rules

Queue, header file <queue>, queuing, Element FIFO,

Priority_queue, priority queue, header file <queue>, internal in a certain order, the highest priority element first dequeue.

2.4 member functions in a container

The public member functions of the sequential container and the associated container:

begin--returns an iterator that points to the first element

end--returns the latter iterator to the last element

rbegin--returns an iterator that points to the last element

rend--returns the previous iterator that points to the first element

Erase--delete one or more elements in a container

Clear-Removes all elements within the container

member functions for sequential containers:

Front--Returns a reference to the first element

Back--Returns a reference to the last element

Push_back--Add a new element at the end of the container

Pop_back--Delete the element at the end of the container

Erase--Deletes an iterator to an element or all elements within a range, returning an iterator that deletes the next element of the element

3. iterators

A pointer-like usage that is used primarily to point to the location of an element object, defined as follows:

Container class Name:: Iterator it;

Container class Name:: Const_iterator it; Const iterator, value of non-modifiable object

Container class Name:: Reverse_iterator it; A reverse iterator, it++ points to the previous element, and if it is a forward iterator, point to the next element

A special iterator is a random iterator that can operate it + I, jumping directly into the I element

  

Summary of C + + STL structure

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.