Jump to content
  • Advertisement

Archived

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

Sphet

STL vector insertions

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I use the STL template with classes, do I need to declare memory for each item I am inserting, or is the template allocating its own memory an copying the class in?

Share this post


Link to post
Share on other sites
Advertisement
It will allocate memory for the object itself, and then usually call a copy constructor on the object inserted. For this reason it''s usually better (from a speed point of view anyway) to use vectors of object pointers rather than vectors of objects.

Share this post


Link to post
Share on other sites
well, it depends on what the vector contains...

if you have a class (CSomething), and make a vector like so:

vector<CSomething> vec;

then you dont have to allocate or deallocation

however, if you are doing:

vector<CSomething*> vec;

then you DO have to allocate each object, or at least send pointers that will be valid through the life of the vector.

Edited by - TANSTAAFL on November 27, 2000 4:42:41 PM

Share this post


Link to post
Share on other sites
Ok, both of these makes sense to me .. a further question:

I have a vector_A which is a collection of objects in the order they were inserted.

I want to compare vector_A vs vector_B, which is a sorted vector of objects (sorted by filename, for example).

I want to go through vector_A and vector_B until I find that there is an object in vector_B that is NOT in vector_A, or vice-versa.

My thought would be to have a second vector, vector_A_sorted and keep it sorted by filename so I can just iterate through that vs. vector_B and when I find something that doesn't match, stop searching. I don't want to sort vector_A because I need to keep track of the order they were inserted in, and having to resort based on a class member "insert time" or something seems wasteful.

Does this make sense?

Edited by - Sphet on November 27, 2000 4:53:54 PM

Share this post


Link to post
Share on other sites
FYI

If you store object pointers in your vector, when you use the methods vector.clear(), or vector.erase(iterator first, iterator last), remember that this only removes the entries from the vector and not the object itself. The object still exists in memory until you delete the object.




Share this post


Link to post
Share on other sites
Sphat:
It makes some sense, though I think you might be talking about three contianers here:
- a vector (list?) of objects that retain order
- a map of filenames to objects (which would always be sorted)
- a temporary sorted vector to compare to the map

I''m a little concerned that objects may exist in one container and not the other, by your problem description. Vectors are at their best when you only insert and remove at the end of the vector. If you''re ever inserting or removing from anything that''s not the end (beginning or middle), vector will have to do some serious work. You might be wanting a list instead of a vector.

Anyway, here''s what I''d suggest:
every time you need to add an object, push it to the back of your sequential container (vector for now), and insert it into the sorted map using the filename as the key. When you need to compare the two, create a temporary sorted vector of object pointers. You should then be able to take two iterators through both the temporary and sorted containers and find the first mismatch.

Share this post


Link to post
Share on other sites
I think I get what you are talking about.

The real problem here is that I am trying to make a sensible attempt at a "watched folder" for a project I am doing.

I have a path that is looked at every 5 seconds. It then compares what it finds with what it thought was there 5 seconds ago. If anything is different from what it was before, it generates an event causing the GUI to update the display with the new list. I need to keep track of which files appeared in the folder chronologically first, but when I compare the old list with the new list, it makes sense to compare them sorted.

I''m only ever going to be inserting from the bottom and removing from the top since the file list of ''known files'' is essentially a FIFO. And when I''m building the list of files in a directory, its going to be build pushing back one filename after another.

I want to store additional information in the ''known files'' so it isn''t just the filename, but the sorted list that is being compared really is just a filename.

Any other ideas?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!