C++ STL
vector<int> vec_nVal;
// Store elements into vector ...
// Accessing vector elements :
int ninitialize;
for ( ninitialize = 0 ; ninitialize < vec_nVal.size() ; ninitialize++ )
{
int nVal = vec_nVal[ninitialize];
cout<< nVal ;
}
Two Time saving Tips;
1.
Try avoiding function call at the for loop condition check
for ( ninitialize = 0 ; ninitialize < vec_nVal.size() ; i++ )
Function calls are costlier . So in the above code , for every iteration,vector::size() function will be called , which is not recommended
unless or otherwise if you know that vector size is going to change inside the loop .
For example : If the vector contains 100K elements then vector::size will be called 100K times.
so the best practice will be like , getting the size of the vector before the loop begin and substitute the variable accordingly . something like this ...
"int nVecSize = vec_nVal.size();
for ( ninitialize = 0 ; ninitialize < nVecSize ; i++ ) "
2 .
Use Iterators
One of the main concept of stl's are iterator. use it when ever required .
The above sample program can be written like this for the best performance and time saving .
vector<int>::iterator itrNumber;
itrNumber = vec_nVal.begin();
while( itrNumber != vec_nVal.end())
{
int nVal =*(itrNumber );
cout<<nVal;
itrNumber++
}
by using iterator the element retrieval time will be faster when compare to the previous one[ vector[] ] .
The same holds good for all stl containers . (eg:stl::map)
I am so sure that the above one will improve your performance and save your time leap and bound if you are using it excessively.