• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Pointer to vector

## Recommended Posts

Craazer    122
If i have vector like this: vector vMyVector; hove can i make it pointer, is it bossible? example how could i get vMyVector to *pvMyVector ? [edited by - Craazer on October 20, 2002 10:20:47 AM]

##### Share on other sites
Craazer    122
quote:
Original post by Kylotan
Same way you do with any other variable: the address-of operator '&'.

vector* pvMyVector = &vMyVector;

Is that really so? i allready tryed to do that but didnt work.
see if i normally have STRUCT vector and access its variables like this:

vMyVector.x

and now if i use pointer (vector* pvMyVector = &vMyVector; )

pvMyVector.x

doesnt work!
or
pvMyVector->x

doesnt work either

it doesnt work becose i start using vector class variables instead of my struct stored to vector. U know?

[edited by - Craazer on October 21, 2002 12:42:18 PM]

##### Share on other sites
Oluseyi    2103
quote:
Original post by Craazer
and now if i use pointer vector* pvMyVector = &vMyVector;, pvMyVector.x doesnt work!

It''s not supposed to. Member dereference for a vector uses operator ->.

quote:

pvMyVector->x doesnt work either. it doesnt work becose i start using vector class variables instead of my struct stored to vector. U know?

No, I don''t know. If you have a vector data structure which has a member x, then a pointer to an instance of vector named pvMyVector will return the member x with the syntax pvMyVector->x. Perhaps you are doing something funky?

##### Share on other sites
Oluseyi    2103
quote:
Original post by Craazer
If i have vector like this:
vector<int> vMyVector;

There''s the problem! std::vector is a dynamic array, not a 3d vector. It doesn''t have a member named x. What you want is your own 3d vector class, something like this:
template < typename T >struct vector3{  T x, y, z;};

You can then do the following:
vector3< int > vMyVector;vector3< int > * pvMyVector = &vMyVector;pvMyVector->x = 12;

##### Share on other sites
Craazer    122

    // Sorry my vector is vector<struct> vMyVector;// not vector<int> vMyVector;  like i sayd, sorry. // Heres some simble code what i just write struct TT{ int x; int y;};vector<TT> vec;void main(){  vector<TT> *pvec; pvec = &vec; pvec->_Ctptr; // accesses only the vector class }

So i hove can i have the pointer?
should i use template < typename T > in this one then?

[edited by - Craazer on October 21, 2002 12:56:44 PM]

##### Share on other sites
SabreMan    504
quote:
Original post by Craazer
vector<TT> *pvec;

Why are you using a pointer? Use a reference instead.
quote:

pvec->_Ctptr; // accesses only the vector class

You haven''t called operator[] to retrieve an element. If you want to access elements via a pointer, you should be doing this:

pvec->operator[](index).x = 10;

If you used a reference rather than pointer, you wouldn''t have to use such a warped syntax.

##### Share on other sites
Craazer    122
quote:
Original post by SabreMan
[quote]Original post by Craazer
vector *pvec;
Why are you using a pointer? Use a reference instead.

I need to use pointer becose i have multible vectors wich contains stuctures but they basicly hold the same data.
so i have to choose wich vector to use at wich point.

[edited by - Craazer on October 21, 2002 1:18:54 PM]

##### Share on other sites
SabreMan    504
quote:
Original post by Craazer
I need to use pointer becose i have multible vectors wich contains stuctures but they basicly hold the same data.

That doesn''t rule out use of a reference.
quote:

so i have to choose wich vector to use at wich point.

You mean it needs to be reseatable? In that case, a reference won''t do, so you should consider a suitable smart pointer in preference to the dumb pointer you''re currently using. Check out the boost::shared_ptr at Boost''s website.

##### Share on other sites
petewood    819
you need to access the vector using [] first.

then use the structure it gives you

##### Share on other sites
Craazer    122
quote:
Original post by SabreMan
[quote]Original post by Craazer
I need to use pointer becose i have multible vectors wich contains stuctures but they basicly hold the same data.

That doesn''t rule out use of a reference.
quote:

so i have to choose wich vector to use at wich point.

You mean it needs to be reseatable? In that case, a reference won''t do, so you should consider a suitable smart pointer in preference to the dumb pointer you''re currently using. Check out the boost::shared_ptr at Boost''s website.

Thanks but thats not what i want.

so lets just stay on the queston wich is:
how can i have pointer to that vector, or is it bossible?
struct TT{ int x; int y;};vector vec;

