The Standard C++ and Boost Libraries
Examples for Standard C++ Libraries

Examples for Standard C++ Libraries

Table of Contents

Iterators
MMP File for Iterators
Source Code for Iterators

 


Iterators

This example demonstrates the usage of iterators in two ways:

  • Usage1: It demonstrates how to use an iterator for manipulating a container (vector). Use the iterator to find all the elements in the vector. To insert the corresponding value into the vector after moving to a position assignment, use the insert_iterator function.

    Note: If insert_iterator is used several times to assign, then several elements are inserted into the underlying container.

  • Usage2: An output iterator provides a mechanism for storing (but not necessarily accessing) a sequence of values. One example of output iterator is a tape. The user can write a value to the current location and advance to the next location, but cannot read values and cannot rewind. An ostream_iterator is an output iterator that performs formatted output of objects, of type T to a particular ostream. The function usage2 copies the elements of an array to the standard output, separated by hyphens (-) and the elements of the same array to a file TEST.DAT, one per line.

 


MMP File for Iterators

TARGET		  OpenCPPiteratorsEx.exe
TARGETTYPE		  exe
UID		 0 0xE28A7700

USERINCLUDE		 ..\inc

SOURCEPATH      .		.\data
START RESOURCE  		OpenCPPiteratorsEx_reg.rss
#ifdef WINSCW
TARGETPATH 		    \private\10003a3f\apps
#else
TARGETPATH 		    \private\10003a3f\import\apps
#endif
END //RESOURCE

SOURCEPATH	  	..\src
SOURCE		OpenCPPiteratorsEx.cpp

SYSTEMINCLUDE	   	 \epoc32\include 
SYSTEMINCLUDE		\epoc32\include\stdapis
SYSTEMINCLUDE		\epoc32\include\stdapis\sys
SYSTEMINCLUDE		\epoc32\include\stdapis\stlport
SYSTEMINCLUDE		\epoc32\include\stdapis\stlport\stl


STATICLIBRARY		libcrt0.lib
LIBRARY		libstdcpp.lib
LIBRARY		libc.lib
LIBRARY		libpthread.lib
LIBRARY		euser.lib

OPTION CW -wchar_t on
MACRO  _WCHAR_T_DECLARED

 


Source Code for Iterators

Following code snippet demonstrates the usage of iterators to write applications using opencpp_plugin.

#include<iostream>
#include<vector>
#include<algorithm>
#include<fstream>
#include<iterator>


// This is a GCCE toolchain workaround needed when compiling with GCCE
// and using main() entry point

#ifdef __GCCE__
#include<staticlibinit_gcce.h>
#endif

using namespace std;

int usage1()
{
cout<<"Example to demonstrate insert Iterators\n";
vector<int>v;
vector<int>::iterator itr;
int i;
for(i=0; i<5; i++)
v.push_back(i);
cout << "Original array: ";
itr = v.begin();
while(itr != v.end())
cout << *itr++ << " ";
cout << endl;
itr = v.begin();
itr += 2; // point to element 2

// create insert_iterator to element 2
insert_iterator<vector<int>>i_itr(v, itr);

// insert rather than overwrite
*i_itr++ = 100;
*i_itr++ = 200;
cout << "Array after insertion: ";
itr = v.begin();
while(itr != v.end())
cout << *itr++ << " ";
cout<<"\nPress a character to continue!";
int c= getchar();
cout<<"\n\n\n";
return 0;
}

int usage2 () 
{
cout<<"Example to demonstrate os_stream iterator\n";
int ary[] = {2,5,7,2,8,9};
ofstream ofile("TEST.DAT");
        
// write to STDOUT
    copy(ary,ary+6,ostream_iterator<int>(cout," - "));
// write into file "TEST.DAT"
    copy(ary,ary+6,ostream_iterator<int>(ofile,"\n"));
    ofile.close();
    cout << endl;
    ifstream ifile("TEST.DAT");
	 
    istream_iterator<int>start_file(ifile);
    istream_iterator<int>end_file;
    
// read from file and write to STDOUT
    copy(start_file,end_file,ostream_iterator(cout," * "));
    ifile.close();
    cout << endl;
    cout<<"\nPress a character to exit!";
    int c= getchar();
	
    return 0;
}

int main()
{
     usage1();
     usage2();
     return 0;
}

Give feedback of this article


ŠNokia 2008

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.