I wanna sort a pointer vector with the algorithm "sort" in STL like
this:
"
int compare(vector< string*>::itera tor a,vector<string *>::iterator b){
return (**a)<(**b);
}
sort(iter,iter_ end,compare);
"
but this dosen't work,and I really don't know why.
Could anyone show me how to use this correctly? 11 3539
Magcialking wrote:
I wanna sort a pointer vector with the algorithm "sort" in STL like
this:
"
int compare(vector< string*>::itera tor a,vector<string *>::iterator b){
return (**a)<(**b);
}
should be:
int compare(const string *a, const string *b)
{
return *a < *b;
}
sort(iter,iter_ end,compare);
"
but this dosen't work,and I really don't know why.
Could anyone show me how to use this correctly?
Magcialking wrote:
I wanna sort a pointer vector with the algorithm "sort" in STL like
this:
"
int compare(vector< string*>::itera tor a,vector<string *>::iterator b){
return (**a)<(**b);
}
sort(iter,iter_ end,compare);
"
You don't specify the type of iter and iter_end, but I am going to
assume that they're of type vector<string*> ::iterator.
>
but this dosen't work,and I really don't know why.
Could anyone show me how to use this correctly?
The problem is that your comparison function takes iterators as
parameters, when it should take values. Additionally, it should return
a bool. For example:
bool compare(string *a,string *b) {
return (*a) < (*b);
}
Another thing you might consider is writing a generic dereferencing
binary predicate adapter:
#include <functional>
template <typename It,typename Pred>
class deref_pred : public std::binary_fun ction<It,It,boo l>
{
public:
deref_pred() {}
deref_pred(cons t Pred &pred) : pred(pred) {}
bool operator()(cons t It &a,const It &b) const
{ return pred(*a,*b); }
private:
Pred pred;
};
Then you would call sort like:
std::sort(iter, iter_end,
deref_pred<std: :string*,std::l ess<std::string ());
This is a bit more work on the front end, but it's much easier to reuse.
Hope this helps,
Nate
Another thing you might consider is writing a generic dereferencing
binary predicate adapter:
#include <functional>
template <typename It,typename Pred>
class deref_pred : public std::binary_fun ction<It,It,boo l>
{
public:
deref_pred() {}
deref_pred(cons t Pred &pred) : pred(pred) {}
bool operator()(cons t It &a,const It &b) const
{ return pred(*a,*b); }
private:
Pred pred;
};
Then you would call sort like:
std::sort(iter, iter_end,
deref_pred<std: :string*,std::l ess<std::string ());
it seems that deref_pred is just a wrapping paper,the real compare
function need to be pass to it when used?so, what's deref_pred doing
here?
and I am also confuse about the inheritance here:public
std::binary_fun ction<It,It,boo l>,
what's it for?
Magcialking wrote:
>#include <functional>
template <typename It,typename Pred> class deref_pred : public std::binary_fun ction<It,It,boo l> { public:
deref_pred() {} deref_pred(cons t Pred &pred) : pred(pred) {}
bool operator()(cons t It &a,const It &b) const { return pred(*a,*b); }
private:
Pred pred; };
Then you would call sort like:
std::sort(iter ,iter_end, deref_pred<std: :string*,std::l ess<std::string ());
it seems that deref_pred is just a wrapping paper,the real compare
function need to be pass to it when used?so, what's deref_pred doing
here?
It dereferences the pointers/iterators passed as arguments, which allows
it to work on containers of pointers/iterators, like the
std::vector<std ::string*contai ner you have. The reason for the Pred
parameter is that you can change the predicate to, say, std::greater, if
you wish to reverse the sort.
and I am also confuse about the inheritance here:public
std::binary_fun ction<It,It,boo l>,
what's it for?
This base class does nothing other than define a few typedefs that allow
it to be used more easily with other parts of the STL.
See http://www.sgi.com/tech/stl/functors.html, specifically the third
paragraph of the Description section.
Nate
Nate Barney 写道:
Magcialking wrote:
#include <functional>
template <typename It,typename Pred>
class deref_pred : public std::binary_fun ction<It,It,boo l>
{
public:
deref_pred() {}
deref_pred(cons t Pred &pred) : pred(pred) {}
bool operator()(cons t It &a,const It &b) const
{ return pred(*a,*b); }
private:
Pred pred;
};
Then you would call sort like:
std::sort(iter, iter_end,
deref_pred<std: :string*,std::l ess<std::string ());
it seems that deref_pred is just a wrapping paper,the real compare
function need to be pass to it when used?so, what's deref_pred doing
here?
It dereferences the pointers/iterators passed as arguments, which allows
it to work on containers of pointers/iterators, like the
std::vector<std ::string*contai ner you have. The reason for the Pred
parameter is that you can change the predicate to, say, std::greater, if
you wish to reverse the sort.
and I am also confuse about the inheritance here:public
std::binary_fun ction<It,It,boo l>,
what's it for?
This base class does nothing other than define a few typedefs that allow
it to be used more easily with other parts of the STL.
See http://www.sgi.com/tech/stl/functors.html, specifically the third
paragraph of the Description section.
Nate
OK, I got it. Thanks a lot!
"Magcialkin g" <ma********@163 .comschrieb im Newsbeitrag
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
>I wanna sort a pointer vector with the algorithm "sort" in STL like
this:
"
int compare(vector< string*>::itera tor a,vector<string *>::iterator
b){
return (**a)<(**b);
}
sort(iter,iter_ end,compare);
struct MySort
{
bool operator() (const string*& p1, const string*& p2)
{
return *p1 < *p2;
}
};
int main()
{
std::vector<std ::string*vec;
// fill vector
std::sort(vec.b egin(), vec.end(), MySort() );
}
Question: Why are you holding a string* instead of a string in your
vector?
Gernot Frisch 写道:
"Magcialkin g" <ma********@163 .comschrieb im Newsbeitrag
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
I wanna sort a pointer vector with the algorithm "sort" in STL like
this:
"
int compare(vector< string*>::itera tor a,vector<string *>::iterator
b){
return (**a)<(**b);
}
sort(iter,iter_ end,compare);
struct MySort
{
bool operator() (const string*& p1, const string*& p2)
{
return *p1 < *p2;
}
};
int main()
{
std::vector<std ::string*vec;
// fill vector
std::sort(vec.b egin(), vec.end(), MySort() );
}
Question: Why are you holding a string* instead of a string in your
vector?
Cause I think this will save time than to push string itself into a
vector.
In article <11************ *********@i3g20 00cwc.googlegro ups.com>, ma********@163. com says...
[ ... ]
Question: Why are you holding a string* instead of a string in your
vector?
Cause I think this will save time than to push string itself into a
vector.
A string object isn't usually a _whole_ lot more than a wrapper around a
pointer and a couple of size_t's. Depending on implementation, it may
also include a _small_ array of elements, but it's still usually pretty
fast to copy around and such.
--
Later,
Jerry.
The universe is a figment of its own imagination.
Question: Why are you holding a string* instead of a string in your
vector?
Cause I think this will save time than to push string itself into a
vector.
And you know you have to delete the strings themselfes some day?? The
string you forgot, because you put it's pointer in the vector?
It's OK if you do, but are you aware if it? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have any preconceived ideas about it. I have
noticed, however, that every programmer I talk to who's aware of Python is
also talking about Ruby. So it seems that Ruby has the potential to compete
with and displace Python. I'm curious on what basis it...
|
by: Brandon J. Van Every |
last post by:
I'm realizing I didn't frame my question well.
What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump
up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN'
COOL!!! ***MAN*** that would save me a buttload of work and make my life
sooooo much easier!"
As opposed to minor differences of this feature here, that feature there.
Variations on style are of no interest to me. I'm coming at this from a...
|
by: Marco Aschwanden |
last post by:
Hi
I don't have to talk about the beauty of Python and its clear and
readable syntax... but there are a few things that striked me while
learning Python.
I have collected those thoughts. I am sure there are many discussions
on the "problems" mentioned here. But I had this thoughts without
looking into any forums or anything... it is kind of feedback.
|
by: Marco Aschwanden |
last post by:
I just read the changes for 2.4 and while scanning the list the past tense
built-ins got my attention:
sorted() - a new builtin sorted() acts like an in-place list.sort() but
can be used in expressions, as it returns a copy of the sequence, sorted.
reversed() - a new builtin that takes a sequence and returns an iterator
that loops over the elements of the sequence in reverse order (PEP 322)
|
by: Sarah Tanembaum |
last post by:
Beside its an opensource and supported by community, what's the fundamental
differences between PostgreSQL and those high-price commercial database (and
some are bloated such as Oracle) from software giant such as Microsoft SQL
Server, Oracle, and Sybase?
Is PostgreSQL reliable enough to be used for high-end commercial
application? Thanks
| |
by: Shi Mu |
last post by:
Got confused by the following code:
>>> a
>>> b
>>> c
{1: , ], 2: ]}
>>> c.append(b.sort())
>>> c
{1: , ], 2: , None]}
|
by: Cameron Laird |
last post by:
QOTW: "Python makes it easy to implement algorithms." - casevh
"Most of the discussion of immutables here seems to be caused by
newcomers wanting to copy an idiom from another language which doesn't
have immutable variables. Their real problem is usually with binding,
not immutability." - Mike Meyer
Among the treasures available in The Wiki is the current
copy of "the Sorting min-howto":
|
by: neilcancer |
last post by:
My English is poor...
There are some sort algorithms which sort a sequencial list. The
sequencial list was defined in "list-seq.h".
sort_and_time() accepts one of these sort functions. It uses the sort
function to sort the list and compute the time consumed by the sort
function.
I wrote the program as below. When sort_and_time() is called in main(),
|
by: gaoxtwarrior |
last post by:
a sort which is stable means it keeps the object's original order. A sort
which is in place is stable. does anyone can explain me what is sort in
place?
thx.
|
by: Steven D'Aprano |
last post by:
Am I the only one who finds that I'm writing more documentation than code?
I recently needed to write a function to generate a rank table from a
list. That is, a list of ranks, where the rank of an item is the position
it would be in if the list were sorted:
alist = list('defabc')
ranks =
To do that, I needed to generate an index table first. In the book
|
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets 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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |