Introduction and application of STL Library in C + +

Source: Internet
Author: User
Tags data structures

As an indispensable part of the C + + standard, the STL should permeate the corner of C + + program. The STL is not the pet in the lab, nor the device on the programmer's desk, her excitement is not a flash in the pan. This tutorial is designed to disseminate and popularize the basic knowledge of STL, if can take this opportunity for the extension of STL to do some things, to be a happy thing.

Initial knowledge STL: Answer some questions

1.1 One of the most concerned questions: what is STL

"What is STL. "If you know very little about STL, then I think you must be very interested to know the answer to this question, and frankly, it is not easy to hope to articulate this problem in just a few words." Therefore, if you are after reading this section still feel indefinitely, big can not worry, after reading the following content, I believe that your understanding of the STL, will be more clear, accurate and complete. But does that sound a bit like a vindication of their poor expressive power? :)

I wonder if you've ever had such an experience. When you are ready to finish the homework that the teacher assigned to the data structure, or when you add a new feature to a software project that you are responsible for, you find that you need to use a list or a map, but there is no ready-made code on hand. So it's unavoidable to manually implement the list or map before you begin to formally consider the program's functionality. So ... and eventually you finished the task. Perhaps at this time, as a highly literate programmer you still will not give up (or a like lazy top students:), because you would think, if you still encounter such a situation to do. There's no need to do the same thing again.

If that happens every day, it may be a bit exaggerated. But if the entire software sector has been struggling for a purpose for decades--reusability (reusability)--that does not seem to be an exaggeration. From the earliest process-oriented library, to object-oriented program design ideas, to various component technologies (e.g., COM, EJB), to design patterns, and so on. And STL is doing similar thing, at the same time it contains a kind of new program design idea--universal design (generic programming).

Continue to the example mentioned above, if you keep the list or map intact, you are secretly proud. Wait a minute, if the next list is not a floating-point number but an integer. If you implement a map that is always less satisfying and sometimes a bit of a bug. How do you deal with these problems? Using STL is a good choice, and indeed, the STL can pretty much solve the problems mentioned above, although you can also look for other ways.

Say for a long time, in the end STL is what thing.

STL (Standard Template Library), the standard template base, is an industry-intensive, efficient C + + program library. It is housed in the C + + standard library (c + + Standard library) and is the newest and most revolutionary part of the Ansi/iso C + + standard. The library contains many basic data structures and algorithms commonly used in the field of computer science. For the vast number of C + + programmers to provide an extensible application framework, highly reflects the software reusability. This behavior is similar to the MFC (Microsoft Foundation Class Library) in Microsoft Visual C + +, or VCL in Borland C + + Builder (visual Component Library), for both, we must not be unfamiliar with it.

From the logical level, in the STL embodies the idea of the universal design (generic programming), introduced a lot of new nouns, such as demand (requirements), concept (concept), models (model), Container (container), algorithm (ALGORITHMN), iterative sub (iterator), etc. Like the polymorphism (polymorphism) in OOP (object-oriented programming), generics are also a software reuse technique.

From the implementation level, the entire STL is implemented in a type parameterized (kind parameterized) way, based on a language feature-template (template) that was not present in the previous C + + standard. If you look up any version of the STL source code, you will find that the template as the cornerstone of the entire STL is a true thing. In addition, there are many new C + + features for the implementation of STL to provide convenience.

I don't know how you feel about all the jargon coming out of here, I hope it doesn't make you unhappy. If you are not familiar with most of them, please be assured that these nouns will be discussed in the following sections. As mentioned at the beginning.

Interestingly, there is another explanation for STL--stepanov%26amp; Lee, the former refers to the founder of Alexander Stepanov,stl, and the latter is Meng Lee, she is also to enable the implementation of the STL, the first STL finished product is their cooperation to complete. This reference originated in March 1995, Dr.dobb's journal special correspondent, a famous technical book writer Al Stevens an interview with Alexander Stepanov.






1.2 Roots: The History of the STL


When you meet new people, most people can't help but want to know the other person's past. This section will give you a brief overview of the STL past.

Known as the father of the STL Alexander Stepanov, born in Moscow, Soviet Union, as early as the second half of the the 1970s, he has begun to consider, in the premise of ensuring efficiency, the algorithm from a variety of specific applications of the possibility of abstraction, this is later on the embryonic form of the pan-shaped thinking. To test his thinking, he developed a language called Tecton, with Deepak Kapur, a professor at the State University of New York, and Professor David Musser of the University of Lille technology. Although this attempt did not achieve practical results at the end, it gave Stepanov a great revelation.

In the years that followed, he and David Musser have built large libraries in the schema language (a variant of Lisp) and the ADA language. In the meantime, Alexander Stepanov began to realize some of the problems that existed in object-oriented programming at the time, such as the flaws in the concept of abstract data types. Stepanov hopes to form a conceptual framework for software design by classifying various components in the software domain.

