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 5 3905
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 ) );
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
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));
}
"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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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_) {
|
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 like I could
not find any related topic in the book.
|
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 and gives the error
message (in Borland C++Builder 6)
Unit1.cpp E2285 Could not find a match for
|
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 vector,
which is passed to the function not by reference but by value.
Now, at the end of this mail...
|
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 ---------------------
| |
by: Peter Olcott |
last post by:
Does anyone know how to do this?
|
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*>.
|
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 3.4.2). I'm just hoping a definitive answer
resides in one of the brains who frequent this group.
|
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 some call optimization.
My 'hand coded' version is based on D. Musser's suggestions and on
some...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |