This chapter describes the general introduction of containers, iterators, algorithms, and function simulation. It is a summary of chapter 6, 7, and 8.
The basic concept of STL is to separate data from operations. Data is managed by container categories, and operations are defined by custom algorithms. The iterator acts as an adhesive between the two. Any algorithm can interact with any container.
The container category is used to manage a group of elements.
In general, containers are divided into two categories:
①. Sequence containers is an ordered cluster. Including vector, queue, and list.
②. Associative containers is a sorted cluster. Including set, multiset, map, and multimap.
The associated container Automatically sorts its elements based on specific sorting rules. The sorting criterion is presented as a function to compare the value or key of an element ). By default, operator <is used for comparison. However, you can also provide a comparison function to define different sorting rules.
Eg
Set <int> tk1; // sort data in ascending order
Set <int, greater <int> tk2; // sort data in ascending order.
Collation
Generally, the associative container is implemented by the binary sorting tree.
In addition, the C ++ Standard Library provides the Container adapter (Container Adpters), stack, queue, and priority queue.
About the key/value of pair.
It can be accessed through first and second.
Eg
Pos-> first;
(* Pos). second;
Iterator category:
①. Bidirectional iterator, which is provided by list, set, multiset, map, and multimap.
② Random access iterator, which is provided by vector, deque, and strings.
Iterator Adapter)
① Insert iterators (Insert iterator)
Including back_inserter (), front_inserter (), inserter (), and push_back (), push_front (), and insert ().
② Stream iterators (Stream iterator)
Istream_iterator <T> (cin) reads the T type,
Istream_iterator <T> () generates a "stream Terminator"
Ostream_iterator <T> (cout) standard reading
③ Reverse iterators (Reverse iterator)
The following example uses three iterators:
// Author: Tanky Woo
// Blog: www.WuTianQi.com
# Include <cstddef>
# Include <iostream>
# Include <vector>
# Include <list>
# Include <deque>
# Include <set>
# Include <algorithm>
# Include <iterator>
Using namespace std;
Int main ()
{
List <int> coll1;
For (int I = 1; I <= 9; ++ I)
Coll1.push _ back (I );
Cout <"insert iterator \ n ";
Vector <int> coll2;
Copy (coll1.begin (), coll1.end (), back_inserter (coll2 ));
Deque <int> coll3;
Copy (coll1.begin (), coll1.end (), front_inserter (coll3 ));
Set <int> coll4;
Copy (coll1.begin (), coll1.end (), inserter (coll4, coll4.begin ()));
Copy (coll1.begin (), coll1.end (), ostream_iterator <int> (cout ,""));
Cout <endl;
Copy (coll2.begin (), coll2.end (), ostream_iterator <int> (cout ,""));
Cout <endl;
Copy (coll3.begin (), coll3.end (), ostream_iterator <int> (cout ,""));
Cout <endl;
Copy (coll4.begin (), coll4.end (), ostream_iterator <int> (cout ,""));
Cout <endl;
Cout <"--------------------------------------------------- \ n ";
Cout <"reverse iterator \ n ";
Copy (coll1.rbegin (), coll1.rend (), ostream_iterator <int> (cout ,""));
Cout <endl;
Cout <"--------------------------------------------------- \ n ";
Cout <"stream iterator \ n ";
Vector <int> coll5;
Copy (istream_iterator <int> (cin), istream_iterator <int> (), back_inserter (coll5 ));
Copy (coll5.begin (), coll5.end (), ostream_iterator <int> (cout ,""));
Cout <endl;
System ("PAUSE ");
Return EXIT_SUCCESS;
}
Not to mention, I will summarize the details later.