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

Examples for Standard C++ Libraries

Table of Contents

STL
MMP File for STL
Source Code for STL

 


STL

Add elements in a vector with the help of the push_back() function. There are three different methods to access the elements in the vector and each method prints data stored in vector. The methods are as follows:

  • Loop by index
  • Using constant iterators: A constant iterator enables the user to access the elements of the vector. It cannot modify the elements of the vector.
  • Using reverse iterators: A reverse_iterator is an iterator adaptor that enables backward traversal of a range.

In addition, to adding and accessing the elements in a vector the example:

  • Prints the size of the vector using the size() function.
  • Swaps two elements of the vector using the swap() function and prints the swapped values.

 


MMP File for STL

TARGET		  	OpencppSTLex.exe
TARGETTYPE	  	exe
UID			 0 0xEEE67B46

USERINCLUDE	 	..\inc

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

SOURCEPATH	  	..\src
SOURCE		  	OpencppSTLex.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 STL

The code snippet shows three methods of accessing the data within the vector.

#include<iostream>
#include<vector>
#include<string>

// 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 main()
{
vector<string> hello;

   hello.push_back("The number is 10");
   hello.push_back("The number is 20");
   hello.push_back("The number is 30");

   cout << "Loop by index:" << endl;

   int i;
   for(i=0; i < hello.size(); i++)
   {
      cout << hello[i] << endl;
   }

   cout << endl << "Constant Iterator:" << endl;

vector<string>>::const_iterator cite;
   for(cite=hello.begin(); cite!=hello.end(); cite++)
   {
      cout << *cite << endl;
   }

   cout << endl << "Reverse Iterator:" << endl;

vector<string>reverse_iterator rite;
   for(rite=hello.rbegin(); rite!=hello.rend(); ++rite)
   {
      cout << *rite << endl;
   }

   cout << endl << "Sample Output:" << endl;

   cout << hello.size() << endl;
   cout << hello[2] << endl;

   swap(hello[0], hello[2]);
   cout << hello[2] << endl;
   getchar();
   return 0;
}

Give feedback of this article


Back to top


Copyright ©2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user.