Question:
[C++]Vector: What's the difference between a counter variable and an iterator?
Damien Bell
2011-06-15 15:36:02 UTC
Assume that I have the two codes, how do they differ at all? (if I make a syntax error, don't worry too much about it, I'm not coding in my ide)

int x=5;
vector v(x);
for (int i=1; i<=5; i++) v.push_back(i);

vector::iterator iter;

cout << "vector v contains:";

for ( iter=v.begin() ; iter < v.end(); iter++ )
cout << " " << *iter;


How does that differ at all from something like this?

int x=5;
vector v(x);
for (int i=1; i<=5; i++)v.push_back(i);

for (int i=0; icout << " " << v[i];
}


Does it even matter?
Four answers:
?
2011-06-15 20:10:57 UTC
Iterator-based iteration will work, unchanged, for any kind of a sequence -- vector, string, C++ array, list, set, input stream, boost tokenizer, recursive directory iterator, asio tcp resolver, etc -- anything that exposes a pair of iterators.



Index-based iteration will work for indexed containers only: vectors, strings, deques, arrays. The second code is less generic.



In case of vector, it does not matter; both loops are as fast as it gets (although some compilers may generate slightly better code for one or the other), both can access the vector out of bounds (if the iterator is incremented past v.end() or if the index is incremented past v.size())
?
2011-06-15 15:48:48 UTC
An iterator is a different type, and has useful methods associated with. Its part of the STL and with that is much better programming practice. A counter can drop off the end or go past the beginning without error, a iterator generates a error which you can catch and deal with. Using a iterator is much more powerful, secure, the only advantage of programming counters all the time is its lightweight, maybe faster, but realistically its less effort to use the iterator and better.



What your asking abbreviated is "Whats the difference between C and C++", the lower code is C code (apart from the vector) the above is C++. The vector is an array fundamentally with bells and whistles, your treating it as such , in a C way not using the C++ iterator way, which your perfectly at will to do.



No.
peteams
2011-06-15 17:14:00 UTC
The iterator better describes what you're doing and misuse of the iterator is more likely to be spotted by the compiler. If you're iterating through collections of two different types of object, if you were using counters i and j and got them mixed up the program would get the wrong answer when ran, possibly even crash. If i and j were iterators and you mixed them up, you would most likely get a compiler error.



You don't necessarily know how a collection is stored. We can assume vector is stored as a contiguous array of memory, but other types may be stored as linked lists, trees or a host of other methods. Using a counter to get item n may involve starting from the beginning and counting your way along until you arrive at item n. Using an iterator ensures that you have state from where you last were, so stepping forward is typically as efficient as it can be.



So iterators are safer to use and can be faster than a counter. It matters.
kelm
2016-11-19 07:17:00 UTC
oftentimes, for meats like filet that are in the back of the counter are best shrink while the pre-packed stuff is decision. a minimum of it somewhat is the way my markets perform those are U.S. phrases that relate to the US branch of Agriculture score of beef. It basically has to do with the marbling of the beef. severe end eating places (steak homes) tend to serve basically best meats (which even tend to have slightly greater fat)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...