very strange about vector

Started by
7 comments, last by nicknamenick123 17 years, 4 months ago
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.
Advertisement
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
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
If iterator is not pointer, why can we use iter++ or itera++?

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?
Is there a way to get the pointer instead of iterator?

Why we use iterator instead of pointer? I didn't see a clue.
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.
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.
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.
Thanks for all, I think I need some time to digest. I tried &*iter, it seems work.

And sorry, I am not using printf(""), in fact, I use TRACE("")

This topic is closed to new replies.

Advertisement