220 17073 <3cd99ed6-5459-474c-881e-55923429770f@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: Re: Definition of std::is_sorted for iterators of the
 input iterator category
Date: Wed, 18 Mar 2015 11:22:05 -0700 (PDT)
Lines: 240
Approved: news@gmane.org
Message-ID: <3cd99ed6-5459-474c-881e-55923429770f@isocpp.org>
References: <29db7c40-6e09-42dd-8133-1ecb5723f005@isocpp.org>
 <23ACE1F6-A65D-4C22-A745-E57526ABBA0A@gmail.com> <4eae9fd4-6efd-4915-946d-0b6200401e66@isocpp.org>
 <CAGg_6+PwsD624-OBqVBPX208CtRef7nx=scRhpUFMFhR33koLg@mail.gmail.com> <921e1a2b-94e3-49e8-8e04-6d745dbe66d5@isocpp.org>
 <CAGg_6+OrfGgCq+j8RiXGcbmj9nzeO-UQExZRq8PFR2Jw174w_A@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_6507_1423946482.1426702925760"
X-Trace: ger.gmane.org 1426702931 5815 80.91.229.3 (18 Mar 2015 18:22:11 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 18 Mar 2015 18:22:11 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCXLLRHD7IDRBTUEU6UAKGQEZ5HBIMI@isocpp.org Wed Mar 18 19:22:11 2015
Return-path: <std-proposals+bncBCXLLRHD7IDRBTUEU6UAKGQEZ5HBIMI@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+bncBCXLLRHD7IDRBTUEU6UAKGQEZ5HBIMI@isocpp.org>)
	id 1YYIbb-0000Oy-46
	for gclcip-std-proposals@m.gmane.org; Wed, 18 Mar 2015 19:22:11 +0100
Original-Received: by iedm5 with SMTP id m5sf36147330ied.1
        for <gclcip-std-proposals@m.gmane.org>; Wed, 18 Mar 2015 11:22:10 -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:in-reply-to:references
         :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=w27I+3kXh4/4DTeXXuqUMrI0wKY3jGdnDvataW8yajU=;
        b=JRt5Ia3JJqUVeBuGr2i2ffdwfKl/La/lfO41/lkH/BlFKGHz2IHZPmOZiMAZLRPqyT
         gE9f5IiFk9LveWSz5sosB/xKH4oKARqYfpz08wN8N2tL8KDfqSIXUKS7ukTA39Ymi8VL
         2YxpUFaQAiJyaIlmLK08BWnUBlAO4Vv027TYA5g+bqDaLc20Z4urMuhXOns+MhIBYlhG
         KtF00a84XM/i3WA7S+fHPhQ2WK1160MkI4nuKS9CcMZTZL8dvf9CYhd4EhFjR7+fW8b4
         7kERXzB0xc8W5XwsClNbPkg64+q5gIunbnF9CvfIPofi+63KgWPTEkjN0V4n2lW0tExk
         NgAQ==
X-Gm-Message-State: ALoCoQl1bs+4OaeS64sxbQ8bhmWoiPKUzIwIEmpUKXfsxzv1BTB1bhtu624dUtdFw/OIdaA9mI8C
X-Received: by 10.182.125.100 with SMTP id mp4mr38768457obb.21.1426702930029;
        Wed, 18 Mar 2015 11:22:10 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.85.148 with SMTP id n20ls766962qgd.23.gmail; Wed, 18 Mar
 2015 11:22:06 -0700 (PDT)
X-Received: by 10.140.36.134 with SMTP id p6mr1116361qgp.26.1426702926558;
        Wed, 18 Mar 2015 11:22:06 -0700 (PDT)
In-Reply-To: <CAGg_6+OrfGgCq+j8RiXGcbmj9nzeO-UQExZRq8PFR2Jw174w_A@mail.gmail.com>
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:17073
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/17073>

------=_Part_6507_1423946482.1426702925760
Content-Type: multipart/alternative; 
	boundary="----=_Part_6508_710760806.1426702925760"

------=_Part_6508_710760806.1426702925760
Content-Type: text/plain; charset=UTF-8

Specially for you I prepared a simple and at the same time elegant example

Here you are

#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <vector>

template <class InputIterator>
bool is_sorted( InputIterator first, InputIterator last )
{
 if ( first != last )
 {
  typename std::iterator_traits<InputIterator>::value_type prev( *first );
  
  while ( ++first != last && !( *first < prev ) ) prev = *first;
 }
 
 return first == last;
}

