473,241 Members | 1,545 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,241 software developers and data experts.

Sorting a vector of structs

Hi there,

I have struct like this one:

Expand|Select|Wrap|Line Numbers
  1. struct kurs
  2. {
  3.         unsigned short int var1;
  4.         string string_var;
  5.         int sort_var;
  6. };
// and a vector like this one
Expand|Select|Wrap|Line Numbers
  1. vector < struct kurs> kurs_2;

thing is that I want to sort a vector by sort_var.
How can I do that?

Dec 9 '07 #1
2 3412
849 Expert 512MB
You can overload operator< to return based on that value, or write a function that takes two kurs inputs and returns one < other based on that variable. Then, you can call sort() from #algorithm and bingo, it's sorted. Note that using the second option requires using the three-parameter sort with the function name as the third argument.
Dec 9 '07 #2
Thank you for reply.

I made it this way:
Expand|Select|Wrap|Line Numbers
  1. class sorting
  2. {
  4. public:
  6.     bool operator() ( const kurs & s, const kurs & t)
  8.     {
  10.         return s.wspolczynnik_sort < t.wspolczynnik_sort;
  12.     }
  14. };
Expand|Select|Wrap|Line Numbers
  1. std::sort(kursy_2.begin(),kursy_2.end(),sorting());
Hope it will be helpful to someone :)

Dec 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is...
by: Joe Van Dyk | last post by:
I have a vector of structs. Say I get a new struct that's supposed to replace one of the structs in the vector. I believe I could use replace_if() to do this, but I'm not sure what the syntax...
by: avimitrani | last post by:
Hi, I am trying to apply the sort() algorithm on a vector of structs. I want the vector to be sorted according to one of the struct fields. I see two ways in which this could be done, and neither...
by: sam961 | last post by:
Hi, I am a beginner in C++ so I hope you can be patient on me since my purpose is to learn. I am now involved in a relatively complicated Data Analysis exercise. I have a series of frames...
by: themadme | last post by:
im running code thats using threads, everytime i get this debug assertion failed box poping up. It involves with my stl vector. its saying that vector iterators incompatible. the vector...
by: engggirl3000 | last post by:
I was told to write a function that will read data, in a user defined text file, into a vector of structs. I am not sure where to start with this. Could some one tell me what is a vector of...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

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.