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_fun ction<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>::itera tor it = aset.begin(); it != aset.end();
it++)
cout << (*it)->getA() << endl;
return 0;
}
$ g++ -o Main Main.C A.C
$ ./Main
1
3
2 1 1754
<ch********@gma il.com> schrieb im Newsbeitrag news:11******** *************@v 46g2000cwv.goog legroups.com...
.... int compare(A*, A*); struct Compare : public std::binary_fun ction<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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Egor Bolonev |
last post by:
why functions created with lambda forms cannot contain statements?
how to get unnamed function with statements?
|
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 trying
to accomplish:
The datagrid is sorted using:
dgWIP.DataSource = dvWIP
dvWIP.Sort = Me.SortColumn & " " & Me.SortOrder
dgWIP.DataBind()
|
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),
|
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 recommend group members to be stored first into an array,
into a list or something similar, and then to sort that list.
This approach will work, but it can be significantly slow, if it is
|
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 doorConflictList be keyed on a door
ID and contain a sorted list called conFlictList. I wrote this
expecting the following
doorConflictList should end up with 3 items.
Item 1 would be a sorted list with 1 item
| |
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 ;
|
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 container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly
----------------------
|
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 the first row, such that an array such as
{4,6,3,7,2},{1,2,3,4,5} would be sorted to
{2,3,4,6,7},{5,3,1,2,4}
I have taken the following example code from an FAQ on the internet, but cannot make it work as intended.
|
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 initialize a SortedList all at once from
sorted data?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |