220 17661 <558876f0-141f-49ca-bece-7215ec173d1d@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Vlad from Moscow <vlad.moscow@mail.ru>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Overloading the family of minmax algorithms
Date: Fri, 1 May 2015 06:47:56 -0700 (PDT)
Lines: 206
Approved: news@gmane.org
Message-ID: <558876f0-141f-49ca-bece-7215ec173d1d@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1031_1686269933.1430488076203"
X-Trace: ger.gmane.org 1430488087 30998 80.91.229.3 (1 May 2015 13:48:07 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Fri, 1 May 2015 13:48:07 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCXLLRHD7IDRBDMIR2VAKGQE4CN22SQ@isocpp.org Fri May 01 15:48:06 2015
Return-path: <std-proposals+bncBCXLLRHD7IDRBDMIR2VAKGQE4CN22SQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ie0-f198.google.com ([209.85.223.198])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCXLLRHD7IDRBDMIR2VAKGQE4CN22SQ@isocpp.org>)
	id 1YoBIQ-000463-Nj
	for gclcip-std-proposals@m.gmane.org; Fri, 01 May 2015 15:48:02 +0200
Original-Received: by iesa3 with SMTP id a3sf167442046ies.0
        for <gclcip-std-proposals@m.gmane.org>; Fri, 01 May 2015 06:48:01 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:message-id:subject:mime-version
         :content-type:x-original-sender:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=aQu6xEa7kMFdG8KXpf+GxQnHNnHq+KeVFQrVn5JfkoE=;
        b=fWhOd9R+Rn5SmATItQDBSEZ5ZZEj9hKsz2cYwtaSJvOCi4N2cEJWdZtHS0VLANBH6f
         katrz65PGpbNdrCcFlEC4eJd53zFKAHKzD6R4z9JU4H23oqMxEwuAEpOvOXmdE/cxi06
         KNf/SORXo6vcIWyCLYt7lvkTQZrcESCKLaaV7MsKFQ1QOZ6y3cjrM82WOVeUobL9QM2n
         u40R2awamr0x0GrUCIQGjQF/PwJzJyINfSsCFsJxAJTU3f2c47UPGHoQeg2q/ZqerAke
         rl90pBLpNpDteLXiFvKj+GbXymb18eiTE7NKd1gKL+vnX/YKiPHnz3Q4BGIi/ZHSjzEk
         sDOg==
X-Gm-Message-State: ALoCoQltnr8lvq4HuV+mFiLNjvHlPGUk7ztJlm9qa5qxN0KmSQtpSvMBn0OLKTK5Bz3bhYgojv0U
X-Received: by 10.182.55.105 with SMTP id r9mr17587497obp.3.1430488077692;
        Fri, 01 May 2015 06:47:57 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.51.77 with SMTP id t71ls1640783qga.10.gmail; Fri, 01 May
 2015 06:47:56 -0700 (PDT)
X-Received: by 10.140.40.81 with SMTP id w75mr103737qgw.37.1430488076935;
        Fri, 01 May 2015 06:47:56 -0700 (PDT)
X-Original-Sender: vlad.moscow@mail.ru
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:17661
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/17661>

------=_Part_1031_1686269933.1430488076203
Content-Type: multipart/alternative; 
	boundary="----=_Part_1032_797074472.1430488076203"

------=_Part_1032_797074472.1430488076203
Content-Type: text/plain; charset=UTF-8

As it is known for example standard algorithm std::minmax_element returns a 
pair of iterators where the first element of the pair points to the first 
minimum element in the given sequence while the second element of the pair 
points to the last maximum element in the given sequence.

A natural question arises how to apply the algorithm that to find for 
example the first and last minimum elements or the first and last maximum 
elements in the given sequence?

This task othen occurs in practice. 

It can be resolved very simple if to introduce an overloaded algorithm that 
accepts a pair of comparators.

It looks the following way

template <class ForwardIterator, class Compare1, class Compare2>
std::pair<ForwardIterator, ForwardIterator>
minmax_element( ForwardIterator first, 
                ForwardIterator last,
                std::pair<Compare1, Compare2> comp );

In this case to find for example the first and last minimum elements for 
the given sequence it is enough to write

    auto p = ::minmax_element( std::begin( a ), std::end( a ),
                               std::make_pair( std::less<>(), 
std::greater<>() ) );

Or to find for example the first and last maximum elements for the given 
sequence it is enough to write

    auto p = ::minmax_element( std::begin( a ), std::end( a ),
                               std::make_pair( std::greater<>(), 
std::less<>() ) );

If you will run the following demonstrative program (to write the 
implementation of the algorithm is your homework :) )

#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>

template <class ForwardIterator, class Compare1, class Compare2>
std::pair<ForwardIterator, ForwardIterator>
minmax_element( ForwardIterator first, 
                ForwardIterator last,
                std::pair<Compare1, Compare2> comp )
{
    // ...
}
                
