Microsoft KB Archive/168046

= HOWTO: STL Sample for [list::rbegin, rend, reverse, rev_iterator] =

Article ID: 168046

Article Last Modified on 9/23/2003

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li> Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

-

<div class="notice_section">

This article was previously published under Q168046

<div class="notice_section">

Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

<div class="summary_section">

SUMMARY
The sample code below illustrates how to use the [list::rbegin, list::rend, list::reverse_iterator, list::reverse] STL function(s) in Visual C++.

NOTE: There are some differences in the implementation of the Standard C++ Library components in Visual C++ version 4.2 versus later revisions. The relevant sections of code below compile conditionally based upon the value of _MSC_VER.

<div class="moreinformation_section">

Prototype
const_reverse_iterator rbegin const; reverse_iterator rbegin; const_reverse_iterator rend const; reverse_iterator rend; typedef reverse_bidirectional_iterator<iterator, value_type, reference, A::types<T>::pointer, difference_type> reverse_iterator; void reverse; NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description
This example shows how to iteratate through a list backwards. It also shows how to reverse a list.

Sample Code
//////////////////////////////////////////////////////////////////////   //    // Compile options needed: -GX //   // rev.cpp :  This example shows how to iteratate through a list //           backwards. It also shows how to reverse a list. //   // Functions: //   //  list::rbegin // list::rend // list::reverse_iterator // list::reverse //   // Written by Andrew Bradnan // Copyright (c) 1996 Microsoft Corporation. All rights reserved. //////////////////////////////////////////////////////////////////////   #pragma warning(disable:4786) #include #include #include using namespace std;

#if _MSC_VER > 1020  // if VC++ version is > 4.2 using namespace std; // std c++ libs implemented in std #endif

typedef list<string, allocator > LISTSTR;

// We use this object so that list::sort can sort in a different // order.Normally greater<> is used. We override it here to switch to  // less. struct less_str : greater { bool operator(const string& _X, const string& _Y) const {return (_X < _Y); } };

void main {     LISTSTR test; LISTSTR::iterator i;     LISTSTR::reverse_iterator r;

test.push_back("one"); test.push_back("two"); test.push_back("three");

// one two three cout << "Forwards" << endl << " "; for (i = test.begin; i != test.end; ++i) cout << *i << " "; cout << endl;

// three two one cout << "Backwards" << endl << " "; for (r = test.rbegin; r != test.rend; ++r) cout << *r << " "; cout << endl;

// Switch the order test.reverse;

// three two one cout << "Reversed" << endl << " "; for (i = test.begin; i != test.end; ++i) cout << *i << " "; cout << endl;

// sort alphabetically (decending by default) test.sort;

// two three one cout << "Sorted descending" << endl << " "; for (i = test.begin; i != test.end; ++i) cout << *i << " "; cout << endl;

test.sort(less_str);

// One three two cout << "Sorted ascending" << endl << " "; for (i = test.begin; i != test.end; ++i) cout << *i << " "; cout << endl; }

Program Output
<pre class="fixed_text">  Forwards one two three Backwards three two one Reversed three two one Sorted descending one three two Sorted ascending two three one

<div class="references_section">