C++ STL  iterator, a very simple list program example using begin() and end()

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Creating and iterating the list element using begin() and end() in C++ programming

To show: How to use the iterator, to traverse the list element in C++ programming using begin() and end()

 

// C++ STL iterator, simple list example

#include <iostream>

#include <list>

using namespace std;

 

int main(void)

{

// lst, list container for character elements

list<char> lst;

 

// append elements from 'A' to 'Z' to the list lst container

for(char chs='A'; chs<='Z'; ++chs)

lst.push_back(chs);

 

// iterate over all elements and print, separated by space

list<char>::const_iterator pos;

cout<<"lst list: ";

for(pos = lst.begin(); pos != lst.end(); ++pos)

cout<<*pos<<' ';

cout<<endl;

return 0;

}

 

Output examples:

 

lst list: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index