473,506 Members | 17,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL set members sorted?

I am studying STL set and write a small example to understand it. But
my program output does not show that the members are sorted. Could
anyone help me to see what's wrong? Thanks.

// A.h
#include <functional>

class A {
public:
A(int);
int getA();
private:
int a;
};

int compare(A*, A*);

struct Compare : public std::binary_function<A*, A*, bool> {
bool operator()(A *aoo, A *boo) const {
return compare(aoo, boo);
}
};

// A.C
#include "A.h"

A::A(int b) : a(b) {}
int A::getA() { return a; }

int compare(A *aoo, A *boo) {
if (aoo->getA() < boo->getA()) return -1;
if (aoo->getA() > boo->getA()) return 1;
return 0;
}

// Main.C
#include <iostream>
#include <set>
#include "A.h"
using namespace std;

int main() {
set<A *, Compare> aset;
A a2(2), a3(3), a1(1);
aset.insert(&a2); aset.insert(&a3); aset.insert(&a1);
for (set<A *, Compare>::iterator it = aset.begin(); it != aset.end();
it++)
cout << (*it)->getA() << endl;
return 0;
}

$ g++ -o Main Main.C A.C
$ ./Main
1
3
2

Mar 17 '06 #1
1 1748
<ch********@gmail.com> schrieb im Newsbeitrag news:11*********************@v46g2000cwv.googlegro ups.com...
....
int compare(A*, A*);

struct Compare : public std::binary_function<A*, A*, bool> {
bool operator()(A *aoo, A *boo) const {
return compare(aoo, boo);
}
}; .... int compare(A *aoo, A *boo) {
if (aoo->getA() < boo->getA()) return -1;
if (aoo->getA() > boo->getA()) return 1;
return 0;
}


This does not define a strikt weak ordering, so it is not valid to be used with std::set, std::map or std::multiset. Basically such a function should behave like "less than", but yours implements some sort of "not equal". Change the return statement in operator() above to

return compare(aoo, boo) < 0

HTH
Heinz
Mar 17 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

57
3547
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
3
1620
by: sam | last post by:
Hello group, The datagrid is sorted using a dataview. Can I get the sorted dataview or table from the datagrid or any other way. All I need to get is the sorted data. Here is my code and what I am...
2
2294
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
0
1242
by: Misa | last post by:
Hi there How to enumerate members of a group sorted by one of the user's properties. I'm wondering what's default order that usual "foreach" enumeration will produce ? Someone will...
1
1627
by: J L | last post by:
I want to create a sorted list whose values are themselves sorted lists. I wrote the following simple test program but it does not behave as I would expect. What I wanted to do was have the...
4
5218
by: Santosh Nayak | last post by:
Hi, Is it possible to sort the array of struct based on the data members. e.g. struct { int a ; float b ; char c ; } TEMP ;
18
2928
by: Hunk | last post by:
Would like some advice on the fillowing I have a sorted list of items on which i require to search and retrieve the said item and also modify an item based on its identity. I think an Map stl...
3
2038
by: ajb1983 | last post by:
Dear users, I have just registered with this forum, and introduced myself as such: http://www.thescripts.com/forum/thread776349.html My question regards a 2d array, to be sorted according to...
1
3088
by: Harold Howe | last post by:
Howdy all, The msdn help says this about SorteList<k,v>: "If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary." My question is this: how do I...
0
7220
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
7105
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...
0
7308
Oralloy
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,...
0
7371
jinu1996
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...
1
7023
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...
0
4702
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...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.