Sign in to follow this  
nicknamenick123

very strange about vector

Recommended Posts

Hello, I write this under Visual Studio 2005: vector<vector<INT> > vecT(2); for(vector<vector<INT> >::iterator iter = vecT.begin(); iter < vecT.end(); iter++) { (*iter).resize(3, 10); } for(vector<vector<INT> >::iterator iter = vecT.begin(); iter < vecT.end(); iter++) { printf("fr = %X \n", iter); for(vector<INT>::iterator itera = (*iter).begin(); itera < (*iter).end(); itera++) { printf("sec = %X \n", itera); } } but the output is: fr = 12F598 sec = DB3D20 sec = DB3D20 sec = DB3D20 fr = 12F598 sec = DB3D34 sec = DB3D34 sec = DB3D34 That is very strange, can anyone tell me why? Thank you very much.

Share this post


Link to post
Share on other sites
Enigma    1410
Why is it strange? An iterator doesn't have to be implemented as a pointer, so printing it as a pointer is a fairly silly thing to do (unless you know for a fact your SC++L implementation uses pointers for vector iterators, which yours appears not to).

EDIT: Sorry, too long since I've used printf. You're printing it as a hexadecimal value, not a pointer. Did you mean to dereference the iterator?

Σnigma

Share this post


Link to post
Share on other sites
keen    122
What is strange about this output? Please explain what your expected results are, otherwise it's hard to guess what you're really attempting to do

//keen

Share this post


Link to post
Share on other sites
Nitage    1107
Quote:
If iterator is not pointer, why can we use iter++ or itera++?

Because those operators, along with most others, can be overloaded.


Quote:

so if I use vector<vector<INT>* > instead of vector<vector<INT> >, and if I want to delete vector<INT>* things, what should I do?


Either use the [] operator, or derference the iterator.

Share this post


Link to post
Share on other sites
Sharlin    864
Because you can overload operators in C++. My guess is that VS05's iterators contain a pointer to the container they belong to, so as to facilitate checking for some error conditions such as trying to iterate over the bounds of the container, and that address then gets printed. If you need the address of the element an iterator points to, use &*iter.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by nicknamenick123
If iterator is not pointer, why can we use iter++ or itera++?
As far as the 'containers and algorithms' portion of the C++ standard library is concerned, an iterator is just a concept. Depending on the type of iterator (forward, reverse, etc.), the concept has certain requirements, such as support for incrementing.

A 'raw' pointer is a model of the iterator concept, but an iterator is not necessarily a pointer. It may, for example, be a class for which the appropriate operators (such as pre- and post-increment) have been overloaded.

So in short, the vector iterators you're working with are not (necessarily) pointers, and passing them as arguments to printf() will almost certainly not end well. You got garbage output - with GCC I get a compile-time warning and a run-time crash.

In any case, since you're using C++ you should be using C++-style streaming for output to the console (e.g. std::cout rather than printf()). This doesn't address all the issues in your example however, since streaming of iterators isn't supported anyway (what should we expect to be displayed?).
Quote:
so if I use vector<vector<INT>* > instead of vector<vector<INT> >, and if I want to delete vector<INT>* things, what should I do?
Again, iterators are not (necessarily) pointers, and you don't delete them as you would a raw pointer. As you become more familiar with iterators and their role in the standard library, I think this will all start to make more sense.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this