473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how can i make a set with comparison function ?

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
18 6473
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
14
by: Spoon | last post by:
Hello, I've come across the following comparison function used as the 4th parameter in qsort() int cmp(const void *px, const void *py) { const double *x = px, *y = py; return (*x > *y) -...
2
by: eastern_strider | last post by:
I'm running into problems about defining a comparison function for a map which has a user defined key. For example: class Key { public: string name; int number; Key (na, nu) : name (na),...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
3
by: DragonLord | last post by:
Here is the situation I am inserting rows into a datagridview and then using a function to group similar rows together based on a column. When I call to compare the lastrow with the current row...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.