Connecting Tech Pros Worldwide Help | Site Map

Access contents of pointer to std::vector

Si
Guest
 
Posts: n/a
#1: Nov 7 '05
How do I access the contents of an std:vector pointer (I mean access
contents of the vector)?

What is the correct notation for a pointer ( [index] doesn't seem to work)?


Ajay Kalra
Guest
 
Posts: n/a
#2: Nov 7 '05

re: Access contents of pointer to std::vector


Its just like another C++ object. There is nothing special about it. []
works fine as well. What problems are you specifically having with it?

---------
Ajay Kalra
ajaykalra@yahoo.com

John Harrison
Guest
 
Posts: n/a
#3: Nov 7 '05

re: Access contents of pointer to std::vector


Si wrote:[color=blue]
> How do I access the contents of an std:vector pointer (I mean access
> contents of the vector)?
>
> What is the correct notation for a pointer ( [index] doesn't seem to work)?
>
>[/color]

Perhaps you need this

(*pointer)[index]

but it's hard to be sure because your description is vague. Post the code!

In any case ther is no special syntax for vectors or pointers to
vectors, the usual stuff works.

john
Eric Pruneau
Guest
 
Posts: n/a
#4: Nov 7 '05

re: Access contents of pointer to std::vector


Hi

If I understand, you have something like

vector<int> *MyVec;

assuming the pointer is correctly allocated and there is data in...

to access an element you can use at() function. at() simply return the
element at the specified position but
with a range check. If you dont mind the overhead of a range check use it.

int elem = MyVec->at(0); // the first element
MyVec->at(0) = 5;

if you really want to use [] you can do

int elem = (*Myvec)[0];
(*Myvec)[0] = 5;

Eric


"Si" <si@hotmail.com> a écrit dans le message de news:
tqObf.7477$wh7.4152@newsfe2-gui.ntli.net...[color=blue]
> How do I access the contents of an std:vector pointer (I mean access
> contents of the vector)?
>
> What is the correct notation for a pointer ( [index] doesn't seem to
> work)?
>[/color]


Closed Thread