Sorting a vector
-
Hello! I'm trying to use a vector, but it seems like I'm not that clever. How can I sort my vector by one of the Class attributes, in my case ctDate? typedef struct tagSCHEDULEBLOCK { CTime ctDate; CString csText; }SCHEDULEBLOCK; vector MyVector; Thanks for all your help! /DrZOO
-
Hello! I'm trying to use a vector, but it seems like I'm not that clever. How can I sort my vector by one of the Class attributes, in my case ctDate? typedef struct tagSCHEDULEBLOCK { CTime ctDate; CString csText; }SCHEDULEBLOCK; vector MyVector; Thanks for all your help! /DrZOO
you will have to write a comparison function that looks something like this:
bool date_sort(const SCHEDULEBLOCK &a, const SCHEDULEBLOCK &b)
{
return (a.ctDate < b.ctDate);
}then you can sort it like this:
std::sort(vec.begin(), vec.end(), date_sort);
-c
Cheap oil. It's worth it!
-
you will have to write a comparison function that looks something like this:
bool date_sort(const SCHEDULEBLOCK &a, const SCHEDULEBLOCK &b)
{
return (a.ctDate < b.ctDate);
}then you can sort it like this:
std::sort(vec.begin(), vec.end(), date_sort);
-c
Cheap oil. It's worth it!
It looks nice, but for some reason i get an error; d:\Program Files\Microsoft Visual Studio .NET\Vc7\include\algorithm(1856): error C2064: term does not evaluate to a function which point to row 2 and 3 in the code below (from algorithm) while (_First < _Pfirst && !_Pred(*(_Pfirst - 1), *_Pfirst) && !_Pred(*_Pfirst, *(_Pfirst - 1))) --_Pfirst; Can you please help me? Should I pass some parameters to date_sort? Thanks once again lifesaver ;) /DrZOO
-
It looks nice, but for some reason i get an error; d:\Program Files\Microsoft Visual Studio .NET\Vc7\include\algorithm(1856): error C2064: term does not evaluate to a function which point to row 2 and 3 in the code below (from algorithm) while (_First < _Pfirst && !_Pred(*(_Pfirst - 1), *_Pfirst) && !_Pred(*_Pfirst, *(_Pfirst - 1))) --_Pfirst; Can you please help me? Should I pass some parameters to date_sort? Thanks once again lifesaver ;) /DrZOO
it compiles fine for me.
#include "stdafx.h"
#include #include typedef struct tagSCHEDULEBLOCK
{ CTime ctDate;
CString csText;
}SCHEDULEBLOCK;bool date_sort(const SCHEDULEBLOCK &a, const SCHEDULEBLOCK &b){ return (a.ctDate < b.ctDate);}
void Test()
{
std::vector vec;
std::sort(vec.begin(), vec.end(), date_sort);
}-c
Cheap oil. It's worth it!
-
it compiles fine for me.
#include "stdafx.h"
#include #include typedef struct tagSCHEDULEBLOCK
{ CTime ctDate;
CString csText;
}SCHEDULEBLOCK;bool date_sort(const SCHEDULEBLOCK &a, const SCHEDULEBLOCK &b){ return (a.ctDate < b.ctDate);}
void Test()
{
std::vector vec;
std::sort(vec.begin(), vec.end(), date_sort);
}-c
Cheap oil. It's worth it!
I think my problems are because my vector holds pointers (vector < SCHEDULEBLOCK* >). I read Paul Wolfensberger's article "Using the std::sort() Method" before but I don't quite get it. This code works...; struct MySort { bool operator()(const SCHEDULEBLOCK* a, const SCHEDULEBLOCK* b) { return (a->ctBlockDate < b->ctBlockDate); } }; ... but it doesn't look that nice. /DrZOO
-
I think my problems are because my vector holds pointers (vector < SCHEDULEBLOCK* >). I read Paul Wolfensberger's article "Using the std::sort() Method" before but I don't quite get it. This code works...; struct MySort { bool operator()(const SCHEDULEBLOCK* a, const SCHEDULEBLOCK* b) { return (a->ctBlockDate < b->ctBlockDate); } }; ... but it doesn't look that nice. /DrZOO
this defines the comparison operation as a function of the object. the way i showed defined the comparison as a global function. either way is fine. DrZOO wrote: it doesn't look that nice it's STL - it shouldn't look nice! :) -c
Cheap oil. It's worth it!
-
this defines the comparison operation as a function of the object. the way i showed defined the comparison as a global function. either way is fine. DrZOO wrote: it doesn't look that nice it's STL - it shouldn't look nice! :) -c
Cheap oil. It's worth it!