473,396 Members | 1,933 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,396 software developers and data experts.

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 1377
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...@schwabcenter.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
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...
19
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...
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
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()
1
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>...
18
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...
3
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...
9
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 =...
1
by: subramanian100in | last post by:
consider the program: #include <cstdlib> #include <iostream> #include <utility> using namespace std; class Test {
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.