473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
6 1990
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
11
by: Jason Heyes | last post by:
Look at my code: void modify_element(BigType &x) { /* not shown */ } BigType modify_element_copy(BigType x) { modify_element(x);
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.