C++ STL vector, sort questions

A

Alan

I have some questions about the STL template "vector":

- Do I need to do anything (e.g., "delete") to free memory when I`m
done with a vector? I just declare it, and I know it allocates memory
as needed. Just not sure about it releasing memory.

- Does the STL algorithm "sort" work on vectors of complex objects?
(The simplistic examples I found only sort integers, not objects with
lots of attributes.) If so, on what does it sort? (each attribute in
order?)

I have searched the web but have been unable to find this information.
Thanks, Alan
 
J

James Daughtry

Do I need to do anything (e.g., "delete") to free memory when I`m done with a vector?
Nope, the destructor takes care of things for you. However, if you have
a vector of pointers that were allocated memory with new, you still
have to provide the corresponding delete.
Does the STL algorithm "sort" work on vectors of complex objects?
Try it and see.
 
P

Phil Endecott

Alan said:
I have some questions about the STL template "vector":

- Do I need to do anything (e.g., "delete") to free memory when I`m
done with a vector? I just declare it, and I know it allocates memory
as needed. Just not sure about it releasing memory.

No, it "just works".
- Does the STL algorithm "sort" work on vectors of complex objects?
(The simplistic examples I found only sort integers, not objects with
lots of attributes.) If so, on what does it sort? (each attribute in
order?)

It works as long as a comparison function is defined for the objects.
You'll probably want to define something that compares them in the way
that you want. You can then pass the function as the third parameter to
sort, or you can make it a specialization of less<>.

--Phil.
 
P

Peter Koch Larsen

Alan said:
I have some questions about the STL template "vector":

- Do I need to do anything (e.g., "delete") to free memory when I`m
done with a vector? I just declare it, and I know it allocates memory
as needed. Just not sure about it releasing memory.
It does release the memory - actually it destroys each object in the vector.
Just remember that if you store pointers to objects, it does not delete what
those pointers point to.
- Does the STL algorithm "sort" work on vectors of complex objects?
(The simplistic examples I found only sort integers, not objects with
lots of attributes.) If so, on what does it sort? (each attribute in
order?)

Sort works as long as operator< is defined on these objects and this
operator works in the intuitive way: a said:
I have searched the web but have been unable to find this information.

Should surely be possible.
Thanks, Alan
/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top