By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,984 Members | 2,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,984 IT Pros & Developers. It's quick & easy.

how can i make a set with comparison function ?

P: n/a
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, strCmpages;

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;

Aug 9 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
PicO wrote:
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.

--
Ian Collins.
Aug 9 '07 #2

P: n/a
Ian Collins wrote:
PicO wrote:
>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.
However, once an element is in the set, you cannot modify the fields
that you use for the comparison.
Aug 9 '07 #3

P: n/a
On Aug 9, 12:25 am, red floyd <no.s...@here.dudewrote:
Ian Collins wrote:
PicO wrote:
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.

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 ) ..

Aug 9 '07 #4

P: n/a
PicO wrote:
On Aug 9, 12:25 am, red floyd <no.s...@here.dudewrote:
>Ian Collins wrote:
>>PicO wrote:
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.
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 ) ..
Then you have your answer.

--
Ian Collins.
Aug 9 '07 #5

P: n/a
On Aug 9, 12:50 am, Ian Collins <ian-n...@hotmail.comwrote:
PicO wrote:
On Aug 9, 12:25 am, red floyd <no.s...@here.dudewrote:
Ian Collins wrote:
PicO wrote:
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.
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 ) ..

Then you have your answer.

--
Ian Collins.
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 ...

Aug 9 '07 #6

P: n/a
On Aug 9, 12:50 am, Ian Collins <ian-n...@hotmail.comwrote:
PicO wrote:
On Aug 9, 12:25 am, red floyd <no.s...@here.dudewrote:
Ian Collins wrote:
PicO wrote:
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.
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 ) ..

Then you have your answer.

--
Ian Collins.
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 ...

Aug 9 '07 #7

P: n/a
On Aug 9, 12:50 am, Ian Collins <ian-n...@hotmail.comwrote:
PicO wrote:
On Aug 9, 12:25 am, red floyd <no.s...@here.dudewrote:
Ian Collins wrote:
PicO wrote:
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.
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 ) ..

Then you have your answer.

--
Ian Collins.
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 ...

Aug 9 '07 #8

P: n/a
On Aug 9, 12:50 am, Ian Collins <ian-n...@hotmail.comwrote:
PicO wrote:
On Aug 9, 12:25 am, red floyd <no.s...@here.dudewrote:
Ian Collins wrote:
PicO wrote:
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.
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 ) ..

Then you have your answer.

--
Ian Collins.
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 ...

Aug 9 '07 #9

P: n/a
PicO wrote:
>
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,Cmps;

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;
}
}
--
Ian Collins.
Aug 9 '07 #10

P: n/a
PicO wrote:
>
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,Cmps;

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;
}
}
--
Ian Collins.
Aug 9 '07 #11

P: n/a
PicO wrote:
>
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,Cmps;

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;
}
}
--
Ian Collins.
Aug 9 '07 #12

P: n/a
PicO wrote:
>
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,Cmps;

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;
}
}
--
Ian Collins.
Aug 9 '07 #13

P: n/a
On Aug 9, 12:50 am, Ian Collins <ian-n...@hotmail.comwrote:
PicO wrote:
On Aug 9, 12:25 am, red floyd <no.s...@here.dudewrote:
Ian Collins wrote:
PicO wrote:
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.
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 ) ..

Then you have your answer.

--
Ian Collins.
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 ...

Aug 9 '07 #14

P: n/a
PicO wrote:
>
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,Cmps;

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;
}
}
--
Ian Collins.
Aug 9 '07 #15

P: n/a
On Aug 9, 9:25 am, red floyd <no.s...@here.dudewrote:
Ian Collins wrote:
PicO wrote:
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.
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 9 '07 #16

P: n/a
On Aug 9, 10:16 am, Ian Collins <ian-n...@hotmail.comwrote:
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.
}
};
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 9 '07 #17

P: n/a
On Aug 9, 1:16 am, Ian Collins <ian-n...@hotmail.comwrote:
PicO wrote:
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,Cmps;

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;
}

}

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

Aug 10 '07 #18

P: n/a
PicO wrote:
On Aug 9, 1:16 am, Ian Collins <ian-n...@hotmail.comwrote:
>PicO wrote:
>>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 ;
}

*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?

--
Ian Collins.
Aug 10 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.