#### Archived

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

# inner workings of STL vectors

This topic is 5930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how exactly does an STL vector store the objects you push_back into it? i have done this in two ways:

// CPolygon is my own homemade class

// p_vector is a member variable of the class all this is in

void TestVector(void)
{
CPolygon tPOLY[10];
for (int i = 0; i < 10; ++i)
{
tPOLY[i].initCrap();
p_vector.push_back(tPOLY[i]);
};
};

void TestVector2(void)
{
CPolygon * tPOLY = new CPolygon[10];
for (int i = 0; i < 10; ++i)
{
tPOLY[i].initCrap();
p_vector.push_back(tPOLY[i]);
};
};

when the first one worked, i assumed that STL vector must copy the stuff you put in it, since the array of CPolygons i created would go out of scope and be destroyed right after they are put in the vector. i also discovered that when using the array allocated in the heap, i could delete[] them before the function returns, and the vector still contains usable data. so, this post is just to affirm my thoughts, because i don't want things to fall apart later because of faulty assumptions. does the STL vector in fact copy the objects you push_back() into it? or am i just "getting lucky" with this example? thanks! EDIT: duh... i forgot the other part of my question: does the "clear" function in the vectors clean up the objects inside it? meaning, does it call their destructors? --- krez (krezisback@aol.com) [edited by - krez on September 15, 2002 5:26:04 PM]

##### Share on other sites
Yes and yes. Remember, though, that if you have a vector of pointers it won''t delete the pointers.

If I had my way, I''d have all of you shot!

codeka.com - Just click it.

thank you!

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013162
×