473,791 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2639
"Steve Edwards" <gf*@lineone.ne t> schrieb im Newsbeitrag news:gf******** *************** @news.btinterne t.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<(SymTr ipleCount 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<SymTri ple, int> instead of std::set<SymTri pleCount>. 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************ **********@news read4.arcor-online.net>,
"Heinz Ozwirk" <ho**********@a rcor.de> wrote:
"Steve Edwards" <gf*@lineone.ne t> schrieb im Newsbeitrag
news:gf******** *************** @news.btinterne t.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<(SymTr ipleCount 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<SymTri ple, int> instead of
std::set<SymTri pleCount>. 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.ne t> schrieb im Newsbeitrag news:gf******** *************** @news.btinterne t.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.ne t> schrieb im Newsbeitrag news:gf******** *************** @news.btinterne t.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**********@a rcor.de> wrote: But I would prefer using std::map<SymTri ple, int> instead of std::set<SymTri pleCount>. 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<S ymTriple>, 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.11191420 032006
@news.btinterne t.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
780
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
1892
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 physical memory located on a piece of custom hardware - this memory is only accessible using machine specific i/o so I want to hide all this in a class. I'm imagining
2
5142
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 maintainability, I've come up with creating nested structs that contain the sets of attributes manipulated by one API call - so currently the way to access actual attributes is something like 'theObject.Details.Name' or...
1
2150
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 not consistent: (0x0c0001a5). Display.obj : error LNK2022: metadata operation failed (80131195) : Custom attributes are not consistent: (0x0c000108). The help for LNK2022 is completely useless:
6
2953
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 will employ a software "Plug-In" architecture. Taking the plug-in route will give us a design that can adapt to, as yet, undefined future requirements (within the scope of the plug-in interface spec of course). In the past I've done this with...
12
2216
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 projects (dll's) in it. Now I have to design a Web-Client modul (actually the web-interface). To maintain the modularity, I thought of making one ASP.NET-Project inside of the solution and create some projects containing Custom Web Controls. Did...
10
2656
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 namespace file so I can import them in the webpage. Can someone clarify? Thanks.
4
1944
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 ? what are advantages and disadvantages.. please let me know Kishor
1
1875
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 with this statement: import MacToolbar Here is the relevant portion of the class:
0
9669
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, 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...
0
9515
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,...
0
10427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
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 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...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
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();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.