What is STL? STL is the standard template library, the standard template library. This may be the most boring term for the most exciting tool in history. Basically, STL is a collection of "containers", which include list, vector, set, map, and so on. STL is also Algorithm And other components. The collection of containers and algorithms here refers to the masterpiece of many smart people in the world for many years. The purpose of STL is to standardize components so that existing components can be used without re-development. STL is now part of C ++, so no additional installation is required. It is built into your compiler. Because STL list is a simple container, I plan to introduce how to use STL from it. If you understand this concept, there will be no other problems. In addition, the list container is quite simple and we will see this. In this article, we will see how to define and initialize a list, calculate the number of its elements, find elements from a list, delete elements, and some other operations. To do this, we will discuss two different algorithms. General STL algorithms can operate on more than one container, while List member functions are proprietary operations of list containers. This is a concise outline of three main STL components. STL containers can save objects, built-in objects, and class objects. They securely Save the object and define the interface of the object that we can operate on. The eggs on the egg won't roll to the table. They are safe. Therefore, objects in STL containers are safe. I know this metaphor sounds very old, but it is correct. STL algorithms are standard algorithms. We can apply them to objects in those containers. These algorithms have well-known execution features. They can sort, delete, count, and compare objects, find special objects, merge them into another container, and perform other useful operations. STL iterator is like a pointer to an object in a container. STL algorithms use iterator to operate on containers. Iterator sets the boundary of the algorithm, the length of the container, and other things. For example, some iterator only allows the algorithm to read elements, some allow the algorithm to write elements, and some allow both. Iterator also determines the processing direction in the container. You can call the container member function begin () to obtain an iterator pointing to the starting position of a container. You can call the end () function of a container to obtain the last value of the past (that is, the value of the stopped value ). This is all about STL, containers, algorithms, and iterator that allows algorithms to work on elements in containers. The algorithm operates the object in an appropriate and standard method, and the precise length of the container can be obtained through iterator. Once this is done, they will not "run out of the boundary ". There are also some other components with enhanced functionality for these core component types, such as function objects. We will see these examples. Now, let's take a look at the STL list. Define a list We can define an STL list as follows:
# Include <string> # Include <list> Int main (void) { List <string> milkshakes; Return 0; } |
You have defined a list. Simple? The list <string> milkshakes statement declares an instance of the List <string> template class, and then instantiates an object of the class. But don't rush to do this. In this step, you only need to know that you have defined a string list. You need to include the header file that provides the STL list class. I used GCC 2.7.2 to compile this test on my Linux.ProgramFor example: G ++ test1.cpp-O test1 note that the header file iostream. H has been abandoned by the STL header file. This is why this is not found in this example. Now we have a list. We can use it to install things. We will add a string to this list. A very important thing is the value type of list. The value type is the type of the object in the list. In this example, the Value Type of this list is string, because this list is used to put strings. Use the List member functions push_back and push_front to insert an element to the list:
# Include <string> # Include <list>Int main (void) { List <string> milkshakes; Milkshakes. push_back ("Chocolate "); Milkshakes. push_back ("strawberry "); Milkshakes. push_front ("Lime "); Milkshakes. push_front ("Vanilla "); Return 0; } |
We now have four strings in the list. The List member function push_back () puts an object behind a list, while push_front () puts the object in front. I usually put some error messages push_back () into a list, and then push_front () into a list, so that it will print it before the error message. The List member function empty () It is important to know whether a list is empty. If list is empty, the empty () member function returns true. I usually use it like this. I use push_back () to put error messages in the list. Then, by calling empty (), I can tell whether the program has reported an error. If I define a list to put information, a warning, and a serious error, I can use empty () to easily tell whether a type of error has occurred. I can sort these lists and use titles to sort them, or sort them into classes before printing them.
/* | Using a list to track and report program messages and status */ # Include <iostream. h> # Include <string> # Include <list> Int main (void) { # Define OK 0 # Define info 1 # Define warning 2 Int return_code; List <string> infomessages; List <string> warningmessages; // During a program these messages are loaded at various points Infomessages. push_back ("info: program started "); // Do work... Warningmessages. push_back ("Warning: no customer records have been found "); // Do work... Return_code = OK; If (! Infomessages. Empty ()){ // There were info messages Infomessages. push_front ("informational messages :"); //... Print the info messages list, we'll see how later Return_code = Info; } If (! Warningmessages. Empty ()){ // There were warning messages Warningmessages. push_front ("warning messages :"); //... Print the warning messages list, we'll see how later Return_code = warning; } // If there were no messages say so. If (infomessages. Empty () & warningmessages. Empty ()){ Cout <"there were no messages" <Endl; } Return return_code; } |
|