473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C++ STL vector, sort questions

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

Jul 23 '05 #1
3 12971
> 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.

Jul 23 '05 #2
Alan wrote:
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.
Jul 23 '05 #3

"Alan" <al********@3n.net> skrev i en meddelelse
news:11**********************@g44g2000cwa.googlegr oups.com...
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 < b & b < c => a < c. !(a < b) & !(b
< a) <=> a = b.
I have searched the web but have been unable to find this information.
Should surely be possible.
Thanks, Alan

/Peter
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
16
by: Kitty | last post by:
Hi, everyone. Given a vector<int>, what is the fastest way to find out whether there is a repeated element in it? The result is just "true" or "false". Thanks. Kitty
4
by: Johan | last post by:
Hi, Why does my vector not sort. What I understand is you have to overload the < operator, but that does not work. see code below Thanks Johan
10
by: eiji | last post by:
Hi folks, I have a problem compiling this under VC6! Maybe someone has some minutes to look at this: template<class T> class Compare { public: Compare(){}; virtual ~Compare(){};
8
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
2
by: robertoviperbr | last post by:
Hello to everybody. I have a doubt see the code: #include <vector> #include <iostream> #include <string> #include <algorithm> #include <sstream> #include <fstream> #include <iomanip>
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
3
by: Eric Lilja | last post by:
Hello, consider the following assignment and my code for it: /* Write a program that does the following: * Integer numbers shall be read from a textfile and stored in a std::vector. The name...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.