Page 1 of 1

Vectors problem

Posted: Wed Jan 22, 2003 10:00 am
by Dominik Michniewski
Could anyone tell me which difference is between this to lines of code ?

We assume that v is type: vector<int> v;

Code: Select all

v[i]
and

Code: Select all

*(v.begin()+i)
In my program first line cuases RTE. Why ? In my opinion this two lines have the same meaning ....

Regards
Dominik Michniewski

Posted: Sat Jul 26, 2003 3:45 am
by Krzysztof Duleba
It works fine on my PC. Could you show the code that doesn't want to work?

Posted: Sat Jul 26, 2003 8:45 am
by Dominik Michniewski
Problem is, that n computer (Windows 2K, VC++) always is OK. I try now to install Linux, and I try it again. But problem appears in online jugde. ...

Best regards
DM

Posted: Sat Jul 26, 2003 2:26 pm
by Krzysztof Duleba
[cpp]#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v(50,50);
cout<<v[5]<<endl<<*(v.begin()+5)<<endl;
}[/cpp]

This code works fine under Win2k+Cygwin/g++ 2.95/3.2 and Linux with g++ 2.95.

As for the difference you asked, there is no. Look at the definition of operator[] in stl's header file:

[cpp]reference operator[](size_type __n) { return *(begin() + __n); }[/cpp]

It's just the same.

Posted: Mon Jul 28, 2003 7:48 am
by Dominik Michniewski
Hmm, you have right. I must have other problem with my code , but when I send my code with such changes (operator[] was changed to 'begin()+i') I've got WA instead of RTE, so I think that was a problem :)

Best regards fo help
DM