int main()
{
    int a[] = { 5, 3, 1, 6, 2, 6, 1 };
    
    auto p = std::minmax_element( std::begin( a ), std::end( a ) );
    
    std::cout << "min: " << std::distance( std::begin( a ), p.first ) << 
std::endl;
    std::cout << "max: " << std::distance( std::begin( a ), p.second ) << 
std::endl;
    
    std::cout << std::endl;
    
    p = ::minmax_element( std::begin( a ), std::end( a ),
                          std::make_pair( std::less<>(), std::greater<>() ) 
);
    std::cout << "min: " << std::distance( std::begin( a ), p.first ) << 
std::endl;
    std::cout << "min: " << std::distance( std::begin( a ), p.second ) << 
std::endl;
    
    std::cout << std::endl;
    
    p = ::minmax_element( std::begin( a ), std::end( a ),
                          std::make_pair( std::greater<>(), std::less<>() ) 
);
    std::cout << "max: " << std::distance( std::begin( a ), p.first ) << 
std::endl;
    std::cout << "max: " << std::distance( std::begin( a ), p.second ) << 
std::endl;
    
}

you get the following output

min: 2
max: 5

min: 2
min: 6

max: 3
max: 5



-- 

--- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_1032_797074472.1430488076203
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>As it is known for example standard algorithm std::mi=
nmax_element returns a pair of iterators where the first element of the pai=
r points to the first minimum element in the given sequence while the secon=
d element of the pair points to the last maximum element in the given seque=
nce.</div><div><br></div><div>A natural question arises how to apply the al=
gorithm that to find for example the first and&nbsp;last minimum elements o=
r the first and last maximum elements in the given sequence?</div><div><br>=
</div><div>This&nbsp;task&nbsp;othen occurs in practice. </div><div><br></d=
iv><div>It can be resolved very simple if to introduce an overloaded&nbsp;a=
lgorithm that accepts a pair of comparators.</div><div><br></div><div>It lo=
oks the following way</div><div><br></div><div>template &lt;class ForwardIt=
erator, class Compare1, class Compare2&gt;<br>std::pair&lt;ForwardIterator,=
 ForwardIterator&gt;<br>minmax_element( ForwardIterator first, <br>&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp; ForwardIterator last,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::pair&lt;Compare1, Com=
pare2&gt; comp );</div><div><br></div><div>In this case to find for example=
 the first and last minimum elements for the given sequence it is enough to=
 write</div><div><br></div><div>&nbsp;&nbsp;&nbsp; auto p =3D ::minmax_elem=
ent( std::begin( a ), std::end( a ),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std:=
:make_pair( std::less&lt;&gt;(), std::greater&lt;&gt;() ) );</div><div><br>=
</div><div>Or to find for example the first and last&nbsp;maximum elements =
for the given sequence it is enough to write</div><div><br>&nbsp;&nbsp;&nbs=
p; auto p =3D ::minmax_element( std::begin( a ), std::end( a ),<br>&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp; std::make_pair( std::greater&lt;&gt;(), std::less&lt=
;&gt;() ) );</div><div><br></div><div>If&nbsp;you will run the following de=
monstrative program (to write the implementation of the algorithm is your h=
omework :) )</div><div><br></div><div>#include &lt;iostream&gt;<br>#include=
 &lt;algorithm&gt;<br>#include &lt;iterator&gt;<br>#include &lt;functional&=
gt;</div><div><br></div><div>template &lt;class ForwardIterator, class Comp=
are1, class Compare2&gt;<br>std::pair&lt;ForwardIterator, ForwardIterator&g=
t;<br>minmax_element( ForwardIterator first, <br>&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ForwardIte=
rator last,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::pair&lt;Compare1, Compare2&gt; comp )<b=
r>{<br>&nbsp;&nbsp;&nbsp;&nbsp;// ...<br>}</div><div>&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </div>=
<div>int main()<br>{<br>&nbsp;&nbsp;&nbsp; int a[] =3D { 5, 3, 1, 6, 2, 6, =
1 };<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; auto p =3D std::minmax_el=
ement( std::begin( a ), std::end( a ) );<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&n=
bsp;&nbsp; std::cout &lt;&lt; "min: " &lt;&lt; std::distance( std::begin( a=
 ), p.first ) &lt;&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; =
"max: " &lt;&lt; std::distance( std::begin( a ), p.second ) &lt;&lt; std::e=
ndl;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; std::e=
ndl;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; p =3D ::minmax_element( s=
td::begin( a ), std::end( a ),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::make_pair( std::less&lt;&gt;(), std=
::greater&lt;&gt;() ) );</div><div>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "m=
in: " &lt;&lt; std::distance( std::begin( a ), p.first ) &lt;&lt; std::endl=
;<br>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "min: " &lt;&lt; std::distance( =
std::begin( a ), p.second ) &lt;&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; <br>&=
nbsp;&nbsp;&nbsp; std::cout &lt;&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; <br>&=
nbsp;&nbsp;&nbsp; p =3D ::minmax_element( std::begin( a ), std::end( a ),<b=
r>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p; std::make_pair( std::greater&lt;&gt;(), std::less&lt;&gt;() ) );</div><d=
iv>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "max: " &lt;&lt; std::distance( st=
d::begin( a ), p.first ) &lt;&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; std::cou=
t &lt;&lt; "max: " &lt;&lt; std::distance( std::begin( a ), p.second ) &lt;=
&lt; std::endl;<br>&nbsp;&nbsp;&nbsp; <br>}<br><br>you get the following ou=
tput</div><div><br></div><div>min: 2</div><div>max: 5</div><div><br></div><=
div>min: 2</div><div>min: 6</div><div><br></div><div>max: 3</div><div>max: =
5<br><br></div><div><br></div><div><br></div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_1032_797074472.1430488076203--
------=_Part_1031_1686269933.1430488076203--

.
