C + + must be aware of (iv) STL

Source: Internet
Author: User
Tags data structures

The short description of the STL (Standard Template Library) is not enough to reflect its design prowess, and the following text is merely an "appetizer" that encourages you to delve into the STL.

STL is not only a library, it is an excellent idea as well as a set of conventions. STL contains three main components: container, algorithm and iterator. The container is used to hold and organize the elements, the algorithm performs the action, and the iterator is used to access the elements in the container. These are not new things, many traditional libraries also contain similar components, and many programs are implemented using templates.

The good idea of STL is that there is no need to know each other between the algorithms executed on the container and the container, and this trick is accomplished by iterators. An iterator is similar to a pointer (actually a pointer is an STL iterator). Like a pointer, an iterator can point to an element in a sequence or dereference it to get the value of the object it points to. We can manipulate the iterator to point to different elements in the sequence, like manipulating the pointer.

An STL iterator can be either a predefined pointer or a user-defined class type, and, of course, this type needs to overload the appropriate operator to have the same usage syntax as a predefined pointer.

STL Container is an abstraction of data structure, which is realized by the way of class template. Because of their different data structures, different containers organize elements in different ways to optimize access or manipulation. The STL defines 7 standard containers (8 for string), and some widely accepted non-standard containers are also implemented.

STL algorithm is a kind of abstraction of function, which is realized by function template. Most STL algorithms are used to process the values of one or more sequences, each of which is defined by a pair of ordered iterators. The first iterator points to the first element of the sequence, and the second iterator points to the position after the last element of the sequence. If two iterators point to the same location, they define an empty sequence.

Iterators provide a mechanism that enables containers and algorithms to work together. A container can generate a pair of iterators to specify a sequence of elements (either all elements or just a child interval), and the algorithm operates on the sequence. In this way, containers and algorithms can work together closely, while still keeping each other uninformed. In addition to containers, algorithms, and iterators, the STL also defines a large number of auxiliary functions. The algorithms and containers can be customized according to the needs of function pointers and function objects, and these function objects can be connected and combined through various object adapters.

Containers can also be connected by using a container adapter to modify the container's interface to stack, queue, and precedence queues. STL has a strong reliance on conventions. Containers and function objects must describe themselves through a set of standard nested type names. Both container and function object adapters require member functions to have a specific name and contain specific type information. The algorithm requires that the iterator passed to IT support specific operations and can identify what operations are. When you use or extend STL, if you abandon the agreement and disregard, then also give up the dream come true hope. Abide by the agreement, will have a simple and easy life.

The STL Convention does not specify specific implementation details, but it specifies an efficiency constraint on the implementation. In addition, because STL is a template library, many optimizations and performance tuning can be done at compile time. The naming and information conventions mentioned earlier have a significant impact on compile-time optimization. In general, the efficiency of an STL can be compared with the efficiency of an expert's handwriting code, but it is obviously better than the handwriting code of a common programmer and a programmer's team. In addition, using STL can make your code clearer and easier to maintain. Learn STL, and widely use STL bar!

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.