1,Algorithm
Different from the specific language
Poor --- confirm that the process can end after the limited steps are completed
Confirm --- what are the specific actions of each statement?
Input and Output-all algorithms have outputs, screen printing, file writing, and database writing.
2. Quick sorting
If the number of data exceeds one, you can select one of the data as the demarcation value. Other data is divided into two groups based on the Size relationship. The demarcation value is in the middle.
Implement recursive reorganization of the two groups of data
View code
// A Fast Sorting Algorithm with the highest efficiency. The first parameter indicates the first address of the array, the second parameter indicates the starting position of the array, and the third parameter indicates the Ending position. Void Mysort ( Int * P, Int Left, Int Right ){ Int L = left; // Start from the left Int R = right; // Start from the right Int Povit = P [(left + right )/ 2 ]; // Use a data in the middle of the array as the demarcation point Do { While (P [l] <povit & L <right ){ // If the program exits cyclically, l finds a bigger one than himself. L ++ ;} While (P [R]> povit & R> Left) {r -- ;} If (L <= R ){ Int T = P [l]; P [l] = P [R]; P [R] =T; L ++ ; R -- ;}} While (L <= R ); // The condition is that two people have not met yet. If (R> left ){ // As long as the value on the right is larger than the value on the left, the loop will continue. Mysort (p, left, R );} If (L <right ){ // As long as the left side is larger than the right side, the loop will continue. Mysort (P, L, right );}}
3. directly use the system's qsort () function
Define a sorting rule by yourself
4. templates
(1) The template parameter must appear at least once to determine the type.
(2) It can only be used in followed functions, and function declaration follows
Declare Multiple Template types. <class T1, class T2>
Class keywords cannot be omitted
(3) For the template type requirements, you must be able to reload ">", "<", "="
Suggestion: Do not use multiple operators when using one operator During encoding to avoid multiple overloading.
(4) functions written using templates are called function templates.
Function template determines the type when calling
A class written with a template is called a class template.
Templates can be used for data type, parameter type, and function return type.
A class template is not a class, but an incomplete class.
The class template must be specified with the class name <int> During declaration to determine the type.
(5) C ++ generics (templates) are types determined during compilation-efficiency
Java generics are Runtime
(6) template class declarations and definitions (Multi-file structure) cannot be separated
The declaration and definition of the template function can be separated. The template <class T> must appear in both the header file and the implementation file.
5. STL contains three categories: Container class (Objects of other objects can be stored), algorithm (a series of encapsulated functions), and iterator (classes used to traverse operations)
Containers can store objects directly or object pointers. MatureProgramUsers prefer indirect storage.
The container mainly includes two types: sequence class (generally linear storage) and Association class (generally non-linear storage ).
Vector ---- variable array length does not provide the function of removing the Header element from pop_front ()
List ----- linked list
(1) When vector V [1000] crosses the border, a segment error occurs.
V. ( 1000 ) When it is out of bounds, an exception of out_of_range will be thrown, and V. size () returns the length. You can use this loop to iterate v. empty () determines whether the container is empty iterator: * Operation *Iterator ITER -> Name <=> (* ITER). Name ITER ++ V. Begin () points to the beginning of the array v. End () points to the end of the last element of the array, which is an end sign Vector < Int > V1; vector < Int >:: Iterator it; // Iterator is an internal class of vector. For (It = v1.begin (); it! = V1.end (); It ++) Cout <* It < Endl; V. insert (ITER, 5 ); // Insert 5 before the element specified by ITER V. insert (ITER, 5 , 100 ); // Insert 5 100 before the element specified by ITER Such an insert operation will invalidate the original iterator, and assign a new value to the new device, so that you can continue to use it.
(2) List
Do not do ()
Push_front () and pop_front ()
ITER cannot add n
Used for frequent insert/delete operations
6. Associated containers
(1) Map
Suitable for key-based value search operations
The storage is sorted by key value, and the key value is unique.
Map < Int , Student> M; student s ( 1 , " Liucy " ); M. insert (Map < Int , Student> : Value_type (S. GETID (), s )); // Create a pair and coexist to the first position of map. value_type is a static function of map. Student S2 ( 4 , " Tangliang " ); M. insert (Map < Int , Student> : Value_type (s2.getid (), S); Map < Int , Student> : Iterator it; For (It = M. Begin (); it! = M. End (); It ++ ) {Cout <It-> first < " " <It-> Second; cout <Endl ;}
It is not safe to use [] queries in Map
M. Find (1); // query the value of key 1
Returns an ITER pointing to the key-value pair found. If not, the ITER will be equal to the ITER. End () value.
(2) multimap
Duplicate keys are allowed.
Search: multimap < Int , Student> : Iterator it; multimap < Int , Student> : Iterator lt; multimap < Int , Student> : Iterator ut; lt = M. lower_bound ( 1 ); UT = M. upper_bound ( 1 ); For (It = lt; it! = Ut; it ++ ) {Cout <It-> first < " " ; Cout <It-> second <Endl ;}
(3) set
Duplicate data cannot be inserted in the Set, which is equivalent to the key in the map.
You do not need to specify a location when inserting data.
Because it is consistent with the key in the map, the sorting feature is still retained.
(4) Multiset
Like vector, the only difference is that the sorting feature is retained.
7. The template declaration and implementation are written in the header file.
/Usr/local/include/C ++/3.2/