Im a fairly fresh programmer, and I often run into problems like most of us. Most of the time I get it solved in 10 minutes by some google power, but this time Im stuck for real (spent the last 4 hours looking around the web).

I first ran into the problem of not beeing able to retrieve the size of an array through a pointer (since sizeof(*k); doesn't work). I tried solving this by using a Vector instead, but ended up running into this horrible problem.

From what I understood from forums etc, the arrow operator (->) should be useable with a iterator of a vector to reach functions defined in the vector object.

So, my questions are: Why doesn't this work? And do someone have a better suggestion to get the size of an array/vector by using a pointer than this? (This is just an illustration, in my real program the pointer is sent to a function where I want to retrieve the length of the array without sending it from my main function).

void main()
{
vector<int> x;
x[0]=5;
vector<int>::iterator y = x.begin();
int z = y->size();
}

This won't do what you want because an iterator points at one of the elements of the vector, not the vector itself. If you want to know the size of a std::vector then just do x.size() .

If you're passing an iterator to a function, then you generally have to pass two iterators; one for the start and one for the end, rather than the start and a size.

Thank you ravenous. Your explanation made some sence, more than I can say about most of the internet. You did not however answer my question of why it doesn't compile. Would be nice to know for future use of vectors!

You did not however answer my question of why it doesn't compile.

I cannot find that question in your first post.

I'd say: size() is not a member of vector<int>::iterator. So it cannot compile.

But I'm sure your compiler gave you an error message...

He told you why it doesn't compile. The vector contains ints. ints has no member functions. So when you call vector.begin() it returns an iterator that points to a list of ints. And when you use the arrow operator on the iterator, it deferences the iterator, thus 'returning' ints. And so when you call the size() function on the ints, its a compile error because ints has no member function call size() or any other functions for that matter.

It doesn't compile because it's not right! When you do y->size() you're trying to call a method named size() on an int . int doesn't have this method, so the compiler won't let you do it. Remember that the iterator is basically a reference to the underlying element, so you can only use methods on iterators that are defined for the elements of the container. For example, if you have a vector of vectors, then you could do what you have done, but you would get the size of the vector that the iterator points to, not the size of the vector of vectors. Maybe not immediately clear, but have a think about it :o)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.