##### Share on other sites
Stoffel    250
I usually use references locally in situations like this because the code compiles-out (e.g. compiles to same code as if you didn''t) and it removes the need for dereferencing acrobatics:

  void someFunc (vector<TT> *pVec){  vector<TT> &vec = *pVec; // vec now acts like a vector object,                           // not a pointer to a vector  vec[0].x = something;}

##### Share on other sites
Craazer    122
quote:
Original post by Stoffel
I usually use references locally in situations like this because the code compiles-out (e.g. compiles to same code as if you didn''t) and it removes the need for dereferencing acrobatics:

    void someFunc (vector<TT> *pVec){  vector<TT> &vec = *pVec; // vec now acts like a vector object,                           // not a pointer to a vector  vec[0].x = something;}

if i would use reference i would have to use function?

##### Share on other sites
Stoffel    250
No, just showing that anywhere you have a pointer to a vector (*pVec), you can define a reference to the dereferenced pointer, and treat that reference as an object.

##### Share on other sites
Craazer    122
quote:
Original post by Stoffel
No, just showing that anywhere you have a pointer to a vector (*pVec), you can define a reference to the dereferenced pointer, and treat that reference as an object.

Oh ok.

i tryed to also use reference:

  struct TT{ int x; int y;};vector<TT> vec;void main(){  vector<TT> &pvec; pvec = &vec;}

but like u might ques that doesnt work. (sure it links but cant access x and y)

so inst there anything i could try, or should i give up the vectors and start using linked list or shometing?

im really confused whit this one and worried becose its very importand part of my game. that vector i mean.

##### Share on other sites
Guest Anonymous Poster
//Try somthing like thisvector<TT> myVector;//Fill Vector with structsvector<TT> * pMyVector = &myVector;  //Pointer to your Vector(*myVector)[1].x = 2;  //Defrefrence pointer so you have a vector                       //Use subscript operator to get item 1                       //Use dot operator to get member x                       //Use member x however you wish[Code]

##### Share on other sites
Oluseyi    2103
quote:
Original post by Craazer
vector<TT> vec;void main(){ vector<TT> &pvec; pvec = &vec;}

Absolutely wrong! First, references can not "refer" to null, which means they must be initialized. Second, you assign your reference the address of the vector (ie, a pointer) instead of the vector itself.

As an aside, Standard C++ requires main to return an int, but various compilers allow other return values so as not to break existing code.
vector<TT> vec, vec1;int main(){  vec.push_back( new TT );  vec1.push_back( new TT );  vector<TT> &vec_r = vec;  vec_r[0].x;  vec_r = vec1;  vec_r[0].y;}

I suggest you read up on references and std::vector.

##### Share on other sites
SabreMan    504
quote:
Original post by Craazer
so lets just stay on the queston wich is:
how can i have pointer to that vector, or is it bossible?

Yes, petewood and I have both told you what your immediate problem is. Try reading the responses again.

##### Share on other sites
Craazer    122
Great got it work now!

some of you sayd that use [] but actualy i did i just didnt type them to my samble code, sorry about confusion whit that :/

and about that pointer, thanks for anymost poster showing how its done. i tryed my self almost same code earlier but i didnt realize to use ()

and Oluseyi in my app they werent NULL and i didnt post excatly the same code here, and some of thoes trys was like "here goes nothing"

          struct TT{ int x; int y;};	   // pointer style	vector<TT> myVector;	vector<TT> * pmyVector = &myVector;    (*pmyVector)[0].x;	  // reference style	vector<TT> vec;	vector<TT> &vec_r = vec; 		vec_r[0].x;

Well this dump head has learn shometing new again so i gues the final queston is wich one im goin to use reference or pointer. I think im goin to use reference but if somebody has shometing against it say it now

Thanks for all of you for your posts!

[edited by - Craazer on October 22, 2002 6:17:41 AM]

##### Share on other sites
SabreMan    504
quote:
Original post by Craazer
some of you sayd that use [] but actualy i did i just didnt type them to my samble code, sorry about confusion whit that

I must remember to get that crystal ball fixed.

##### Share on other sites
Craazer    122
quote:
Original post by SabreMan
[quote]Original post by Craazer
some of you sayd that use [] but actualy i did i just didnt type them to my samble code, sorry about confusion whit that

I must remember to get that crystal ball fixed.

Hehheh ya, sorry