I wanna sort a pointer vector with the algorithm "sort" in STL like
this:
"
int compare(vector<string*>::iterator 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 3528
Magcialking wrote:
I wanna sort a pointer vector with the algorithm "sort" in STL like
this:
"
int compare(vector<string*>::iterator 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*>::iterator 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_function<It,It,bool>
{
public:
deref_pred() {}
deref_pred(const Pred &pred) : pred(pred) {}
bool operator()(const 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::less<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_function<It,It,bool>
{
public:
deref_pred() {}
deref_pred(const Pred &pred) : pred(pred) {}
bool operator()(const 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::less<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_function<It,It,bool>,
what's it for?
Magcialking wrote:
>#include <functional>
template <typename It,typename Pred> class deref_pred : public std::binary_function<It,It,bool> { public:
deref_pred() {} deref_pred(const Pred &pred) : pred(pred) {}
bool operator()(const 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::less<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*container 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_function<It,It,bool>,
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_function<It,It,bool>
{
public:
deref_pred() {}
deref_pred(const Pred &pred) : pred(pred) {}
bool operator()(const 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::less<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*container 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_function<It,It,bool>,
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!
"Magcialking" <ma********@163.comschrieb im Newsbeitrag
news:11**********************@m73g2000cwd.googlegr oups.com...
>I wanna sort a pointer vector with the algorithm "sort" in STL like
this:
"
int compare(vector<string*>::iterator 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.begin(), vec.end(), MySort() );
}
Question: Why are you holding a string* instead of a string in your
vector?
Gernot Frisch 写道:
"Magcialking" <ma********@163.comschrieb im Newsbeitrag
news:11**********************@m73g2000cwd.googlegr oups.com...
I wanna sort a pointer vector with the algorithm "sort" in STL like
this:
"
int compare(vector<string*>::iterator 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.begin(), 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*********************@i3g2000cwc.googlegroups.c om>, 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?
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.
Doesn't mean that it isn't (in the context) expensive to construct, I
believe a copy means the contents are also copied?
In article <11*********************@e3g2000cwe.googlegroups.c om>, ju***@liimatta.org says...
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.
Doesn't mean that it isn't (in the context) expensive to construct, I
believe a copy means the contents are also copied?
You clearly have one copy that takes place when you put the string into
the vector. From there it depends: when the vector expands, it can copy
construct all the strings into the newly expanded area, then destroy the
old ones -- which (as you suggest) would result in a (relatively slow
deep copy of each of the existing strings -- not to mention temporarily
using a lot of extra memory.
For nearly anything that uses remote ownership (i.e. anything with a
difference between shallow and deep copy), however, that's not usually
the best way. Instead, you can create your new, expanded memory full of
empty items (strings in this case), and then use swap to put the real
ones into the new memory and the empty ones into the old memory, without
copying the actual data.
Of course, on a platform where it get away with it (i.e. most) the
library can just do a shallow copy from the old space to the new, and be
done with it. It's not portable, but the library doesn't need to be
portable internally -- it just has to provide a portable interface. This
is the sort of thing for which template specialization was invented...
--
Later,
Jerry.
The universe is a figment of its own imagination. 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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |