Wednesday, February 19, 2014

pointer to a vector

Newsgroup: comp.lang.c++

Subject: pointer to a vector

From: "A" <a@...>

Date: Wed, 12 Feb 2014 20:46:12 +0100



There are 2 vectors each one having a structure



struct MyStruct

{

int a;

std::string b;

}



std::vector<MyStruct> v1;

std::vector<MyStruct> v2;



Now I want a pointer to which one I will use... v1 or v2



std::vector<MyStruct> *v = (condition)? &v1 : &v2;



Finally I access it using:



for (unsigned i = 0; i < v->size(); i++)

{

v->operator[](i).a = i + 1;

}



My lack of understanding here is:



a) does the above *v needs to be deleted? Isn't it just a pointer variable?

Or it works differently when it points to a vector? Just to be clear, I

don't actually need to delete v1 or v2. I just need to cleanup *v if

required.



b) what's the heap or stack or difference or advantage of them in relation

to the above?











via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/MC6KBR

View all the progranning help forums at:

http://ift.tt/1dP9txN

No comments:

Post a Comment