Article ID: 157671
Article Last Modified on 9/23/2003
<iterator>
template<class InIt, class Dist>
void advance(InIt& it, Dist n);
iterator += n.Otherwise, it performs each increment by evaluating:
++iterator.If the iterator is an input or forward iterator type, n must not be negative.
//////////////////////////////////////////////////////////////////////
//
// Compile options needed: /GX
//
// <filename> : Advance.cpp
//
// Functions:
//
// advance()
//
// Written by Linda Koontz
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
/* Compile options needed: /GX
*/
#include <iostream>
#include <string>
#include <list>
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
#pragma warning (disable:4786)
typedef list<string, allocator<string> > STRLIST;
void main() {
STRLIST List;
STRLIST::iterator iList;
STRLIST::difference_type dTheDiff;
List.push_back("A1");
List.push_back("B2");
List.push_back("C3");
List.push_back("D4");
List.push_back("E5");
List.push_back("F6");
List.push_back("G7");
// Print out the list
iList=List.begin();
cout << "The list is: ";
for (int i = 0; i < 7 ; i++, iList++)
cout << *iList << " ";
// Initialize to the first element"
iList=List.begin();
cout << "\n\nAdvance to the 3rd element." << endl;
advance(iList,2);
cout << "The element is " << *iList << endl;
dTheDiff = distance( List.begin(), iList);
}
Program Output is: The list is: A1 B2 C3 D4 E5 F6 G7 Advance to the 3rd element. The element is C3
Additional query words: STL STLSample
Keywords: kbinfo kbtemplate kbstl kbcode KB157671