473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

map with pair of key

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
May 10 '06 #1
4 3432
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.
May 10 '06 #2
"Florent Garcin" <fl************@epfl.ch> wrote in message
news:11************@sicinfo3.epfl.ch...
: 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
May 10 '06 #3


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.

May 10 '06 #4
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
May 10 '06 #5

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

Similar topics

14
by: Neil Zanella | last post by:
Hello, I would like to ask how come the design of C++ includes std::pair. First of all I don't think many programmers would use it. For starters, what the first and second members are depends...
3
by: JustSomeGuy | last post by:
in the stl map class I see the use of a function pair and make_pair. What is the difference between pair and make_pair? dictionary.insert(std::pair<Key, Value>(k,v)); works as well as: ...
6
by: pmatos | last post by:
Hi all, Is there a way of (after creating a pair) set its first and second element of not? (pair is definitely a read-only struct)? Cheers, Paulo Matos
1
by: Allerdyce.John | last post by:
I have a vector of Pair of int: typedef pair<int, int> MyPair; typedef vector <MyPair > MyVector I would like to remove entries if their first are equal, or if their value is swap ( first of...
11
by: kietzi | last post by:
Hello world, I was wondering whether it would be possible to create a map which uses a pair of ints as key and a float as value. I have used maps with const char* as key, but have so far not been...
4
by: onkar | last post by:
#include<iostream> using namespace std; int main(void){ const pair<const char*,const char*arr={ pair<const char*,const char*>("1","1"), pair<const char*,const char*>("12","12"), pair<const...
18
by: desktop | last post by:
I have made this little test with std::pair: test<intt1(1); test<intt2(2); std::pair<test<int>,test<int mypair = std::make_pair(t1,t2); where test is a template class I wrote. It seems a bit...
2
by: subramanian100in | last post by:
Consider the following piece of code: #include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> #include <iterator> #include <algorithm> int main()
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.