473,396 Members | 1,784 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,396 software developers and data experts.

Difficulty designing a map with custom structs.

Hi,

With much help from this forum I've been using stl containers, but
always with other common - or stl - types as members. I've now run in to
a problem while trying to use some of my own structures:

enum MySym{symA, symB, symC, symD ... etc.}

typedef struct{
MySym s1, s2, s3;
}SymTriple;
I have a large (10k) collection of SymTriples, and many of them are
equal (i.e. have same values for s1, s2, s3).
What I'd like to do is have a smaller collection without duplicates, and
a count of how many times a particular one occurs.

Maybe...

typedef struct{
SymTriple st;
long numEntries;
}SymTripleCount;

I've tried various permutations of maps/multimaps, by using the
SymTriple as a key and keeping count of when that key already exists.
The problem is I always get this error:
error: no match for 'operator<' in '__x < __y'
I'm guessing this means that the stl container can not compare or sort
my structs?

Is there any way to store my structs in an stl container, with no
duplicates, but with a count the number of occurences of each matching
pattern?

Thanks

Steve

( I could do this in regular C by looping through every 10k structs and
then testing against every (0 - x,000) unique structs I've already
collected, but this is impossibly slow.)
Mar 20 '06 #1
5 2613
"Steve Edwards" <gf*@lineone.net> schrieb im Newsbeitrag news:gf***********************@news.btinternet.com ...
Hi,

With much help from this forum I've been using stl containers, but
always with other common - or stl - types as members. I've now run in to
a problem while trying to use some of my own structures:

enum MySym{symA, symB, symC, symD ... etc.}

typedef struct{
MySym s1, s2, s3;
}SymTriple;


I have a large (10k) collection of SymTriples, and many of them are
equal (i.e. have same values for s1, s2, s3).
What I'd like to do is have a smaller collection without duplicates, and
a count of how many times a particular one occurs.

Maybe...

typedef struct{
SymTriple st;
long numEntries;
}SymTripleCount;

I've tried various permutations of maps/multimaps, by using the
SymTriple as a key and keeping count of when that key already exists.
The problem is I always get this error:
error: no match for 'operator<' in '__x < __y'


The compiler cannot guess when one of your user defined objects is less than another. You have to help and define one. The easiest way to do so is implementing it like

bool operator<(SymTripleCount const& lhs, SymTripleCount const& rhs)
{
return lhs.s1 < rhs.s1
|| lhs.s1 == rhs.s1 && lhs.s2 < rhs.s2
|| lhs.s1 == rhs.s1 && lhs.s2 == rhs.s2 && lhs.s3 < rhs.s3;
}

But I would prefer using std::map<SymTriple, int> instead of std::set<SymTripleCount>. Using a set, you cannot modify the count later, but using a map, you can. Of cause, with a map you have to define operator< for SymTriple, not for SymTripleCount, but that shouldn't be too hard.

HTH
Heinz
}
Mar 20 '06 #2
In article <44**********************@newsread4.arcor-online.net>,
"Heinz Ozwirk" <ho**********@arcor.de> wrote:
"Steve Edwards" <gf*@lineone.net> schrieb im Newsbeitrag
news:gf***********************@news.btinternet.com ...
Hi,

With much help from this forum I've been using stl containers, but
always with other common - or stl - types as members. I've now run in to
a problem while trying to use some of my own structures:

enum MySym{symA, symB, symC, symD ... etc.}

typedef struct{
MySym s1, s2, s3;
}SymTriple;
I have a large (10k) collection of SymTriples, and many of them are
equal (i.e. have same values for s1, s2, s3).
What I'd like to do is have a smaller collection without duplicates, and
a count of how many times a particular one occurs.

Maybe...

typedef struct{
SymTriple st;
long numEntries;
}SymTripleCount;

I've tried various permutations of maps/multimaps, by using the
SymTriple as a key and keeping count of when that key already exists.
The problem is I always get this error:
error: no match for 'operator<' in ' x < y'


The compiler cannot guess when one of your user defined objects is less than
another. You have to help and define one. The easiest way to do so is
implementing it like

bool operator<(SymTripleCount const& lhs, SymTripleCount const& rhs)
{
return lhs.s1 < rhs.s1
|| lhs.s1 == rhs.s1 && lhs.s2 < rhs.s2
|| lhs.s1 == rhs.s1 && lhs.s2 == rhs.s2 && lhs.s3 < rhs.s3;
}

