1. STL
STL (Standard Template Library) is an important part of the C ++ language standard. STL takes the form of template classes and template functionsProgramProvides various data structures andAlgorithmIf programmers can make full use of STLCodeSpace, execution time, and coding efficiency are greatly improved.
STL can be roughly divided into three categories: algorithm (algorithm), container (container), and iterator (iterator ).
STL containers are some template classes and provide a variety of common methods for organizing data, such as vector, array-like, list, And deque) set, MAP, stack, queue, priority_queue, etc. The element type in the container can be specified through template parameters.
STL algorithms are some template functions that provide a considerable number of useful algorithms and operations, from simplicity such as for_each (traversal) to complexity such as stable_sort (stable sorting ).
The STL iterator is a generalization of pointers in C and is used to associate algorithms with containers. Almost all STL algorithms use iterators to access element sequences, and each container in STL also defines its proprietary iterator to access elements in containers. Interestingly, normal pointers can also work like iterators.
After you are familiar with STL, you will find that many functions can be implemented in just a few lines. With STL, we can construct elegant and efficient code, even better than the code you manually implement.
Another feature of STL is that it is provided free of charge by means of source code. programmers can not only freely use the code, but also learn its source code and even modify it as needed.
The following is the code for question 1067 -- uugly numbers written in STL:
# Include <iostream>
# Include <queue>
Using namespace STD;
Typedef pair <unsigned long, int> node_type;
Main ()
{
Unsigned long result [1500];
Priority_queue <node_type, vector <node_type>, greater <node_type> q;
Q. Push (make_pair (1, 2 ));
For (INT I = 0; I <1500; I ++)
{
Node_type node = Q. Top (); q. Pop ();
Switch (node. Second)
{
Case 2: Q. Push (make_pair (node. First * 2, 2 ));
Case 3: Q. Push (make_pair (node. First * 3, 3 ));
Case 5: Q. Push (make_pair (node. First * 5, 5 ));
}
Result [I] = node. first;
}
Int N;
Cin> N;
While (n> 0)
{
Cout <result [n-1] <Endl;
Cin> N;
}
Return 1;
}
Ii. Use STL
In the C ++ standard, STL is organized into the following header files (note that there is no. h suffix !) :
Algorithm/deque/functional/iterator/LIST/Map
Memory/numeric/queue/set/stack/utility/vector
When we need to use a function of STL, We need to embed the corresponding header file. Note that in the C ++ standard, STL is defined in the STD namespace. For example:
# Include <stack>
Main ()
{
STD: Stack <int> S;
S. Push (0 );
...
Return 1;
}
If you want to directly reference STL in the program, you can also use the using namespace statement to import the STD namespace after embedding the header file. For example:
# Include <stack>
Using namespace STD;
Main ()
{
Stack <int> S;
S. Push (0 );
...
Return 1;
}
STL is a model for the use of C ++ language mechanisms. By studying STL, you can better understand the ideas and methods of C ++ language. In this seriesArticleInstead of in-depth analysis of STL, I just want to introduce some basic STL applications.
this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/camel_flying/archive/2009/08/17/4454169.aspx