In STL , there are many method can traverse the vector object , but the most efficency one ,I think , it's 'for_each' function. And it can be used very near the real life language , what you need do is only to define a function express how you want to act while it's traversing.
E.G
/********************************************************************
created: 2007/04/19
created: 19:4:2007 2:04
filename: C:\testvc6\TestStll\TestStll.cpp
file path: C:\testvc6\TestStll
file base: TestStll
file ext: cpp
author: Chang xinglong(King.C)
purpose: How to traverse a Vector ?
*********************************************************************/
You can define a travers Function which simply couput the node data , it was defined like this :
void Traverse(int t)
{
cout<<t<<endl;
};
Then Create a integer vector , and use the for each to travers it ,here the code goes like this:
vector <int> colt;
for (int i=0; i<=10;i++)
{
colt.push_back(i);
}
for_each(colt.begin(),colt.end(),Traverse);
cout<<endl;
Very easy and effiency , try to use it in the program instead of the following code:
for (int i=0;i<max;i++)
{
int v =(int) aVector[i];
traverse(v);
}