But I would prefer using std::map<SymTriple, int> instead of
std::set<SymTripleCount>. Using a set, you cannot modify the count later, but
using a map, you can. Of cause, with a map you have to define operator< for
SymTriple, not for SymTripleCount, but that shouldn't be too hard.

HTH
Heinz
}


Thanks, Heinz, I can see how that works now. But how does the compiler
know to use my new operator< ? How is the association made?

Steve
Mar 20 '06 #3
"Steve Edwards" <gf*@lineone.net> schrieb im Newsbeitrag news:gf***********************@news.btinternet.com ...
Thanks, Heinz, I can see how that works now. But how does the compiler
know to use my new operator< ? How is the association made?


The compiler has to use some operator<. That's how std::set and std::map have been implemented. It has to use one, so it takes the one that matches its needs best. Without your own definition of operator<, there was no suitable one at all (otherwise the compiler hadn't complained). So yours will be the only one it can use. Read about function overloading in a book of your choice or google for it.

HTH
Heinz
Mar 20 '06 #4
> "Steve Edwards" <gf*@lineone.net> schrieb im Newsbeitrag news:gf***********************@news.btinternet.com ...
Hi,

With much help from this forum I've been using stl containers, but
always with other common - or stl - types as members. I've now run in to
a problem while trying to use some of my own structures:

enum MySym{symA, symB, symC, symD ... etc.}

typedef struct{
MySym s1, s2, s3;
}SymTriple;
I have a large (10k) collection of SymTriples, and many of them are
equal (i.e. have same values for s1, s2, s3).
What I'd like to do is have a smaller collection without duplicates, and
a count of how many times a particular one occurs.

Maybe...

typedef struct{
SymTriple st;
long numEntries;
}SymTripleCount;

Heinz Ozwirk <ho**********@arcor.de> wrote: But I would prefer using std::map<SymTriple, int> instead of std::set<SymTripleCount>. Using a set, you cannot modify the count later, but using a map, you can. Of cause, with a map you have to define operator< for SymTriple, not for SymTripleCount, but that shouldn't be too hard.


I would suggest using a std::multiset<SymTriple>, and you can use the
count() member function to keep track of the duplicates. You still need
to define operator< for your SymTriples though.

--
Marcus Kwok
Mar 20 '06 #5
In article <gfx-9B0D68.11191420032006
@news.btinternet.com>, gf*@lineone.net says...

[ ... ]
Thanks, Heinz, I can see how that works now. But how does the compiler
know to use my new operator< ? How is the association made?


The container uses whatever comparison function has been
specified for it. If you don't specify something else, it
uses std::less<T>. std::less<T> looks something like
this:

template <class T>
bool less(T const &a, T const &b) {
return a < b;
}

IOW, it just ends up attempting to use the '<' operator
to compare the objects. If you're storing built-in types
for which '<' is pre-defined, it uses the built-in
version. If you're storing some user-defined type, you
have to define the operator yourself. The alternative is
to specify a functor (or function) of your own it should
use to do the comparison. This tend to be most useful
when you have a type that you want to look at in
different orders (e.g. ordering people alphabetically by
last name or by height, depending...)

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 21 '06 #6

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

Similar topics

0
by: Plinkerton | last post by:
I'm designing a custom Visual Studio .NET Add-In. I want it to call a Web Service. Visual Studio is COM based. Web References aren't offered. ....Does anyone know what my options are?
12
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of...
2
by: Thomas Themel | last post by:
Hi, I'm currently trying to model a few complex data objects in C#. These have a huge number of attributes which are manipulated in large blocks by comparatively few API calls. For...
1
by: Bret Pehrson | last post by:
I've converted a non-trivial C++ library to managed, and get the following unhelpful linker error: Assignment.obj : error LNK2022: metadata operation failed (80131195) : Custom attributes are...
6
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that...
12
by: Don Huan | last post by:
Hi my job is to migrate our WinForms application to ASP.NET. This app was build very modular so every peace of code can be replaced by another "modul". There are 1 VS-solution with about 60...
10
by: cykill | last post by:
Hi. I'm new to asp.net and I've been searching on the net and in book on how to use custom namespace. I've created my own custom namespace and I just need a straight answer on where to put the...
4
by: =?Utf-8?B?a2lzaG9y?= | last post by:
Hi, has any one used webservices for returning custom objects other than datasets like custom classes and their internal classes ?. What problems you have faced if any ? is there any limitation...
1
by: Kevin Walzer | last post by:
I'm trying to create a custom Tkinter widget class, and I'm having some difficulty getting it set up properly. The class is called MacToolbar, saved in its own module MacToolbar.py, and imported...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.