CHAPTER8-18.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:2k
源码类别:

STL

开发平台:

C/C++

  1. //文件名:CHAPTER8-18.cpp
  2. #include <list>
  3. #include <iostream>
  4. #if _MSC_VER > 1020   // if VC++ version is > 4.2
  5.    using namespace std;  // std c++ libs implemented in std
  6. #endif
  7. int main( ) 
  8. {
  9.    using namespace std;
  10.    list <int> c1;
  11.    list <int>::iterator c1_Iter;
  12.    list <int>::reverse_iterator c1_rIter;
  13.    // If the following line had replaced the line above, an error would 
  14.    // have resulted in the line modifying an element (commented below)
  15.    // because the iterator would have been const
  16.    // list <int>::const_reverse_iterator c1_rIter;
  17.    c1.push_back( 10 );
  18.    c1.push_back( 20 );
  19.    c1.push_back( 30 );
  20.    c1_rIter = c1.rend( );
  21.    c1_rIter --;  // Decrementing a reverse iterator moves it forward in 
  22.                  // the list (to point to the first element here)
  23.    cout << "The first element in the list is: " << *c1_rIter << endl;
  24.    cout << "The list is:";
  25.    for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
  26.       cout << " " << *c1_Iter;
  27.    cout << endl;
  28.    // rend can be used to test if an iteration is through all of the 
  29.    // elements of a reversed list
  30.    cout << "The reversed list is:";
  31.    for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )
  32.       cout << " " << *c1_rIter;
  33.    cout << endl;
  34.    c1_rIter = c1.rend( );
  35.    c1_rIter--;  // Decrementing the reverse iterator moves it backward 
  36.                 // in the reversed list (to the last element here)
  37.    *c1_rIter = 40;  // This modification of the last element would have 
  38.                     // caused an error if a const_reverse iterator had 
  39.                     // been declared (as noted above)
  40.    cout << "The modified reversed list is:";
  41.    for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )
  42.       cout << " " << *c1_rIter;
  43.    cout << endl;
  44.    return 0;
  45. }