Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a STL vector

Status
Not open for further replies.

jayjay60

Programmer
Jun 19, 2001
97
FR
I need in my application to resize at each step of a for loop 2 arrays. I have read that for reallocate memory, it's better to use <vector>, rather than use the definition of the usual pointer. But, as i'm a beginner in this kind of knowledges, i would like to know if someone could explain me how i could declare 2 arrays, with 2 differents size, so that at each step, as i use an iterative method, one of this array &quot;becomes&quot; the other.

thanks in advance

jayjay
 
Hi there,

firstly it's pretty unclear to me quite what you mean with resize and so, however, vector<> has a special swap function which enables you to swap two vectors very efficiently (actually just pointers are being changed):

vector<int> vi1;
vector<int> vi2;

vi1.push_back(1);
vi1.push_back(2);
vi1.push_back(3);
vi2.push_back(101);
vi2.push_back(102);
cout << &quot;vi1 size = &quot; << vi1.size() << endl;
cout << &quot;vi2 size = &quot; << vi2.size() << endl;
cout << &quot;vi1[0] = &quot; << vi1[0] << endl;

vi1.swap(vi2);

cout << &quot;vi1 size = &quot; << vi1.size() << endl;
cout << &quot;vi2 size = &quot; << vi2.size() << endl;

cout << &quot;vi1[0] = &quot; << vi1[0] << endl;

Maybe that helps a bit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top