how can i make a set with comparison function ?

P

PicO

how can i make a set with comparison function ?

all i know that i can make a map with comparison function like this

struct strCmp {
bool operator()( const char* s1, const char* s2 ) const {
return strcmp( s1, s2 ) < 0;
}
};

map<const char*, int, strCmp> ages;

while how can i make set of integers with comparison function ?

struct Cmp
{
bool operator()( const int* s1, const int* s2 ) const {
return s1<=s2 ? true : false ;
}
};

set < int , Cmp > s;
 
I

Ian Collins

PicO said:
how can i make a set with comparison function ?

all i know that i can make a map with comparison function like this
Same with std::set.
 
P

PicO

However, once an element is in the set, you cannot modify the fields
that you use for the comparison.

i don't want to modify an element already in the set .. i want to get
set sort element or object by compare function as i want ( like in map
in example above ) ..
 
I

Ian Collins

PicO said:
i don't want to modify an element already in the set .. i want to get
set sort element or object by compare function as i want ( like in map
in example above ) ..
Then you have your answer.
 
I

Ian Collins

PicO said:
if i have my answer , I'll not post this article . the example above
is not legal for sets , so i asked for another one ...
Based on your original post, with a minor correction:

#include <set>
#include <iostream>

struct Cmp {
bool operator()( int s1, int s2 ) const {
return s1<=s2 ? true : false ;
}
};

int main() {
std::set<int,Cmp> s;

s.insert( 3 );
s.insert( 1 );
s.insert( 4 );
s.insert( 2 );

for( std::set<int,Cmp>::iterator i = s.begin(); i != s.end(); ++i ) {
std::cout << *i << std::endl;
}
}
 
J

James Kanze

However, once an element is in the set, you cannot modify the fields
that you use for the comparison.

You cannot modify an element of the set, period, because you
can't get anything but a const reference to it. (Whether this
is a bug or a feature of the standard depends on who you talk
to.)
 
J

James Kanze

struct Cmp {
bool operator()( int s1, int s2 ) const {
return s1<=s2 ? true : false ;

return s1 <= s2 ;

Please. What you've written strongly suggests that you don't
know what a boolean type is.
 
D

duane hebert

return s1 <= s2 ;
Please. What you've written strongly suggests that you don't
know what a boolean type is.


Why would a functor called Cmp return true for
anything other than == ? <g>

Shouldn't the set use a less functor with strict
weak ordering (return s1 < s2)?
 
P

PicO

Why would a functor called Cmp return true for
anything other than == ? <g>

Shouldn't the set use a less functor with strict
weak ordering (return s1 < s2)?

yea i use <= to be like mulitset ... that's it
 
P

PicO

Based on your original post, with a minor correction:

#include <set>
#include <iostream>

struct Cmp {
bool operator()( int s1, int s2 ) const {
return s1<=s2 ? true : false ;
}

};

int main() {
std::set<int,Cmp> s;

s.insert( 3 );
s.insert( 1 );
s.insert( 4 );
s.insert( 2 );

for( std::set<int,Cmp>::iterator i = s.begin(); i != s.end(); ++i ) {
std::cout << *i << std::endl;
}

}

thanks lan , it's working now .. but i want to know why it work with
int s1,s2 and not const int *s1,s2 ? ....
 
I

Ian Collins

*Please* dont quote signatures.
thanks lan , it's working now .. but i want to know why it work with
int s1,s2 and not const int *s1,s2 ? ....
Why should it? You are building a set of int, not int*. Were you
thinking of const references?
 
F

Frank Birbacher

PicO said:
yea i use <= to be like mulitset ... that's it

You must not. If you want to be like multiset, then use multiset. The
comparator must implement a strict weak ordering. This includes any
element x not to be less than itself. That is when compare(x, x) ==
false. Your function returns true however. So your function is NOT a
legal strict weak ordering.

See http://www.sgi.com/tech/stl/StrictWeakOrdering.html for invariants
of a strict weak ordering.

BTW, you should not be posting the same question to multiple newsgroups
at the same time. Wait a day or two for responses.

Frank
 
J

James Kanze

"James Kanze" <[email protected]> wrote in message
Why would a functor called Cmp return true for
anything other than == ? <g>
Shouldn't the set use a less functor with strict
weak ordering (return s1 < s2)?

You can specify the comparator type, and it must implement a
strict weak ordering. What you call it doesn't matter.
(Historically, I've always expected "compare" to be a three
state operation, returning distinguishable values for less than,
equal or greater than. But that doesn't work here, of course.
In fact, about the only place I can think of where it would be
directly usable is in a Fortran arithmetic if.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top