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 4 3451
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.
"Florent Garcin" <fl************ @epfl.ch> wrote in message
news:11******** ****@sicinfo3.e pfl.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
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.
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_atla s_DOT_de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 on what you are using the pair
for. For instance if I am using coordinates in two
dimensional space then I like to use x and y. So
I might as well define my own struct with x and
y members in it and create a constructor so
|
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:
dictionary.insert(std::make_pair<Key, Value>(k,v));
|
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
|
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 first pair equals to seconds of second pair):
bool equals(MyPair src, MyPair dest ) {
if (( src.first == dest.first) && ( src.second == dest.second))
|
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 successful to use a version
which uses pairs as keys.
For instance, I would like to set up the map as follows:
struct eqstr {
bool operator()(pair<int,int> s1, pair<int,int> s2) const{
| |
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 char*,const char*>("123","123"),
pair<const char*,const char*>("1234","1234"),
pair<const char*,const char*>("12345","12345"),
};
|
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 cumbersome to
make a pair are there any better way to do it than the above procedure?
|
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()
|
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
|
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...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |