473,763 Members | 7,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL <map> with two keys ?

I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two key.
Do you have a little example.

Thanks Markus
Jul 19 '05 #1
11 39940
Markus Hämmerli wrote:
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two key.
Obviously not everybody knows, since you don't... ^_^

Seriously though, I don't really understand what you mean by two keys? If
you mean you need two keys to uniquely identify a value, can't you just use
a class with two members as a key?
Do you have a little example.


#include <map>

class Keys {
public:
Keys(int k1, int k2) : key1(k1), key2(k2) { }
bool operator<(const Keys &right) const {
return (key1 < right.key1 && key2 < right.key2);
}
int key1;
int key2;
};

int main() {
std::map<Keys, int> mymap;
mymap.insert(st d::pair<Keys, int>(Keys(3, 8), 5));
return 0;
}

--
Unforgiven

"Most people make generalisations "
Freek de Jonge

Jul 19 '05 #2
Markus Hämmerli wrote:
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two key.
Do you have a little example.


Your question is not very clear, but perhaps std::multimap is what you
are looking for. It is similar to std::map, but allows multiple keys
with the same value. (By 'with the same value' I mean that the keys
compare equal to each other.)

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
Unforgiven wrote:
Markus Hämmerli wrote:
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two key.

Obviously not everybody knows, since you don't... ^_^

Seriously though, I don't really understand what you mean by two keys? If
you mean you need two keys to uniquely identify a value, can't you just use
a class with two members as a key?

Do you have a little example.

#include <map>

class Keys {
public:
Keys(int k1, int k2) : key1(k1), key2(k2) { }
bool operator<(const Keys &right) const {
return (key1 < right.key1 && key2 < right.key2);
}
int key1;
int key2;
};

int main() {
std::map<Keys, int> mymap;
mymap.insert(st d::pair<Keys, int>(Keys(3, 8), 5));
return 0;
}


Couldn't you just use pair for the key?

std::map<pair<i nt,int>, int> mymap;

NR

Jul 19 '05 #4
"Markus Hämmerli" <m.*********@so lnet.ch> wrote in message
news:3f******** *************** @newsspool.soln et.ch...
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two
key. Do you have a little example.


On Boost (www.boost.org), there was a discussion about
a "bimap", which is a two-way map in which the value type
was actually a second key type. The reason you would
want to do this instead of having a std::pair<> with a single
key is that you might want to search on either key
independently. It was suggested that a generalization with
an arbitrary number of keys would be useful, but I don't
think such a beast has shown up anywhere yet.

Dave

Jul 19 '05 #5
David B. Held wrote:
"Markus Hämmerli" <m.*********@so lnet.ch> wrote in message
news:3f******** *************** @newsspool.soln et.ch...
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two
key. Do you have a little example.

On Boost (www.boost.org), there was a discussion about
a "bimap", which is a two-way map in which the value type
was actually a second key type.


Maybe I'm not thinking clearly (it's getting rather late), but it seems
like you could do this with a std::set where you insert the two keys at
the same time, and each with some kind of reference to the other.
Wrapping it in a new class type would make it convenient to use.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:xB******** *******@newsrea d4.news.pas.ear thlink.net...
David B. Held wrote:
[...]
On Boost (www.boost.org), there was a discussion about
a "bimap", which is a two-way map in which the value type
was actually a second key type.


Maybe I'm not thinking clearly (it's getting rather late), but it
seems like you could do this with a std::set where you insert
the two keys at the same time, and each with some kind of
reference to the other.
Wrapping it in a new class type would make it convenient to
use.


You could do that, but it's slightly slower and has other
undesirable properties. For instance, in the bimap, you
can query the map to see if a key does not exist in the
first set. How do you do that with your design? Maintaining
invariants is tricker with your design. Keys have to be
added and removed in pairs. Someone could make a
mistake and create non-paired keys that would corrupt
the map. Or maybe someone could accidentally create
just one key. At any rate, the wrapper would be fairly
elaborate, and it's not obvious to me that it would be any
better (in terms of simplicity or performance) than a custom
designed n-map.

Also, a bimap supports ordered keys, and yours does not
(at least not without a suitably complex definition of
"reference" ). Consider the case where you wish to insert
both (a, b) and (b, c) into the map, but you wish each key
set to be unique. How do you do this with your design?
You can, but it gets more and more complicated. ;)

Dave

Jul 19 '05 #7
>
On Boost (www.boost.org), there was a discussion about
a "bimap", which is a two-way map in which the value type
was actually a second key type.


Do you mean something like this:

http://www.codeproject.com/vcpp/stl/bimap.asp
Jul 19 '05 #8
"Markus Hämmerli" <m.*********@so lnet.ch> wrote in message
news:3f******** *************** @newsspool.soln et.ch...
I have seen in the STL that the map is working with one key.
Does everyboby know if there is a possibility to have two key.
Do you have a little example.

