Input stream iterator
#include <iostream>#include<iterator>#include<algorithm>#include<vector>using namespacestd;intmain () {vector<int> V (4); Vector<int>::iterator it =V.begin (); cout<<"enter four ints separated by Space & a char:\n"; Istream_iterator <int> Head (CIN), tail; Copy (head, tail, it); Cin.clear (); cout<<"vector="; for(it = V.begin (); It! = V.end (); it++) {cout<< *it <<" "; } cout<<Endl; return 0;}
Table: A doubly linked list in the Standard Template Library
Use of tables
- Defines the container that contains the Point object:list<point> pts (8);
- Insert: Pts.insert (Pts.begin (), point (n));
- Table Header insert: Pts.push_front (point);
- Insert: Pts.insert (Pts.end (), point (n));
- Footer insertion: Pts.push_back (point);
- Defines a container that contains a point pointer: list<point*>ppts (8);
- Insert: Ppts.insert (Ppts.begin (), new Point);
- Insert: Ppts.insert (Ppts.end (), new Point);
- Delete: Delete Ppts.front ();pp Ts.remove (Ppts.front ());
- Determine if the table is empty: if (Pts.empty ()) cout << "empty!";
Tables and iterators
Iterators can work together with tables in the same way as vectors
List<int> A (8);
List<int>::iterator it;
for (it = A.begin (); It! = A.end (); it++)
*it = Generaterandomnumber (10,99);
Table sort
- Directly using the table's member functions: A.sort ();//Default Ascending
- Descending: Reverse ()
- Descending: (incoming functor) greater_equal<int> ()
- A.sort (greater_equal<int> ());
- For custom objects, you need to overload operator < for comparison
Standard algorithms
Functions that invoke the standard Template Library
Standard Sub-function
Arithmetic functor
Plus<t>
Minus<t>
Multiplies<t>
Divides<t>
Modulus<t>
Negate<t>
Relationship functor
Equal_to<t>
Not_equal_to<t>
Greator<t>
Greator_equal<t>
Less<t>
Less_equal<t>
Logical functor
Logical_and<t>
Logical_or<t>
Logical_not<t>
C + + Learning Note 34: Generics programming Development 3