Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > map with pair of key

Reply
Thread Tools

map with pair of key

 
 
Ian Collins
Guest
Posts: n/a
 
      05-10-2006
Florent Garcin wrote:
> Hello!
>
> I would like to use the map structure with a key of Pair<string, string>
> and an int as the value.
>

Didn't this come up a few days ago?

> Pair is defined as:
>
> template <class T1, class T2>
> class Pair
> {
> public:
> T1 first;
> T2 second;
>
> Pair()
> {
> }
>
> Pair(T1 first, T2 second)
> {
> this->first = first;
> this->second = second;

Use initialiser lists.
> }
>
> virtual ~Pair()
> {
> }
>
> bool operator == (const Pair<T1, T2> p) const


Use const Pair&
> {
> return (first == p.first && second == p.second) || (first == p.second
> && second == p.first);
> }
>
> bool operator < (const Pair<T1, T2> p) const


Use const Pair&
> {
> return !(*this == p) && (first < p.first || (!(p.first < first) &&
> second < p.second));
> }
>
> };
>
> That means the two pairs of, for instance, strings ("A", "B") and ("B",
> "A") are equals.
>

Where are your tests for the above assertion? Add some and you should
find your problem.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      05-10-2006
"Florent Garcin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
: I would like to use the map structure with a key of Pair<string, string>
: and an int as the value.
:
: Pair is defined as:
....
: bool operator < (const Pair<T1, T2> p) const
: {
: return !(*this == p) && (first < p.first || (!(p.first < first) &&
: second < p.second));
: }
....
: That means the two pairs of, for instance, strings ("A", "B") and ("B",
: "A") are equals.

Well, this is not what your operator < implies: as it looks through
map elements using a bunary search based on op<, the functions of std::map
will be taken away from the entry being looked for.

But does it make sense for your pair class to accept separate types for
its two elements if the elements are supposed to be interchangeable ??
I don't think so.

: Unfortunately, when I run the following code, I have a strange
behaviour.
....
: Why is it not as expected? Does anyone know what I'm doing wrong?

When std::map does not behave as expected, always check your ordering
function.
If you want the order of the pair's element no not matter,
try something like:
friend bool operator < ( .... a, .... b )
{
T const* a1 = &a.first;
T const* a2 = &a.second;
if( *a2<*a1 ) swap( a1, a2 );
T const* b1 = &b.first;
T const* b2 = &b.second;
if( *b2<*b1 ) swap( b1, b2 );
return (*a1<*b1)||(!(*b1<*a1)&&(*a2<*b2);
}
Or possibly better & easier: keep the two elements of your
pair class ordered at all times ( e.g. swap first & second
if the latter is smaller ).



Amicalement --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Stefan_N=E4we?=
Guest
Posts: n/a
 
      05-10-2006
John schrieb:
>
>>> Pair is defined as:
>>>
>>> template <class T1, class T2>
>>> class Pair
>>> {
>>> public:
>>> T1 first;
>>> T2 second;
>>>
>>> Pair()
>>> {
>>> }
>>>
>>> Pair(T1 first, T2 second)
>>> {
>>> this->first = first;
>>> this->second = second;

>> Use initialiser lists.

>
> what do you mean?
>


Pair(T1 first_, T2 second_)
: first(first_), second(second_)
{
}


/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
 
Reply With Quote
 
Florent Garcin
Guest
Posts: n/a
 
      05-10-2006
Hello!

I would like to use the map structure with a key of Pair<string, string>
and an int as the value.

Pair is defined as:

template <class T1, class T2>
class Pair
{
public:
T1 first;
T2 second;

Pair()
{
}

Pair(T1 first, T2 second)
{
this->first = first;
this->second = second;
}

virtual ~Pair()
{
}

bool operator == (const Pair<T1, T2> p) const
{
return (first == p.first && second == p.second) || (first == p.second
&& second == p.first);
}

bool operator < (const Pair<T1, T2> p) const
{
return !(*this == p) && (first < p.first || (!(p.first < first) &&
second < p.second));
}

};

That means the two pairs of, for instance, strings ("A", "B") and ("B",
"A") are equals.

Unfortunately, when I run the following code, I have a strange behaviour.

map<const Pair<string, string>, int> myMap;

bonds[Pair<string, string>("A", "B")] = 1;
bonds[Pair<string, string>("A", "C")] = 2;
bonds[Pair<string, string>("A", "D")] = 3;
bonds[Pair<string, string>("A", "E")] = 4;
bonds[Pair<string, string>("A", "F")] = 5;

cout << myMap[Pair<string, string>("A", "B")] << "\n";
cout << myMap[Pair<string, string>("B", "A")] << "\n";

cout << myMap[Pair<string, string>("A", "C")] << "\n";
cout << myMap[Pair<string, string>("C", "A")] << "\n";

cout << myMap[Pair<string, string>("A", "D")] << "\n";
cout << myMap[Pair<string, string>("D", "A")] << "\n";

cout << myMap[Pair<string, string>("A", "E")] << "\n";
cout << myMap[Pair<string, string>("E", "A")] << "\n";

cout << myMap[Pair<string, string>("A", "F")] << "\n";
cout << myMap[Pair<string, string>("F", "A")] << "\n";

The outputs are:

1
0
2
2
3
0
4
4
5
0

It should not contain 0! but I should have twice the same number like:
1
1
2
2
3
3
4
4
5
5

Why is it not as expected? Does anyone know what I'm doing wrong?

Thank you in advance,

John
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      05-10-2006


Ian Collins wrote:
> Florent Garcin wrote:
>
>>Hello!
>>
>>I would like to use the map structure with a key of Pair<string, string>
>>and an int as the value.
>>

>
> Didn't this come up a few days ago?


yeah, you are right!

>
>
>>Pair is defined as:
>>
>>template <class T1, class T2>
>>class Pair
>>{
>>public:
>> T1 first;
>> T2 second;
>>
>> Pair()
>> {
>> }
>>
>> Pair(T1 first, T2 second)
>> {
>> this->first = first;
>> this->second = second;

>
> Use initialiser lists.


what do you mean?

>
>> }
>>
>> virtual ~Pair()
>> {
>> }
>>
>> bool operator == (const Pair<T1, T2> p) const

>
>
> Use const Pair&
>
>> {
>> return (first == p.first && second == p.second) || (first == p.second
>>&& second == p.first);
>> }
>>
>> bool operator < (const Pair<T1, T2> p) const

>
>
> Use const Pair&
>
>> {
>> return !(*this == p) && (first < p.first || (!(p.first < first) &&
>>second < p.second));
>> }
>>
>>};
>>
>>That means the two pairs of, for instance, strings ("A", "B") and ("B",
>>"A") are equals.
>>

>
> Where are your tests for the above assertion? Add some and you should
> find your problem.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stl map: get the <key,value> pair which has the minimum value Rui Maciel C++ 2 12-01-2009 11:21 PM
map with pair as key kietzi@web.de C++ 11 06-21-2006 07:14 AM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
map.insert(key,val) vs. map[key]=val ? Patrick Guio C++ 6 10-20-2004 01:54 PM



Advertisments