Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Complex std::map Key - Possible?

48 views
Skip to first unread message

Mike Copeland

unread,
Oct 30, 2016, 5:45:07 PM10/30/16
to
I wabt to have multiple data value component types in a std::map key.
The code below doesn't compile. My intent is to use a std::map key
of a struct type, so I could access the key's component data values
without deconstructing a scalar data type value. The "key" is made up
of 2 parts (a numeric year and a numeric value).
If I construct a std::string value of these components and use that
data type the compiler accepts it.
It won't allow a "struct" data type for a key value.
Is there a way to do this? TIA
[e.g.]
struct LinkKeyType
{
short nYear;
int nBib;
} linkKey;
struct linkStruct
{
char entCode;
int nAge;
std::string nameStr;
} link;
std::map<LinkKeyType, linkStruct> bibMap;
std::map<LinkKeyType, linkStruct>::iterator lIter;
...
link.entCode = 'M', link.nAge = 45;
link.nameStr = "George Washington";
linkKey.nBib = 123, linkKey.nYear = 88;
bibMap[linkKey] = link;



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Paavo Helde

unread,
Oct 30, 2016, 6:38:06 PM10/30/16
to
Yet again you fail to provide the error messages for "doesn't compile",
so be prepared for "garbage in, garbage out".

Still, the most obvious problem is that your struct does not have a
less-than comparison operator which is needed by std::map in order to
put the values in order. Add:

struct LinkKeyType
{
short nYear;
int nBib;
bool operator<(const LinkKeyType& b) const {
return nYear<b.nYear || (nYear==b.nYear && nBib<b.nBib);
}
} linkKey;

HTH

Mike Copeland

unread,
Oct 30, 2016, 7:46:14 PM10/30/16
to
In article <56udnaegcoTY64vF...@giganews.com>,
myfir...@osa.pri.ee says...
>
> On 30.10.2016 23:44, Mike Copeland wrote:
> > I want to have multiple data value component types in a std::map
It does (help): good answer.
To your first point, the compiler (VS 2013) produced many diagnostic
errors, and it wasn't reasonable to post them (or so I thought). Many
of the errors pointed into compiler runtime code, so I knew I was
violating something fundamental in the use of C++ container usage.
<sigh>

Juha Nieminen

unread,
Oct 31, 2016, 4:21:49 AM10/31/16
to
Mike Copeland <mrc...@cox.net> wrote:
> I wabt to have multiple data value component types in a std::map key.
> The code below doesn't compile. My intent is to use a std::map key
> of a struct type, so I could access the key's component data values
> without deconstructing a scalar data type value. The "key" is made up
> of 2 parts (a numeric year and a numeric value).

Either the key type needs to contain an operator<() (which compares in
a weakly ascending order), or you need to provide std::map with a
comparator that compares two key objects.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Vir Campestris

unread,
Nov 1, 2016, 5:06:15 PM11/1/16
to
On 30/10/2016 23:46, Mike Copeland wrote:
> To your first point, the compiler (VS 2013) produced many diagnostic
> errors, and it wasn't reasonable to post them (or so I thought). Many
> of the errors pointed into compiler runtime code, so I knew I was
> violating something fundamental in the use of C++ container usage.
> <sigh>

Post them anyway. The worst that will happen is that nobody will answer
your post, which is quite likely to happen if you don't post them.

And the many errors? Welcome to template error messages. They can be
_horrible_ - hundreds of characters long.

Andy

Paavo Helde

unread,
Nov 1, 2016, 5:51:42 PM11/1/16
to
On 1.11.2016 23:06, Vir Campestris wrote:
>
> Welcome to template error messages. They can be
> _horrible_ - hundreds of characters long.

You meant hundreds of lines.

0 new messages