bijective container

R

rippley.scott

Hi,

I am looking for a container mapping a key value1 to another key
value2. I want to use value1 to lookup value2 in the container. But I
also want to use value2 to find value1 in the container. A
(simplified) example: I have pairs of return codes (int) and return
messages (string). Having the return code I want to find the
respective message. But (and I really need this) sometimes I have the
return message and want to find the respective return code.

I imagined using some kind of bijective container for that, but I did
not find one. I am surprised of this, because I thought this might be
a standard STL container. Did I just oversee it or is there realy no
bijective container in the STL? If not in STL, does someone know any
library providing such a container?

Thx

Alex
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi,

I am looking for a container mapping a key value1 to another key
value2. I want to use value1 to lookup value2 in the container. But I
also want to use value2 to find value1 in the container. A
(simplified) example: I have pairs of return codes (int) and return
messages (string). Having the return code I want to find the
respective message. But (and I really need this) sometimes I have the
return message and want to find the respective return code.

I imagined using some kind of bijective container for that, but I did
not find one. I am surprised of this, because I thought this might be
a standard STL container. Did I just oversee it or is there realy no
bijective container in the STL? If not in STL, does someone know any
library providing such a container?

Don't know of any such container but you can easily build one using
two std::map.
 
A

aiooua

I am looking for a container mapping a key value1 to another key
value2. I want to use value1 to lookup value2 in the container. But I
also want to use value2 to find value1 in the container. A
(simplified) example: I have pairs of return codes (int) and return
messages (string). Having the return code I want to find the
respective message. But (and I really need this) sometimes I have the
return message and want to find the respective return code.

your problem involves two types of lookups, so you can keep two
associate containers. if this is a recurring problem in your domain
then you can consider writing a wrapper template on top of these. say,
something like -

--
/* uncompiled example.*/
template class bijective<T1,T2>{
map<T1,T2> t1_to_t2;
map<T2,T1> t2_to_t1;
void insert(T1 &t1, T2 &t2){
t1_to_t2[t1]=t2;
t2_to_t1[t2]=t1;
}
}
--
I imagined using some kind of bijective container for that, but I did
not find one. I am surprised of this, because I thought this might be
a standard STL container.

i think, a bijective container is omitted for the reasons:
- it's not elementary, in a sense that it can be composed out of the
existing containers (as shown above).
- specialized data-structures that support two-way lookups are not
used often to justify their place in the 'standard' library.

i'd point you to section 16.1 of "The C++ Programming Language, 3rd
Edition" by Bjarne Stroustrup for more on "What ought to be in the
standard library?"

thanks,
 
P

Pavel Vozenilek

I am looking for a container mapping a key value1 to another key
value2. I want to use value1 to lookup value2 in the container. But I
also want to use value2 to find value1 in the container. A
(simplified) example: I have pairs of return codes (int) and return
messages (string). Having the return code I want to find the
respective message. But (and I really need this) sometimes I have the
return message and want to find the respective return code.

I imagined using some kind of bijective container for that, but I did
not find one. I am surprised of this, because I thought this might be
a standard STL container. Did I just oversee it or is there realy no
bijective container in the STL? If not in STL, does someone know any
library providing such a container?

Boost.Bimap

http://cablemodem.fibertel.com.ar/mcape/oss/projects/mc_projects/boost_projects/boost_bimap.html

Recently accepted into Boost, will be released
probably in Boost 1.35.

/Pavel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top