Examples for Standard C++ Libraries |
|
|
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:
In addition, to adding and accessing the elements in a vector the example:
|
|
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
|
|
Following code snippet illustrates 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;
}
| ©Nokia 2008 |
|