1987 years or so, Alexander Stepanov, who works in Bell Labs, began the first time using C + + language to research generic software libraries. Unfortunately, then the C + + language has not introduced the syntax of the template (template), now we can clearly see that the template concept of the implementation of the STL, how important. By the time it is, there is no alternative to adopting an inheritance mechanism. Nevertheless, Stepanov has developed a vast algorithm library. At the same time, in the process of working with top gurus such as Andrew Koenig (chairman of the former ISO C + + standardization Committee) and Bjarne Stroustrup (founder of the C + + language), Stepanov began to notice that C + + The potential advantages of language in the realization of its generic ideas. In the case of the pointer in C/s + +, it is flexible and efficient use, so that later STL in the realization of the universal, but also to maintain high efficiency. In addition, the iterative sub concept, which occupies an extremely important position in STL, is derived from the abstraction of C + + central Plains (native pointer).

In 1988, Alexander Stepanov began to work in HP's Palo Alto Lab, and for the next 4 years he worked on disk drives. It was not until 1992 that he returned to the research of the generics algorithm by participating in and chairing a research project on the algorithm established by the Director of Laboratory, Bill Worley. After the project was established, the participants were gradually reduced from the initial 8, leaving only two--stepanove himself and Meng Lee. After a long period of hard work, eventually, faith and sweat in exchange for a large number of data structures and algorithmic components of a large runtime. This is now the embryonic form of the STL (also is an implementation of the STL version--HP STL).

In 1993, Andrew Koenig at Bell Labs saw the results of Stepanove's research and was excited. With his encouragement and help, stepanove the San Jose for the Ansi/iso C + + Standards Committee (titled "The Science of C + + programming") and told the commissioners about their ideas. Then, in March the following year, at the San Diego meeting, a proposal was submitted to the Committee with a view to making the STL part of the C + + standard library. Although the proposal was so large that it lowered the likelihood of being adopted, the result of the vote was, by overwhelming majority, a postponement of the decision on the proposal because of the new ideas it contained.

Then, with the help of all, including Bjarne Stroustrup, Stepanove also improved the STL. At the same time, it also adds an abstract module which encapsulates the memory mode information, which is now the allocator in STL, which makes most of the implementation of the STL can be independent of the specific memory mode, thus it is independent of the concrete platform. In the summer of Waterloo meeting, members of the 80% in favour, 20% against, and finally passed the proposal, decided to formally incorporate STL into the C + + standardization process, then the STL was put into the conference working paper. Since then, the STL has finally become an important member of the C + + family.

Since then, with the continuous improvement of C + + standards, STL is constantly evolving accordingly. Until 1998, the Ansi/iso C + + standard formally finalized, STL is always the C + + standard is an indispensable part.








A. When to use STL

If, sometimes, you want to use the heap, stack, queue, list and other basic algorithms in the program, and you do not want to implement the data structure of the cumbersome algorithms in the textbook, then you can consider using STL.

In addition, STL as a standard, easy to communicate, master it, on the one hand you can write the program, easy to let others understand, on the other hand you can easily understand other people write procedures.

two. What is STL

All called Standard Template Library, Standard Template Gallery

To use STL, understand the following basic concepts:

Container: It can be understood as the place where the data is stored, some commonly used containers have List (list) stack (stack) dynamic array (vector) two-terminal queue (deque) queue (map)

Cursor (iterator): it can be interpreted as a pointer type, and many functions in the STL need to use them as parameters

Algorithms: They usually need to be used in conjunction with containers and cursors, so that you can easily perform a variety of common operations on the data in the container, such as sorting operations, searching for the maximum element operation, etc.

STL (2)--using

first, the use of container templates

There are roughly 6 steps to follow:

1. Add the corresponding header file (e.g. #include <list>) ( note, no. h )

2. Add STD namespaces (using using namespace std;)

3. Give the template specific use type (such as typedef list<string> LISTSTR;)

4. Instantiate the template (e.g. LISTSTR test;)

5. Instantiated cursors (e.g. liststr::iterator i;)

6. Access the template object through an iterator object, for example

An element in the output list test
for (i = Test.begin (); I!= test.end (); ++i)
cout << *i << "";

ii. Common functions in container templates

assign () assignment

Returns a value other than 0 if the empty () container is empty

Erase () deletes the element in the specified location or range

Push_front () inserts an element from the container's head

push_back () inserts the element from the container tail

Pop_front () deletes the first element

pop_back () deletes the last element

Back () returns a reference to the last element

Front () returns a reference to the first element

begin () returns the cursor to the first element (used in conjunction with the iterator)

   end ()                   returns the cursor   (the last 1 elements plus 1) to the last position of the final element   (used in conjunction with the iterator)

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.