Friday

STL - Vector

The sample code below shows how to use vector
(remember to add #include &lt vector &gt )

vector SS;
stringstream str1;
string str2;

cout << "setting String " << endl;

for (int j=0; j< 10; j++)
{
str1 << j;
cout << " str1 = " << str1.str() << endl;
str2 = "Hello ";
str2.append(str1.str());
SS.push_back(str2);

str2.clear();
str1.str("");
cout << " >> " << str2 << endl;
}

// to print out the string in order
vector::const_iterator cii;
for(cii=SS.begin(); cii!=SS.end(); cii++)
{
cout << *cii << endl;
}

cout << endl;

// to print out the string in reverse order
vector::reverse_iterator rii;
for (rii=SS.rbegin(); rii != SS.rend(); ++rii)
{
cout << *rii << endl;
}

No comments:

Post a Comment