I was just reading the article on iterators at Cprogramming and I was wondering why they recommend the STL approach instead of the old C approach. Why is one better than the other? I am more familiar with the old method.
The two styles are reproduced here exactly as seen in the article:
The old approach
STL approachCode:using namespace std; vector<int> myIntVector; // Add some elements to myIntVector myIntVector.push_back(1); myIntVector.push_back(4); myIntVector.push_back(8); for(int y=0; y<myIntVector.size(); y++) { cout<<myIntVector[y]<<" "; //Should output 1 4 8 }
Code:using namespace std; vector<int> myIntVector; vector<int>::iterator myIntVectorIterator; // Add some elements to myIntVector myIntVector.push_back(1); myIntVector.push_back(4); myIntVector.push_back(8); for(myIntVectorIterator = myIntVector.begin(); myIntVectorIterator != myIntVector.end(); myIntVectorIterator++) { cout<<*myIntVectorIterator<<" "; //Should output 1 4 8 }