int main() 
{
 std::vector<std::string> sorted;
 std::vector<std::string> non_sorted;
 
 std::string record;
 while ( std::getline( std::cin, record ) && !record.empty() )
 {
  std::istringstream is( record );

  if ( is_sorted( std::istream_iterator<int>( is ),
                  std::istream_iterator<int>() ) )
  {
   sorted.push_back( record );
  }                 
  else
  {
   non_sorted.push_back( record );
  }
 }
 
 std::cout << "Sorted" << std::endl;
 for ( const auto &s : sorted ) std::cout << s << std::endl;
 
 std::cout << "\nNon-sorted" << std::endl;
 for ( const auto &s : non_sorted ) std::cout << s << std::endl;
 
 return 0;
}

If to enter for example the following sequences

1 2 3 4 5
1 2 2 2 2
1 2 3 5 4
2 1 1 1 1

then the program output will be

Sorted
1 2 3 4 5
1 2 2 2 2

Non-sorted
1 2 3 5 4
2 1 1 1 1

As you see the code is very clear.


On Wednesday, March 18, 2015 at 7:34:38 PM UTC+3, Nevin ":-)" Liber wrote:

> On 18 March 2015 at 10:38, Vlad from Moscow <vlad....@mail.ru 
> <javascript:>> wrote:
>
>> Suprising behaviour has algorithm std::copy_n. 
>>
>
> What surprising behavior is that?
>  
>
>> As for performance difference for input iterators and forward iterators 
>> then there is no any performance difference because the areas of 
>> application of the algorithm  with input iterators and forward iterators 
>> are ddifferent.
>>
>
> Sure they are, if one requires copying and the other doesn't.  Yes, there 
> is no algorithmic complexity difference, but there most certainly can be a 
> practical difference, especially types that have nontrivial copy semantics.
>  
>
>> It would be correct to compare the performance of the example I showed 
>> with using the algorithm and the approach you are going to apply without 
>> using the algorithm. Can you show your approach? We will compare the 
>> performance for example for a string that contains 10000 integers.
>>
>
> I don't see it as a terribly useful addition outside of that contrived 
> example.  After all, one calls is_sorted presumably to do something based 
> on what it returns, which will typically means one already has a container 
> of objects so that one can do multiple passes over the data, and that 
> container has better than just input iterators.
>
> How, for instance, would you go and sort the string of numbers if 
> is_sorted returns false?
>  
>
>> I am sure it is a good proposal.
>>
>
> I am sure you are sure it is a good proposal.  Best of luck writing it up 
> and coming to Lenexa to present it.
> -- 
>  Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com <javascript:>>  (847) 
> 691-1404
>  

-- 

--- 
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_6508_710760806.1426702925760
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Specially for you I prepared a simple and at the same=
 time elegant example</div><div><br></div><div>Here you are</div><div><br><=
/div><div>#include &lt;iostream&gt;<br>#include &lt;iterator&gt;<br>#includ=
e &lt;string&gt;<br>#include &lt;sstream&gt;<br>#include &lt;vector&gt;</di=
v><div><br></div><div>template &lt;class InputIterator&gt;<br>bool is_sorte=
d( InputIterator first, InputIterator last )<br>{<br>&nbsp;if ( first !=3D =
last )<br>&nbsp;{<br>&nbsp;&nbsp;typename std::iterator_traits&lt;InputIter=
ator&gt;::value_type prev( *first );<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;while (=
 ++first !=3D last &amp;&amp; !( *first &lt; prev ) ) prev =3D *first;<br>&=
nbsp;}<br>&nbsp;<br>&nbsp;return first =3D=3D last;<br>}</div><div><br></di=
v><div>int main() <br>{<br>&nbsp;std::vector&lt;std::string&gt; sorted;<br>=
&nbsp;std::vector&lt;std::string&gt; non_sorted;<br>&nbsp;<br>&nbsp;std::st=
ring record;<br>&nbsp;while ( std::getline( std::cin, record ) &amp;&amp; !=
record.empty() )<br>&nbsp;{<br>&nbsp;&nbsp;std::istringstream is( record );=
</div><div><br>&nbsp;&nbsp;if ( is_sorted( std::istream_iterator&lt;int&gt;=
( is ),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::istream_iterator&lt;int&gt;() )=
 )<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;sorted.push_back( record );<br>&nb=
sp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;{<br>&n=
bsp;&nbsp;&nbsp;non_sorted.push_back( record );<br>&nbsp;&nbsp;}<br>&nbsp;}=
<br>&nbsp;<br>&nbsp;std::cout &lt;&lt; "Sorted" &lt;&lt; std::endl;<br>&nbs=
p;for ( const auto &amp;s : sorted ) std::cout &lt;&lt; s &lt;&lt; std::end=
l;</div><div>&nbsp;</div><div>&nbsp;std::cout &lt;&lt; "\nNon-sorted" &lt;&=
lt; std::endl;<br>&nbsp;for ( const auto &amp;s : non_sorted ) std::cout &l=
t;&lt; s &lt;&lt; std::endl;<br>&nbsp;<br>&nbsp;return 0;<br>}<br></div><di=
v><br></div><div>If to enter for example the following sequences</div><div>=
<br></div><div>1 2 3 4 5<br>1 2 2 2 2<br>1 2 3 5 4<br>2 1 1 1 1</div><div><=
br></div><div>then the program output will be</div><div><br></div><div>Sort=
ed</div><div>1 2 3 4 5<br>1 2 2 2 2<br><br>Non-sorted<br>1 2 3 5 4<br>2 1 1=
 1 1<br></div><div><br></div><div>As you see the code is very clear.<br></d=
