Random access iterators

In this code:

1
2
3
vector<int> mydata(100);

mydata[2] = 999;


In statement 2 does that call the iterator to access the 3rd position and set its value to 999?
i don't know whether its done through the iterator but yes this statement does set the vector value at 3rd position to 999. how does it matter whether its through iterator or not?
Last edited on
Just wanna know how it works lol.

@Anmol444

Why don't you read the reference & and look at it's example?
There is no use of iterators here at all.

*(mydata.begin()+2) = 999 ; // random access iterator used
So begin returns a pointer to its first element?


@TheIdeasMan

Is the reference for VC++ msdn? But I just looked at my actual <vector> header file and its like cire described:

1
2
3
4
reference operator[](difference_type _Off) const
		{	// subscript
		return (*(*this + _Off));
		}


But you refer to this as the random access iterator right?
Doesn't the begin function return an iterator, and the vector container happens to have an algorithm for pointer arithmetic? I've tried adding and subtracting with other container iterators but they don't work.

Also:
http://en.cppreference.com/w/cpp/container/vector/begin
http://www.cplusplus.com/reference/vector/vector/begin/

These references both state that std::vector<T>::begin() returns an iterator object.

Edit:
I mean "...an algorithm for pointer arithmetic with iterators?"
Last edited on
I'm using VC++, so for me its actually a reference. That is the same code from the implementation of vector in VC++.


Also there is more then one [] operator, I checked through them all and they all return a reference.

Also I am talking about the subscript operator, not the begin function. lol
Last edited on
Also begin does result in a iterator, here is the code:

1
2
3
4
	iterator begin()
		{	// return iterator for beginning of mutable sequence
		return (iterator((_Vbase *)this->_Myvec._Myfirst, this));
		}
Sorry to confuse you, Anmol; my question was for cire.
Oooo, yea true. I got what your question meant. Atleast now lol
Last edited on
Doesn't the begin function return an iterator, and the vector container happens to have an algorithm for pointer arithmetic? I've tried adding and subtracting with other container iterators but they don't work.


It works for random access iterators, which a vector has and many other containers do not. A pointer satisfies the criteria for a random access iterator.
Oh... okay. I didn't really factor in the different types of iterators.
Thanks guys!
Anmol444 wrote:
Is the reference for VC++ msdn?


No, I meant the reference on this site - top left of this page. I am hoping that you already know that it is there after 551 posts 8+)
operator []( n ) is described in terms of semantics as returning *(a.begin() + n). However it is not necessary that it indeed uses expression *(a.begin() + n). The only requirements that operator []( n ) will be semantically equivalent to expression *(a.begin() + n).

@TheIdeasMan

Well ofcourse I know about that reference. I use it sometimes except it doesnt give me much implementation details.


@vlad from moscow

Thanks!


But we refer to it as the random access iterator right?
No, we don't. It's not an iterator.
Ok, thanks.
Topic archived. No new replies allowed.