How to reference i th element in std::vector ?

I

imutate

How to directly reference i th element in std::vector (i being an
integer) ?
Example:

std::vector<double> x;
x.push_back(3);
x.push_back(-2);
x.push_back(-2);
x.push_back(-7);
int i = 3;
std::cout << x << std::endl;
 
D

Daniel T.

How to directly reference i th element in std::vector (i being an
integer) ?
Example:

std::vector<double> x;
x.push_back(3);
x.push_back(-2);
x.push_back(-2);
x.push_back(-7);
int i = 3;
std::cout << x << std::endl;


What happens when you run the above? What did you expect?
 
I

imutate

Example:
std::vector<double> x;
std::cout << x << std::endl;


What happens when you run the above? What did you expect?


It is a simplified example, my code is more complex I have a class
instead of a double and I use
x -> somemethod();

but it won't compile, although

x.end() -> somemethod();

will compile.
 
I

imutate

Example:
std::vector<double> x;
std::cout << x << std::endl;


What happens when you run the above? What did you expect?


It is a simplified example, my code is more complex I have a class
instead of a double and I use
x -> somemethod();

but it won't compile, although

x.end() -> somemethod();

will compile. I am hoping it is a syntax problem.
 
J

Jerry Coffin

[ ... ]
It is a simplified example, my code is more complex I have a class
instead of a double and I use
x -> somemethod();

but it won't compile, although

x.end() -> somemethod();

will compile. I am hoping it is a syntax problem.


Try 'x.somemethod();' instead.
 
M

Mike Smith

Example:
std::vector<double> x;
std::cout << x << std::endl;

What happens when you run the above? What did you expect?


It is a simplified example, my code is more complex I have a class
instead of a double and I use
x -> somemethod();

but it won't compile, although

x.end() -> somemethod();

will compile. I am hoping it is a syntax problem.


If you have x declared as a vector<Class>, then x is a Class, while
x.end() is a vector<Class>::iterator, which will cast to (Class *).
 
P

Pete Becker

Mike said:
If you have x declared as a vector<Class>, then x is a Class, while
x.end() is a vector<Class>::iterator, which will cast to (Class *).


Not quite: there is no required conversion from an iterator to a
pointer. To get the address of the object that the iterator points to
you'd use &*iter (assuming that the object's type does not overload the
unary & operator). The reason you can use iter->whatever is that
iterators provide an operator->.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top