By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,983 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,983 IT Pros & Developers. It's quick & easy.

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

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

std::vector<doublex;
x.push_back(3);
x.push_back(-2);
x.push_back(-2);
x.push_back(-7);
int i = 3;
std::cout << x[i] << std::endl;

Sep 23 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
im*****@hotmail.co.uk wrote:
How to directly reference i th element in std::vector (i being an
integer) ?
Example:

std::vector<doublex;
x.push_back(3);
x.push_back(-2);
x.push_back(-2);
x.push_back(-7);
int i = 3;
std::cout << x[i] << std::endl;
What happens when you run the above? What did you expect?

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Sep 23 '06 #2

P: n/a
Example:
std::vector<doublex;
std::cout << x[i] << 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[i] -somemethod();

but it won't compile, although

x.end() -somemethod();

will compile.

Sep 23 '06 #3

P: n/a
Example:
std::vector<doublex;
std::cout << x[i] << 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[i] -somemethod();

but it won't compile, although

x.end() -somemethod();

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

Sep 23 '06 #4

P: n/a
In article <11**********************@m73g2000cwd.googlegroups .com>,
im*****@hotmail.co.uk says...

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

but it won't compile, although

x.end() -somemethod();

will compile. I am hoping it is a syntax problem.
Try 'x[i].somemethod();' instead.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 23 '06 #5

P: n/a
im*****@hotmail.co.uk wrote:
>>Example:
std::vector<doublex;
std::cout << x[i] << 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[i] -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[i] is a Class, while
x.end() is a vector<Class>::iterator, which will cast to (Class *).

--
Mike Smith
Sep 23 '06 #6

P: n/a
Mike Smith wrote:
>
If you have x declared as a vector<Class>, then x[i] 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.
Sep 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.