What is STL?
STL: "standard template library. Basically, STL is a collection of containers, algorithms, and components. The collection of these algorithms and containers is a masterpiece of elites. It aims to standardize components and avoid repeated development.
I think we should start learning STL from list. Nothing else, because it is simple and easy to use.
First, we need to understand some basic concepts.
Template: macro (macro) of classes and structures ). Regular name: fan type (generic ).
The template of a class is called: generic class ).
The template of a function is: generic function ).
Container: A template class that can hold some data. STL contains vector, set, map, multimap, etc.
Vector: A Basic array template, which is a container.
Cursor (Iterator): a pointer to elements in the STL container or other elements.
This document describes how to define and initialize a list, calculate the number of its elements, and find and delete elements from a list.
1. Define list. We can define a list in this way.
// ...... Note: this is an error !!!.............. This is my first mistake.
# Include "String. h"
# Include "List. h" // Error !!
Using namespace std;
Int main (void)
{
List <string> Strlist;
Return 0;
}
The following error occurs during compilation:
Include \ list. h (37): error C2146: syntax error: missing '; 'before identifier 'length'
Include \ list. h (37): error C2501: 'dword': missing storage-class or type specifiers
Include \ list. h (37): error C2501: 'length': missing storage-class or type specifiers
Include \ list. h (53): error C2146: syntax error: missing '; 'before identifier' GetPrevLink'
Include \ list. h (53): error C2433: 'winapi': 'line' not permitted on data declarations
Include \ list. h (53): fatal error C1004: unexpected end of file found
The reason is actually very simple: "list" and "list. h "is completely different," list. h "is a two-way linked list in the form of c, and" list "is a standard c ++ container.
The correct method should be as follows:
# Include "string"
# Include "list"
Using namespace std;
Int main (void)
{
List <string> Strlist;
Return 0;
}
Compile the program and you will find that a large number of errors are missing. Instead, there are a lot of warnings. This is a normal phenomenon because the compiler does not support the Standard c ++ stl very well.
So far, we have defined a list and have not done anything.
Now let's load a string into this list. Before proceeding, we need to know something about "value type ".
"Value Type" indicates the type of objects in the list. Here it is a string.
We can use the list member functions push_back () and push_font () to insert elements into the list. It is called as follows:
# Include "string. h"
# Include "list"
Using namespace std;
Int main (void)
{
List <string> strlist;
Strlist. push_back ("waring1 :");
Strlist. push_back ("exception ");
Strlist. push_front ("error :");
Return 0;
}
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.
But how can we use the elements we put in? We can use Iterator to access them? A pointer !)
In specific operations, I have made some additional gains. I want to output a string. This string is of the string type, and the result shows that the string is not overloaded <error. I checked it to know that it should contain "string". Instead of "string. h". C ++ is extended from C, so generally "XXX. h" is the C header file, and "XXX" is the C ++ file. Please note that you should not make the same mistake as me.
Okay. traverse the list below.
# Include "string"
# Include "list"
# Include "iostream"
Using namespace std;
Int main (void)
{
List <string> strlist;
Strlist. push_back ("waring1 :");
Strlist. push_back ("exception ");
Strlist. push_front ("error :");
List <string >:: iterator Pstrlist;
Pstrlist = strlist. begin ();
For (; pstrlist! = Strlist. end (); pstrlist ++)
{
Cout <* pstrlist;
}
Return 0;
}
In this case, the binary "<": no accepted std: basic_string <_ Elem, _ Traits, _ Ax> "type right operand operator (or there is no acceptable conversion) error.
Because string does not have cout output overload, it should be changed to pstrlist
Note that the Iterator object is not overloaded or greater than> Or less than < , Only! =
In addition, we cannot use strlist in the list container. begin () + 2 points to the third object in the list, because the STL list is implemented by a double-stranded list, it does not support random access. Vector and deque (vector and double-end Queue) and some other STL containers support random access.
So far, we have a preliminary understanding of STL. This is just the beginning, just a fur. More exciting.
STL also designed general algorithms for us, so we don't need to do it even in a loop. Haha, great!
For example, we can use for_each () for the traversal above. It is done by STL for us and its convenience.
Remember to include the header file "algorithm"
# Pragma warning (disable: 4786)
# Include "string"
# Include "list"
# Include "iostream"
# Include "algorithm"
Using namespace std;
Void print (string & str)
{
Cout <str;
}
Int main (void)
{
List <string> strlist;
Strlist. push_back ("waring1 :");
Strlist. push_back ("exception ");
Strlist. push_front ("error :");
For_each (strlist. begin (), strlist. end (), print );
Return 0;
}
This is the source code of for_each ().
Template <class _ Init, Class _ Fn1> Inline
_ Fn1 For_each (_ init _ First, _ Init _ Last, _ Fn1 _ Func)
{
// Perform Function For Each Element
For (; _ First ! = _ Last; ++ _ First)
_ Func (* _ first );
Return (_ Func );
}
Here is a tip. If you hate warning4786 during compilation, you can add a parameter to block it.
# Pragma Warning ( Disable : Xxxx)
Xxxx is the warning number.
You can perform other similar operations.