iv><div><br></div><div><br>On Wednesday, March 18, 2015 at 7:34:38 PM UTC+3=
, Nevin ":-)" Liber wrote:</div><blockquote class=3D"gmail_quote" style=3D"=
margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left-color: rgb(204, 2=
04, 204); border-left-width: 1px; border-left-style: solid;"><div dir=3D"lt=
r">On 18 March 2015 at 10:38, Vlad from Moscow <span dir=3D"ltr">&lt;<a onm=
ousedown=3D"this.href=3D'javascript:';return true;" onclick=3D"this.href=3D=
'javascript:';return true;" href=3D"javascript:" target=3D"_blank" rel=3D"n=
ofollow" gdf-obfuscated-mailto=3D"qj1BooutI2kJ">vlad....@mail.ru</a>&gt;</s=
pan> wrote:<br><div><div class=3D"gmail_quote"><blockquote class=3D"gmail_q=
uote" style=3D"margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left-co=
lor: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid;"=
><div dir=3D"ltr"><div>Suprising behaviour has algorithm std::copy_n. </div=
></div></blockquote><div><br></div><div>What surprising behavior is that?</=
div><div>&nbsp;</div><blockquote class=3D"gmail_quote" style=3D"margin: 0px=
 0px 0px 0.8ex; padding-left: 1ex; border-left-color: rgb(204, 204, 204); b=
order-left-width: 1px; border-left-style: solid;"><div dir=3D"ltr"><div>As =
for performance difference for input iterators and forward iterators then t=
here is no any performance difference because the areas&nbsp;of application=
 of the algorithm &nbsp;with input iterators and forward iterators are ddif=
ferent.</div></div></blockquote><div><br></div><div>Sure they are, if one r=
equires copying and the other doesn't.&nbsp; Yes, there is no algorithmic c=
omplexity difference, but there most certainly can be a practical differenc=
e, especially types that have nontrivial copy semantics.</div><div>&nbsp;</=
div><blockquote class=3D"gmail_quote" style=3D"margin: 0px 0px 0px 0.8ex; p=
adding-left: 1ex; border-left-color: rgb(204, 204, 204); border-left-width:=
 1px; border-left-style: solid;"><div dir=3D"ltr"><div> It would be correct=
 to compare the performance of the example I showed with using the algorith=
m and the approach you are going to apply without using the algorithm. Can =
you show your approach? We will compare the performance for example for a s=
tring that contains 10000 integers.<br></div></div></blockquote><div><br></=
div><div>I don't see it as a terribly useful addition outside of that contr=
ived example.&nbsp; After all, one calls is_sorted presumably to do somethi=
ng based on what it returns, which will typically means one already has a c=
ontainer of objects so that one can do multiple passes over the data, and t=
hat container has better than just input iterators.</div><div><br></div><di=
v>How, for instance, would you go and sort the string of numbers if is_sort=
ed returns false?</div><div>&nbsp;</div><blockquote class=3D"gmail_quote" s=
tyle=3D"margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left-color: rg=
b(204, 204, 204); border-left-width: 1px; border-left-style: solid;"><div d=
ir=3D"ltr"><div></div><div>I am sure it is a good proposal.<br></div></div>=
</blockquote><div><br></div><div>I am sure you are sure it is a good propos=
al.&nbsp; Best of luck writing it up and coming to Lenexa to present it.</d=
iv></div>-- <br><div>&nbsp;Nevin ":-)" Liber&nbsp; &lt;mailto:<a onmousedow=
n=3D"this.href=3D'javascript:';return true;" onclick=3D"this.href=3D'javasc=
ript:';return true;" href=3D"javascript:" target=3D"_blank" rel=3D"nofollow=
" gdf-obfuscated-mailto=3D"qj1BooutI2kJ">ne...@eviloverlord.com</a><wbr>&gt=
;&nbsp; (847) 691-1404</div>
</div></div>
</blockquote></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_6508_710760806.1426702925760--
------=_Part_6507_1423946482.1426702925760--

.
