C + + STL (Standard Template Library)

Source: Internet
Author: User

Why to use STL

The separation of data structures (such as vectors) and the separation of algorithms (e.g., the STL sort () function is completely generic and you can use it to manipulate almost any data set, including linked lists, containers, and arrays).

STL is actually a generic programming (Generic programming, abbreviated GP) is a parameterized (parameterization)-based programming technique: You can use a type to parameterize another type (for example, the element type of a vector is determined by the parameter) The algorithm can also parameterize another algorithm (for example, using a comparison function to parameterize a sort function). The purpose of GP is to generalize the useful algorithms or data structures as much as possible and make them optimal.

three basic STL Components

1) Iterators provide a way to access objects in the container. For example, you can use a pair of iterators to specify a range of objects in a list or vector. In fact, C + + pointers are also an iterator.

2) A container is a data structure, such as List,vector, and deques, provided as a template class method. To access the data in the container, you can use an iterator that is output by the container class.

3) The algorithm is a template function used to manipulate the data in the container. For example, the STL uses sort () to sort the data in a vector and search for an object in a list with find (). The functions themselves are independent of the structure and type of data they manipulate, so they can be used on any data structure from simple arrays to highly complex containers.

The STL flags are encapsulated in the namespace Std. For example, the STL sort () algorithm compiles to Std::sort ()

Comparison of several containers

Vector and array similar, it has a contiguous memory space, and the starting address is unchanged, so it can very well support random access, that is, the [] operator, but because its memory space is continuous, so in the middle of the insert and delete will cause a copy of the memory block, in addition, when the array of memory space is not enough, You need to reapply a large enough memory and make a copy of the memory.

A list is a doubly linked list in a data structure, so its memory space can be discontinuous, with pointers to the data access, which makes its random access becomes very inefficient, that is, there is no overload of the [] operator. However, due to the characteristics of the list, it can be very efficient to support the deletion and insertion anywhere.

Deque is a double-ended queue, its concrete implementation is not very clear, but know that it has the following two features:
It supports the [] operator, which supports random access, and is similar to the efficiency of a vector, which supports both
Operation: Push_back,push_front,pop_back,pop_front, and the efficiency of the list on both sides of the operation
also similar.

Map is an associative container of STL, its elements are key-value pairs, the implementation of the map inside the self-built a red black tree, the tree has the ability to automatically sort data.

Set is a set and an associative container, and the element is a key-value pair. The set does not contain duplicate elements, which is the first difference from vectors, and the second difference is that set is internally implemented with a red-black tree, which facilitates element lookups, while vectors use contiguous memory storage for easy random access.

Eg:

 set  s; //  S.insert (1   2  ); S.insert ( 1 ); //  insert repeating elements, and do not omit  s.insert (6  ); set  ::iterator i;i  = s.find (6 ); //  cout << *i << Endl; //  output element value  

Therefore, in the actual use, how to choose which of the several containers, should be based on your needs, should generally follow the following
Principle of:
1. If you need efficient random access and do not care about the efficiency of insert and delete, use vector
2, if you need a large number of insertions and deletions, and do not care about random access, you should use the list
3. If you need random access and are concerned with inserting and deleting data at both ends, you should use Deque.

4, if you want to store a data dictionary, and the need to easily find value based on key, then map is a good choice

5. If you want to find out if an element is in a collection memory, it is better to use set to store this set

Introduction to several algorithms

1. Sort function

You can sort directly on an array, with a complexity of N*log (n), the default sort is ascending, and the sort function requires # include.

This function can pass two parameters or three parameters. Sort (int a, int b) represents the sorted array interval [A, a, or a). Note The second parameter is the next address of the interval end address. For example, the order of int a[100] is sort (a,a+100), the vector v is sorted by sort (V.begin (), V.end ()). The third parameter is a comparison function that you can define yourself. The return value is type bool, and the type of the two parameter is the array type, which specifies what kind of relationship is "less than". Such as:

BOOL cmp (int A,int  b) {    return  A}

If you want to sort in descending order, just change "<" to ">".

2. Qsort function

There are four parameters, namely, array name, array length, sizeof (array type), Comparison function. such as: Qsort (a,100,sizeof (a[0]), CMP);

The definition of a comparison function differs from sort. For Int,char types

int cmp (voidvoid *b) {       return * (int *) A-* (int *) ) b;}

For double types

Sort the array of double types (especially note)

int   CMP (   const   void   *a   ,   const   void   *b   )    {    return   * (double   *) a   >   * (double   *) b   ?   1   :   -1;    }    

3. Find function

(1) Size_type string::find (char c) const;
(2) Size_type string::find (char c,size_type idx) const;
(3) Size_type string::find (const string& str) const;
(4) Size_type string::find (const string& str,size_type idx) const;
(5) Size_type string::find (const char* CSTR) const;
(6) Size_type string::find (const char* cstr,size_type idx) const;
(7) Size_type string::find (const char* cstr,size_type idx,size_type chars_len) const;

The above function returns STRING::NPOS if the lookup fails, if successful:
function (1) Returns the position of the first character from the beginning
function (2) returns the position of the first character starting with the IDX
Function (3) Returns the position of the first substring from the beginning
Function (4) Returns the position of the first substring starting from the IDX
Function (5) Returns the position of the first substring equal to the char* string from the beginning
Function (6) Returns the position of the first substring equal to the char* string starting from the IDX
Function (7) Returns the position of the first substring that is equal to the Chars_len character before the char* string starting from the IDX

If you can be sure that the string you are looking for is followed by or if there are multiple substrings at the same time, you should use the RFind () series or the Find_last_of () series function to find

C + + STL (Standard Template Library)

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.