473,543 Members | 4,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help on std::sort comparison function


Good afternoon,
I need some advice on the following:

I've got a class that has a member
std::vector<CSt ringm_vFileName and a member CString m_path;
The vector contains a bunch of filenames with no path included
(no C:\...) eg: my_file2.jpg, my_file1.bmp, etc...
and m_path stores the path, eg: C:\folder1

I want to sort this vector according to different criterion, such as
filename(asc, desc), date(asc, desc), size(asc, desc).

To sort the vector by filename I use this comparison function:
bool sortVectorByNam eUp(CString s1, CString s2)
{ return (s1.CompareNoCa se(s2) <= 0)? true : false; }

the call is std::sort(m_vFi leName.begin(), m_vFileName.end (),
sortVectorByNam eUp);

To sort this vector by date I need to obtain last access time for every
file, and for that, I need to read m_path :(
I can't pass m_path as a parameter because (I think), sortVectorByNam eUp
must be a binary function.

If I make this function a member of my class
eg: bool Cteste8View::so rtVectorByNameU p(CString s1, CString s2)

I get compilation errors:
..\teste8View.c pp(478) : error C3867: 'Cteste8View::s ortVectorByName Up':
function call missing argument list; use
'&Cteste8View:: sortVectorByNam eUp' to create a pointer to member
..\teste8View.c pp(478) : error C2780: 'void std::sort(_RanI t,_RanIt)' :
expects 2 arguments - 3 provided
C:\Program Files\Microsoft Visual Studio 8\VC\include\al gorithm
(2751) : see declaration of 'std::sort'

I've tried making the call like this:
std::sort(m_vFi leName.begin(), m_vFileName.end (),
&Cteste8View::s ortVectorByName Up);
but it doesn't work either

Is there a way to have access to m_path inside the comparison function,
besides using a global variable as intermediate?
And what is the best way to get size and last access time from a file?

Thanks a lot in advance
--
fade
email: fade(*AT*)forwa rd(*dot*)to
Dec 21 '06 #1
5 3888
In article <Xn************ ********@194.65 .14.158>,
fade <do************ ****@mail.comwr ote:
Good afternoon,
I need some advice on the following:

I've got a class that has a member
std::vector<CSt ringm_vFileName and a member CString m_path;
The vector contains a bunch of filenames with no path included
(no C:\...) eg: my_file2.jpg, my_file1.bmp, etc...
and m_path stores the path, eg: C:\folder1

I want to sort this vector according to different criterion, such as
filename(asc, desc), date(asc, desc), size(asc, desc).

To sort the vector by filename I use this comparison function:
bool sortVectorByNam eUp(CString s1, CString s2)
{ return (s1.CompareNoCa se(s2) <= 0)? true : false; }

the call is std::sort(m_vFi leName.begin(), m_vFileName.end (),
sortVectorByNam eUp);

To sort this vector by date I need to obtain last access time for every
file, and for that, I need to read m_path :(
I can't pass m_path as a parameter because (I think), sortVectorByNam eUp
must be a binary function.

If I make this function a member of my class
eg: bool Cteste8View::so rtVectorByNameU p(CString s1, CString s2)

I get compilation errors:
.\teste8View.cp p(478) : error C3867: 'Cteste8View::s ortVectorByName Up':
function call missing argument list; use
'&Cteste8View:: sortVectorByNam eUp' to create a pointer to member
.\teste8View.cp p(478) : error C2780: 'void std::sort(_RanI t,_RanIt)' :
expects 2 arguments - 3 provided
C:\Program Files\Microsoft Visual Studio 8\VC\include\al gorithm
(2751) : see declaration of 'std::sort'

I've tried making the call like this:
std::sort(m_vFi leName.begin(), m_vFileName.end (),
&Cteste8View::s ortVectorByName Up);
but it doesn't work either

Is there a way to have access to m_path inside the comparison function,
besides using a global variable as intermediate?
And what is the best way to get size and last access time from a file?

Thanks a lot in advance
struct SortVectorByDat eUp : binary_function < CString, CString, bool >
{
const CString path;
SortVectorByDat eUp( const CString& path_ ): path( path_ ) { }
bool operator()( const CString& left, const CString& right ) const
{
// do whatever you need to do here
}
};

// in body of member function

sort( m_vFileName.beg in(), m_vFileName.end (),
SortVectorByDat eUp( m_path ) );
Dec 21 '06 #2

fade wrote:
Good afternoon,
I need some advice on the following:

I've got a class that has a member
std::vector<CSt ringm_vFileName and a member CString m_path;
The vector contains a bunch of filenames with no path included
(no C:\...) eg: my_file2.jpg, my_file1.bmp, etc...
and m_path stores the path, eg: C:\folder1

I want to sort this vector according to different criterion, such as
filename(asc, desc), date(asc, desc), size(asc, desc).

To sort the vector by filename I use this comparison function:
bool sortVectorByNam eUp(CString s1, CString s2)
{ return (s1.CompareNoCa se(s2) <= 0)? true : false; }
That doesn't sort the vector by filename, it sorts copies which
can/might be modified, not to mention the waste of invoking additional
ctors and d~tors. That statement might sound a little strange for
someone that is doing MFC, or whatever proprietary language you are
using, but thats absolutely fundamental in C++. Comparison parameters
must be const references. Doing otherwise is shooting yourself in the
foot.
Try:
bool sortVectorByNam eUp(const CString& s1, const CString& s2)
{
// ...
}

You should both consider and study associative containers (like
std::set or std::map). These sort at insertion and can simply rely on
an operator< overload to order the container. If you decide to go that
route, you'll find out how dangerous it is not to use const references
in this case.
>
the call is std::sort(m_vFi leName.begin(), m_vFileName.end (),
sortVectorByNam eUp);

To sort this vector by date I need to obtain last access time for every
file, and for that, I need to read m_path :(
I can't pass m_path as a parameter because (I think), sortVectorByNam eUp
must be a binary function.

If I make this function a member of my class
eg: bool Cteste8View::so rtVectorByNameU p(CString s1, CString s2)
no, no. Assuming that the vector in question is part of the CTeste8View
class, you don't provide a member function that sorts the vector member
within by passing its elements to an interface of the containing class.
Thats not logical and will obviously fail in an algorithm.
Your vector should be storing Elements of some structure or class, that
class/struct is where the op< or comparator should act upon. One does
not ask an apple tree to compare apples. Trees compare trees and apples
compare apples.
>
I get compilation errors:
.\teste8View.cp p(478) : error C3867: 'Cteste8View::s ortVectorByName Up':
function call missing argument list; use
'&Cteste8View:: sortVectorByNam eUp' to create a pointer to member
.\teste8View.cp p(478) : error C2780: 'void std::sort(_RanI t,_RanIt)' :
expects 2 arguments - 3 provided
The error is expected. sort is being asked to act on a Cteste8View
object and yet the iterators are iterating through CStrings.
C:\Program Files\Microsoft Visual Studio 8\VC\include\al gorithm
(2751) : see declaration of 'std::sort'

I've tried making the call like this:
std::sort(m_vFi leName.begin(), m_vFileName.end (),
&Cteste8View::s ortVectorByName Up);
but it doesn't work either

Is there a way to have access to m_path inside the comparison function,
besides using a global variable as intermediate?
And what is the best way to get size and last access time from a file?

Thanks a lot in advance
--
fade
email: fade(*AT*)forwa rd(*dot*)to
Dec 21 '06 #3

fade wrote:
Good afternoon,
I need some advice on the following:

I've got a class that has a member
std::vector<CSt ringm_vFileName and a member CString m_path;
The vector contains a bunch of filenames with no path included
(no C:\...) eg: my_file2.jpg, my_file1.bmp, etc...
and m_path stores the path, eg: C:\folder1

I want to sort this vector according to different criterion, such as
filename(asc, desc), date(asc, desc), size(asc, desc).

To sort this vector by date I need to obtain last access time for every
file, and for that, I need to read m_path :(
I can't pass m_path as a parameter because (I think), sortVectorByNam eUp
must be a binary function.
Make it a functor, an object that has an operator() that takes two
params, or make it a member function and use a binder or boost::bind (I
recommend the later) to tie it to the instance of the object in
question.
I've tried making the call like this:
std::sort(m_vFi leName.begin(), m_vFileName.end (),
&Cteste8View::s ortVectorByName Up);
Cteste8View::so rtByNameUp()
{
std::sort(begin (), end(), boost::bind(&CT este8View::sort VByName,
this, _1, _2));
}

Dec 21 '06 #4
"Salt_Peter " <pj*****@yahoo. comwrote:
fade wrote:

I've got a class that has a member
std::vector<CSt ringm_vFileName and a member CString m_path;
The vector contains a bunch of filenames with no path included
(no C:\...) eg: my_file2.jpg, my_file1.bmp, etc...
and m_path stores the path, eg: C:\folder1

I want to sort this vector according to different criterion, such as
filename(asc, desc), date(asc, desc), size(asc, desc).

To sort the vector by filename I use this comparison function:
bool sortVectorByNam eUp(CString s1, CString s2)
{ return (s1.CompareNoCa se(s2) <= 0)? true : false; }

That doesn't sort the vector by filename, it sorts copies which
can/might be modified, not to mention the waste of invoking additional
ctors and d~tors. That statement might sound a little strange for
someone that is doing MFC, or whatever proprietary language you are
using, but thats absolutely fundamental in C++. Comparison parameters
must be const references. Doing otherwise is shooting yourself in the
foot.
Your assertion is incorrect. The only thing that the standard requires
of the predicate is that it satisfy the standard mathematical definition
of a strict weak ordering.

The additional c_tors and d_tors that are invoked, might, or might not,
prove to be a performance hit, one can't know without testing.

That said, parameters of 'const T&' and 'T' are logically equivelent and
the former is highly unlikely to be slower than the latter, and for
non-builtin types, is likely to be faster. So I do agree that the OP
should try using 'const T&' instead.
Dec 21 '06 #5
struct SortVectorByDat eUp : binary_function < CString, CString, bool >
{
const CString path;
SortVectorByDat eUp( const CString& path_ ): path( path_ ) { }
bool operator()( const CString& left, const CString& right ) const
{
// do whatever you need to do here
}
};

// in body of member function

sort( m_vFileName.beg in(), m_vFileName.end (),
SortVectorByDat eUp( m_path ) );
Thanks a lot Daniel, Peter and Noah.
Merry Xmas to all.

--
fade
email: fade(*AT*)forwa rd(*dot*)to
Dec 23 '06 #6

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

Similar topics

8
3493
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function< ValuePair_t, ValuePair_t, bool> ValuePair_IsLess; void SortAscend(const ValuePair_IsLess& isLess_) {
6
2323
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides "vector"? Are "istringstream" and "ostringstream" covered in the book, "STL Tutorial and Reference" (second edition) by D. Musser, et al.? Seems...
4
3317
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and if so, why not? Shouldn the compiler be able to tell which of he overloaded functions to use? The second A::comp() is the one I accidently added...
7
3533
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that there is a huge overhead of using this function, which comes from copying the function object, i.e., the object that compares the elements of the...
8
4212
by: Manfred | last post by:
Hello I am new to template programming, so i tried the 'example' from http://www.sgi.com/tech/stl/functors.html. I can compile the code but when i want to run the program I get a segmentation fault when the part of std::sort(...) is reached, but I don't understand why. ------------ Code start ---------------------
15
3315
by: Peter Olcott | last post by:
Does anyone know how to do this?
4
4511
by: prakashsahni | last post by:
I am using a sort func object like struct mystruct { bool operator () (MyClass* const &a, MyClass* const&b) {}; } Invoke it like std::sort(vec.begin(), vec.end(),mystruct); Where vec is an std::vector<MyClass*>.
11
656
by: Jeff Schwab | last post by:
Would std::sort ever compare an object with itself? I'm not talking about two distinct, equal-valued objects, but rather this == &that. The container being sorted is a std::vector. I've never seen this, but a coworker says he is. NB: I can't post sample code that reproduces the issue, nor do I claim any bug in the STL implementation (GCC...
10
6229
by: ikarus | last post by:
Hello C++ Gurus! I'm comparing sorting algorithm for study goals. I've compared STL std::sort and hand-coded introsort on millions (tens of millions) of integers array sorting. It was tested on random, sorted, and sorted in reverse arrays. In all tests on such a huge arrays std::sort is faster by ~1/4 then my version. So it seems that it...
0
7402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7347
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7590
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7733
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7344
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5264
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.