home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch12 / vectralg.cpp < prev    next >
C/C++ Source or Header  |  2005-06-16  |  4KB  |  148 lines

  1. //             vectralg.cpp
  2. //
  3. // Synopsis  - Instantiates STL vector objects, and STL
  4. //             iterators. Applies some STL algorithms to
  5. //             the vector containers.
  6. //
  7. // Objective - To introduce the concepts of STL in general
  8. //             and the vector container in STL in particular.
  9. //
  10.  
  11. // Include files
  12. #include <vector>                                    // Note 1
  13. #include <algorithm>
  14. #include <iostream.h>
  15.  
  16. using namespace std;                                 // Note 2
  17.  
  18. void display( vector<int> v ); 
  19. // PRECONDITION:  none. 
  20. //
  21. // POSTCONDITION: The elements and some properties of v 
  22. //                have been displayed on cout.
  23.  
  24. void reverseIt( vector<int> v );
  25. // PRECONDITION:  none.
  26. //
  27. // POSTCONDITION: The elements in v have been displayed 
  28. //                on cout in reverse order.
  29.  
  30. void copyex( vector<int> v, vector<int> v1 );
  31. // PRECONDITION:  none. 
  32. //
  33. // POSTCONDITION: No change has occured to the parameters. The 
  34. //                results of invocations of the algorithm copy() 
  35. //                are displayed on standard output.
  36.  
  37. void for_each_ex( vector<int>& v ); 
  38. // PRECONDITION:  none.
  39. //
  40. // POSTCONDITION: Every integer in the vector will 
  41. //                have been multiplied by 2.
  42.  
  43. void mult_by_2( int& x );
  44. // PRECONDITION:  none.
  45. //
  46. // POSTCONDITION: x will be changed by multiplying it by 2.
  47.  
  48. int main ()
  49. {
  50.     vector<int> v1;                                  // Note 3
  51.  
  52.     v1.push_back (21);                               // Note 4
  53.     v1.push_back (4);
  54.     v1.push_back (16);
  55.  
  56.     cout << "Displaying vector v1:\n";
  57.     display( v1 );
  58.  
  59.     cout << "Displaying the elements of v in reverse\n" ;
  60.     reverseIt( v1 );
  61.  
  62.     vector<int> v2;
  63.     cout << "Displaying vector v2:\n";
  64.     display( v2 );
  65.  
  66.     v2.resize( 3 );                                  // Note 5
  67.     cout << "Displaying vector v2 after resizing:\n";
  68.     display( v2 );
  69.  
  70.     cout << "Copy Example\n";
  71.     copyex( v1, v2 );
  72.  
  73.     cout << "for_each example\n";
  74.     for_each_ex( v1 );
  75.  
  76.     while ( !v1.empty() ) {                          // Note 6
  77.         v1.pop_back();                               // Note 7
  78.     }
  79.     cout << "Displaying vector v1 after pop_back operations:\n";
  80.     display( v1 );
  81.  
  82.     return 0;
  83. }
  84.  
  85. /******************************** display() ******************/
  86. void display( vector<int> v )
  87. {
  88.     vector<int>::iterator it;                        // Note 8
  89.     if (v.empty() )                                  // Note 6
  90.         cout << "Vector is empty\n\n";
  91.     else {
  92.         cout << "size () = " << v.size () << endl;   // Note 9
  93.         cout << "front() is " << v.front() << endl;  // Note 10
  94.         cout << "back() is " << v.back() << endl;    // Note 11
  95.         for ( it = v.begin(); it != v.end(); it++ )  // Note 12    
  96.             cout << *it << " ";                      // Note 13
  97.         cout << endl << endl;
  98.     }
  99. }
  100.  
  101. /******************************** display() ******************/
  102. void reverseIt( vector<int> v )
  103. {
  104.     vector<int>::reverse_iterator p2;                // Note 14
  105.     for( p2 = v.rbegin(); p2 != v.rend(); p2++ )     // Note 15
  106.         cout << *p2 << " ";
  107.     cout << endl << endl;
  108. }
  109.  
  110. /******************************** copyex() *******************/
  111. void copyex( vector<int> v, vector<int> v1 )
  112. {
  113.     vector<int>::reverse_iterator r_it = v1.rbegin();    
  114.     copy( v.begin(), v.end(), r_it );                // Note 16
  115.     cout << "Displaying the second parameter to "
  116.          << "copyex after the first copy:\n";
  117.     display( v1 );
  118.  
  119.     if ( v == v1 )
  120.         cout << "v equals v1\n\n";
  121.     else cout << "v does not equal v1\n\n";
  122.  
  123.     copy( v.begin(), v.end(), v1.begin() );          // Note 16
  124.     cout << "Displaying the second parameter to "
  125.          << "copyex after the second copy:\n";
  126.     display( v1 );
  127.     
  128.     if ( v == v1 )                                   // Note 17
  129.         cout << "v equals v1\n\n";
  130.     else cout << "v does not equal v1\n\n";
  131.  
  132.     return;
  133. }
  134.  
  135. /******************************** for_each_ex() **************/
  136. void for_each_ex( vector<int>& v )
  137. {
  138.     for_each( v.begin(), v.end(), mult_by_2 );       // Note 18
  139.     cout << "Displaying vector v after the for_each:\n";
  140.     display( v );
  141.  
  142. /******************************** mult_by_2() ****************/
  143. void mult_by_2( int& x  )
  144. {
  145.     x *= 2;
  146. }
  147.