473,811 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on pair in <utility>

Suppose left and right are two pair<T1, T2objects.

Then
'left < right'
returns
left.first < right.first || !(right.first < left.first) && left.second
< right.second.

Suppose left.first is NOT less than right.first.

Then isn't the subexpression '!(right.first < left.first)' equivalent
to 'left.first == right.first' ?

Is there any reason for mentioning like '!(right.first < left.first)'
instead of
'left.first == right.first' ?

Kindly clarify.

Thanks
V.Subramanian
Feb 4 '08 #1
2 1391
su************* *@yahoo.com, India wrote:
Suppose left and right are two pair<T1, T2objects.

Then
'left < right'
returns
left.first < right.first || !(right.first < left.first) && left.second
< right.second.

Suppose left.first is NOT less than right.first.

Then isn't the subexpression '!(right.first < left.first)' equivalent
to 'left.first == right.first' ?

Is there any reason for mentioning like '!(right.first < left.first)'
instead of
'left.first == right.first' ?
operator< is generally the first operator defined for a given type,
since the standard containers and algorithms default to std::less.
operator==(a, b) is then defined as !(a < b) && !(b < a). Using
operator< in the first place will therefore tend to be a little more
efficient.
Feb 4 '08 #2
James Kanze wrote:
On Feb 4, 5:45 am, Jeff Schwab <j...@schwabcen ter.comwrote:
>subramanian10. ..@yahoo.com, India wrote:
>>Suppose left and right are two pair<T1, T2objects.
>>Then
'left < right'
returns
left.first < right.first || !(right.first < left.first) && left.second
< right.second.
>>Suppose left.first is NOT less than right.first.
>>Then isn't the subexpression '!(right.first < left.first)' equivalent
to 'left.first == right.first' ?

Not if one of the types involved doesn't define an operator==.
>>Is there any reason for mentioning like '!(right.first < left.first)'
instead of
'left.first == right.first' ?
>operator< is generally the first operator defined for a given
type, since the standard containers and algorithms default to
std::less. operator==(a, b) is then defined as !(a < b) &&
!(b < a). Using operator< in the first place will therefore
tend to be a little more efficient.

For many types, an operator== can be implemented at far less
runtime cost than an operator<.
I didn't say it couldn't be, just that it often isn't. operator== can
be defined in terms of operator<, but the reverse is not true; so, if
you need both, you either have to define operator== in terms of
operator<, or you have to implement them both manually and be careful to
keep them in sync. While the (typically) constant-factor speed-up
*might* be worth the increased risk of bugs, I would certainly wait for
a profiler to tell me so.
Feb 5 '08 #3

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

Similar topics

14
45875
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
19
6191
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
6
2756
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
2
7286
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()
1
1840
by: arnuld | last post by:
C++ Primer 4/e says, every pair has 2 members named <firstand <lastbut g++ refuses to accept so. I have just created a pir and trying to print its both members: #include<iostream> #include<string> #include<utility>
18
2513
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the container. I have written both these operators as non-friend functions.
3
1863
by: subramanian100in | last post by:
Given two types T1 and T2, we can create pair<T1, T2p(value1, value2); where value1 and value2 are of types T1 and T2 respectively. Suppose we want to assign a new pair<value to p. We can do it as p = pair<T1, T2>(value3, value4); where value3 and value4 are of types T1 and T2 respectively. The last statement can also be written as
9
3726
by: shaun roe | last post by:
Question about pointer-to-data members I have a deeply nested loop, the innermost bit looks a little like this: for(it(vec.begin(), end(vec.end()), it!=end;++it){ if (global == 0){ myObj = it->func().second->anObj(); } else { myObj = it->func().first->anObj(); }
1
1625
by: subramanian100in | last post by:
consider the program: #include <cstdlib> #include <iostream> #include <utility> using namespace std; class Test {
0
9607
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
10395
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...
1
10408
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,...
0
10137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7673
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
6895
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();...
1
4346
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
2
3874
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3026
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.