Thanks Markus


How about this:

void dual_map_test()
{
typedef std::map<int, double> t_inner_map;
typedef std::map<std::s tring, t_inner_map> t_dual_map;
t_dual_map amap;
amap["abc"][12] = 4.3;
amap["def"][7] = 2.2;
std::cout << amap["abc"][12] << '\n';
std::cout << amap["def"][7] << '\n';
}

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #9
foo
"David B. Held" <dh***@codelogi cconsulting.com > wrote in message news:<bj******* ***@news.astoun d.net>...
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:xB******** *******@newsrea d4.news.pas.ear thlink.net...
David B. Held wrote:
[...]
On Boost (www.boost.org), there was a discussion about
a "bimap", which is a two-way map in which the value type
was actually a second key type.


Maybe I'm not thinking clearly (it's getting rather late), but it
seems like you could do this with a std::set where you insert
the two keys at the same time, and each with some kind of
reference to the other.
Wrapping it in a new class type would make it convenient to
use.


You could do that, but it's slightly slower and has other
undesirable properties. For instance, in the bimap, you
can query the map to see if a key does not exist in the
first set. How do you do that with your design? Maintaining
invariants is tricker with your design. Keys have to be
added and removed in pairs. Someone could make a
mistake and create non-paired keys that would corrupt
the map. Or maybe someone could accidentally create
just one key. At any rate, the wrapper would be fairly
elaborate, and it's not obvious to me that it would be any
better (in terms of simplicity or performance) than a custom
designed n-map.

Also, a bimap supports ordered keys, and yours does not
(at least not without a suitably complex definition of
"reference" ). Consider the case where you wish to insert
both (a, b) and (b, c) into the map, but you wish each key
set to be unique. How do you do this with your design?
You can, but it gets more and more complicated. ;)

Dave

Looking through boost, I coundn't find any code posted there.
I did however find a codeproject link that had the bimap
implementation.

http://www.codeproject.com/vcpp/stl/bimap.asp

Looks like a very handy class, and I'm surprise it's not in the boost
library yet.

It would be nice if they would consider adding this to the standard.
I've frequently seen users ask about the existence of such a class or
functionality in the std::map class.
Jul 19 '05 #10

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

Similar topics

1
2405
by: john smith | last post by:
Hi, I have a question about map and classes that contain maps. My problem is declaring any class methods const that would do something to the map. Presumably it's because when operator is accessed, the map object can be changed. Is there any way that I can somehow say that I'm not going to change the map object? Also, it becomes a problem when I try to declare the copy constructor like this: class A {
1
4208
by: Florian Liefers | last post by:
"Hello World\n", i have the following problem: One of my headerfiles for a lib is including <vector>. When i compile the lib, everything is done well. In my application another file is including <map>. By linking my application and the lib, following errors occur: error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>
8
1477
by: Pierre Couderc | last post by:
I am looking for a "special" kind of map : - it is read like a map - if the searched element exists, it is given back imediately - if the searched element does not exist, an initialise() is called to do the job. - anyway the map has a limited size, when it is full, the oldest element is dropped ( if recalled later, will need again an initialise()) Is there some STL (or not) to do that?
14
5615
by: laurence | last post by:
I am implementing a comprehensive image-map generator utility, so have been studying W3C HTML 4.01 Specification (http://www.w3.org/TR/html4/struct/objects.html#h-13.6) on image maps (among other things). I note the document specifies that block level content can be included within a <map>. Testing this in order to discover why one might wish to do this, I find the block level content is rendered in page flow order anyway, and not...
3
3505
by: Evgeny | last post by:
Hi, all! I didn't find yet solution for this problem! Somebody knows where is a catch? Looks like "operator =" or copy constructor not implemented in one of internal templates.... Thanks in advance class CMyBase;
13
5053
by: Steve Edwards | last post by:
Hi, Given a map: typedef map<long, string, greater<long> > mapOfFreq; Is there a quicker way to find the rank (i.e. index) of the elememt that has the long value of x? At the moment I'm iterating through the map and keeping count of when I hit it.
5
3107
by: Mike Copeland | last post by:
I'm having difficulty updating map objects. In the code I've excerpted below, I store map objects without difficulty, but my attempts to modify elements of the stored data don't work. struct ChipRecord // Chip Times record { time_t hiStartTime; // Start Time // much more... } timeWork; // entrant info records typedef map<int, ChipRecordBCI;
0
9386
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
10144
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
9997
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...
0
8821
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...
1
7366
isladogs
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...
0
6642
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
5270
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2793
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 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...

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.