Topic: Proposal : Add front() and back() to


Author: Jake Arkinstall <jake.arkinstall@gmail.com>
Date: Wed, 18 Jul 2018 13:55:10 +0100
Raw View
--0000000000004a5f350571459356
Content-Type: text/plain; charset="UTF-8"

On Wed, 18 Jul 2018, 13:10 Olivier Sohn, <olivier.sohn@gmail.com> wrote:

> I found that there is no straightforward way to access the last (or first)
> element of a map.
>

For the last:

auto last_it = crbegin(your_map);
assert( last_it != crend(your_map) );
const auto& [key, value] = *(last_it);

For the first:

auto first_it = cbegin(your_map);
assert( first_it != cend(your_map) );
const auto& [key, value] = *(first_it);


I'd choose this over a function returning a reference to the relevant
std::pair any day of the week, because it gives me the ability to ensure
that the contract (the map isn't empty) is fulfilled.

The benefit is that you can then iterate backwards through the map if the
algorithm wants to - and if you want to apply your algorithm to a range of
elements within the map rather than the full thing, you can pass the
relevant crbegin iterator to your function.

I'd like to propose the following: add the front() and back() methods, with
> const and non const versions, to std::(multi)map and std::(multi)set, to
> return a (const-) reference to the last / first element(s).
>

Although I'd prefer the iterator approach, having a back and front might
prove popular enough to allow it. Let's see what the others here have to
say.

>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAC%2B0CCPSxmOXc0rAvztEMqWTCrSbhrbhr--vJ-JRK6cPRRLXdg%40mail.gmail.com.

--0000000000004a5f350571459356
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto"><div><div class=3D"gmail_quote"><div dir=3D"ltr">On Wed, =
18 Jul 2018, 13:10 Olivier Sohn, &lt;<a href=3D"mailto:olivier.sohn@gmail.c=
om">olivier.sohn@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"gma=
il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef=
t:1ex"><div dir=3D"ltr">I found that there is no straightforward way to acc=
ess the last (or first) element of a map.</div></blockquote></div></div><di=
v dir=3D"auto"><br></div><div dir=3D"auto">For the last:</div><div dir=3D"a=
uto"><br></div><div dir=3D"auto">auto last_it =3D crbegin(your_map);</div><=
div dir=3D"auto">assert( last_it !=3D crend(your_map) );</div><div dir=3D"a=
uto">const auto&amp; [key, value] =3D *(last_it);</div><div dir=3D"auto"><b=
r></div><div dir=3D"auto">For the first:</div><div dir=3D"auto"><br></div><=
div dir=3D"auto"><div dir=3D"auto" style=3D"font-family:sans-serif">auto fi=
rst_it =3D cbegin(your_map);</div><div dir=3D"auto" style=3D"font-family:sa=
ns-serif">assert( first_it !=3D cend(your_map) );</div><div dir=3D"auto" st=
yle=3D"font-family:sans-serif">const auto&amp; [key, value] =3D *(first_it)=
;</div><div dir=3D"auto" style=3D"font-family:sans-serif"><br></div></div><=
div dir=3D"auto"><br></div><div dir=3D"auto">I&#39;d choose this over a fun=
ction returning a reference to the relevant std::pair any day of the week, =
because it gives me the ability to ensure that the contract (the map isn&#3=
9;t empty) is fulfilled.</div><div dir=3D"auto"><br></div><div dir=3D"auto"=
>The benefit is that you can then iterate backwards through the map if the =
algorithm wants to - and if you want to apply your algorithm to a range of =
elements within the map rather than the full thing, you can pass the releva=
nt crbegin iterator to your function.</div><div dir=3D"auto"><br></div><div=
 dir=3D"auto"><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" =
style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><di=
v dir=3D"ltr"><div>I&#39;d like to propose the following: add the front() a=
nd back() methods, with const and non const versions, to std::(multi)map an=
d std::(multi)set, to return a (const-) reference to the last / first eleme=
nt(s).<br></div></div></blockquote></div></div><div dir=3D"auto"><br></div>=
<div dir=3D"auto">Although I&#39;d prefer the iterator approach, having a b=
ack and front might prove popular enough to allow it. Let&#39;s see what th=
e others here have to say.</div><div dir=3D"auto"><div class=3D"gmail_quote=
"><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:=
1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAC%2B0CCPSxmOXc0rAvztEMqWTCrSbhrbhr-=
-vJ-JRK6cPRRLXdg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAC%2B0CCPSxmOX=
c0rAvztEMqWTCrSbhrbhr--vJ-JRK6cPRRLXdg%40mail.gmail.com</a>.<br />

--0000000000004a5f350571459356--

.


Author: Richard Hodges <hodges.r@gmail.com>
Date: Wed, 18 Jul 2018 19:52:53 +0200
Raw View
--0000000000000ff4ca057149bcbc
Content-Type: text/plain; charset="UTF-8"

On Wed, 18 Jul 2018 at 14:10, Olivier Sohn <olivier.sohn@gmail.com> wrote:

> Hello,
>
> I recently encountered an algorithm where I needed to access the map's
> last element, knowing the map was non-empty. But I found that there is no
> straightforward way to access the last (or first) element of a map.
>

Without wishing to sound too controversial, I am wondering whether this
algorithm could have been expressed in a different way.

Wanting a reference to the "back" of a container suggests a need to access
the least/greatest/earliest/latest of a group of things.

Are you able to share the motivating use case, as there are other tools for
this in the standard library?


>
> So as suggested by this process : https://isocpp.org/std/submit-a-proposal
> I'd like to propose the following: add the front() and back() methods, with
> const and non const versions, to std::(multi)map and std::(multi)set, to
> return a (const-) reference to the last / first element(s).
>
> Like with other containers implementing these methods, no guaranty would
> be given when the container is empty.
>
> What do you think?
>
> Cheers,
> Olivier
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%40isocpp.org?utm_medium=email&utm_source=footer>
> .
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com.

--0000000000000ff4ca057149bcbc
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Wed=
, 18 Jul 2018 at 14:10, Olivier Sohn &lt;<a href=3D"mailto:olivier.sohn@gma=
il.com">olivier.sohn@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D=
"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding=
-left:1ex"><div dir=3D"ltr">Hello,<div><br></div><div>I recently encountere=
d an algorithm where I needed to access the map&#39;s last element, knowing=
 the map was non-empty. But I found that there is no straightforward way to=
 access the last (or first) element of a map.</div></div></blockquote><div>=
<br></div><div>Without wishing to sound too controversial, I am wondering w=
hether this algorithm could have been expressed in a different way.</div><d=
iv><br></div><div>Wanting a reference to the &quot;back&quot; of a containe=
r suggests a need to access the least/greatest/earliest/latest of a group o=
f things.=C2=A0</div><div><br></div><div>Are you able to share the motivati=
ng use case, as there are other tools for this in the standard library?<br>=
</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 =
0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div=
><br></div><div>So as suggested by this process :=C2=A0<a href=3D"https://i=
socpp.org/std/submit-a-proposal" target=3D"_blank">https://isocpp.org/std/s=
ubmit-a-proposal</a> I&#39;d like to propose the following: add the front()=
 and back() methods, with const and non const versions, to std::(multi)map =
and std::(multi)set, to return a (const-) reference to the last / first ele=
ment(s).</div><div><br></div><div>Like with other containers implementing t=
hese methods, no guaranty would be given when the container is empty.</div>=
<div><br></div><div>What do you think?</div><div><br></div><div>Cheers,</di=
v><div>Olivier</div><div><br></div></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-=
45df-9536-e0bfab52962b%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3=
ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXg=
jJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com</a>.<br />

--0000000000000ff4ca057149bcbc--

.


Author: "Olivier S." <olivier.sohn@gmail.com>
Date: Thu, 19 Jul 2018 11:11:14 +0200
Raw View
--0000000000005a10ad05715690d1
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hello,

This is the motivating example:

#include <cassert>

#include <iostream>

#include <map>

#include <vector>


using namespace std;


using Intervals =3D map<float,int>;


int sample(Intervals const & intervals, float randomFloat) {

    assert(!intervals.empty());

    auto it =3D intervals.lower_bound(randomFloat);

    if(it =3D=3D intervals.end()) {

        // we return the last element of the map:

        return crbegin(intervals)->second;

        // this is what I would prefer writing:

        // return intervals.back();

    }

    return it->second;

}


int main() {

    // The goal is to sample among ints from 0 to 9, with different
probability for each int.


    // The probability of i is proba[i].

    std::array<float,10> proba {{0.01,0.05,0.01,0.15,0.1,0.19,0.19,0.1,0.19=
,
0.01}};


    // The sum of probabilities are 1, so we can conceptually partition

    // the [0,1] interval in 10 intervals of lengths proba[i].

    //

    // we only need to store the upper bounds of these intervals,

    // because the lower bound of interval n is the upper bound of interval
n-1,

    // and because we know that the first lower bound is 0:

    Intervals intervals;

    int i =3D 0;

    float upperbound =3D 0.f;

    for(auto p : proba) {

        upperbound +=3D p;

        intervals.emplace(upperbound,i);

        // Note that the last key, due to floating point rounding errors,
will be

        // close to 1.f but not necessarilly 1.f.

        ++i;

    }



    // Then, to sample ints, we generate a random numbers between 0 and 1,

    // and lookup the correponding interval:


    for(auto v : vector<float>{{

        0.f,

        0.00001f,

        0.02f,

        0.5f,

        0.98f,

        1.f,

        1.00001f}})

    {

        cout << sample(intervals, v) << endl;

    }


    return 0;

}



Olivier


Le mer. 18 juil. 2018 =C3=A0 19:53, Richard Hodges <hodges.r@gmail.com> a =
=C3=A9crit :

>
>
> On Wed, 18 Jul 2018 at 14:10, Olivier Sohn <olivier.sohn@gmail.com> wrote=
:
>
>> Hello,
>>
>> I recently encountered an algorithm where I needed to access the map's
>> last element, knowing the map was non-empty. But I found that there is n=
o
>> straightforward way to access the last (or first) element of a map.
>>
>
> Without wishing to sound too controversial, I am wondering whether this
> algorithm could have been expressed in a different way.
>
> Wanting a reference to the "back" of a container suggests a need to acces=
s
> the least/greatest/earliest/latest of a group of things.
>
> Are you able to share the motivating use case, as there are other tools
> for this in the standard library?
>
>
>>
>> So as suggested by this process :
>> https://isocpp.org/std/submit-a-proposal I'd like to propose the
>> following: add the front() and back() methods, with const and non const
>> versions, to std::(multi)map and std::(multi)set, to return a (const-)
>> reference to the last / first element(s).
>>
>> Like with other containers implementing these methods, no guaranty would
>> be given when the container is empty.
>>
>> What do you think?
>>
>> Cheers,
>> Olivier
>>
>> --
>> You received this message because you are subscribed to the Google Group=
s
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n
>> email to std-proposals+unsubscribe@isocpp.org.
>> To post to this group, send email to std-proposals@isocpp.org.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-63=
37-45df-9536-e0bfab52962b%40isocpp.org
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6=
337-45df-9536-e0bfab52962b%40isocpp.org?utm_medium=3Demail&utm_source=3Dfoo=
ter>
>> .
>>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/=
unsubscribe
> .
> To unsubscribe from this group and all its topics, send an email to
> std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVY=
rbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNV=
YrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Demai=
l&utm_source=3Dfooter>
> .
>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3=
D%2Bzeg%40mail.gmail.com.

--0000000000005a10ad05715690d1
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hello,<div><br></div><div>This is the motivating example:<=
/div><div><br></div><div>




<span></span>





<p class=3D"gmail-p1" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(81,182,255);background-=
color:rgb(0,0,0)">#include <span class=3D"gmail-s1" style=3D"color:rgb(180,=
38,26)">&lt;cassert&gt;</span></p>
<p class=3D"gmail-p2" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(180,38,26);background-c=
olor:rgb(0,0,0)"><span class=3D"gmail-s2" style=3D"color:rgb(81,182,255)">#=
include </span>&lt;iostream&gt;</p>
<p class=3D"gmail-p1" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(81,182,255);background-=
color:rgb(0,0,0)">#include <span class=3D"gmail-s1" style=3D"color:rgb(180,=
38,26)">&lt;map&gt;</span></p>
<p class=3D"gmail-p1" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(81,182,255);background-=
color:rgb(0,0,0)">#include <span class=3D"gmail-s1" style=3D"color:rgb(180,=
38,26)">&lt;vector&gt;</span></p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Helvetica;background-color:rgb(0,0,0);m=
in-height:14px"><br></p>
<p class=3D"gmail-p4" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(255,178,109);background=
-color:rgb(0,0,0)">using<span class=3D"gmail-s3" style=3D"color:rgb(231,254=
,226)"> </span>namespace<span class=3D"gmail-s3" style=3D"color:rgb(231,254=
,226)"> std;</span></p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Helvetica;background-color:rgb(0,0,0);m=
in-height:14px"><br></p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)=
">using</span> Intervals =3D map&lt;<span class=3D"gmail-s4" style=3D"color=
:rgb(255,178,109)">float</span>,<span class=3D"gmail-s4" style=3D"color:rgb=
(255,178,109)">int</span>&gt;;</p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Helvetica;background-color:rgb(0,0,0);m=
in-height:14px"><br></p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)=
">int</span> sample(Intervals <span class=3D"gmail-s4" style=3D"color:rgb(2=
55,178,109)">const</span> &amp; intervals, <span class=3D"gmail-s4" style=
=3D"color:rgb(255,178,109)">float</span> randomFloat) {</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span>assert(!intervals.empty());</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">auto</=
span> it =3D intervals.lower_bound(randomFloat);</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">if</sp=
an>(it =3D=3D intervals.end()) {</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </spa=
n></span>// we return the last element of the map:</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span><span class=3D"gmail-s4" style=3D"color:rgb(255,17=
8,109)">return</span> crbegin(intervals)-&gt;second;</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </spa=
n></span>// this is what I would prefer writing:</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </spa=
n></span>// return intervals.back();</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span>}</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">return=
</span> it-&gt;second;</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)">}</p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Helvetica;background-color:rgb(0,0,0);m=
in-height:14px"><br></p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)=
">int</span> main() {</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// Th=
e goal is to sample among ints from 0 to 9, with different probability for =
each int.</p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Helvetica;background-color:rgb(0,0,0);m=
in-height:14px"><br></p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// Th=
e probability of i is proba[i].</p>
<p class=3D"gmail-p7" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(212,212,212);background=
-color:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)=
"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>std::arr=
ay&lt;</span><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">floa=
t</span><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)">,</span>1=
0<span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)">&gt; proba {{</s=
pan>0.01<span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0=
..05<span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.01<s=
pan class=3D"gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.15<span c=
lass=3D"gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.1<span class=
=3D"gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.19<span class=3D"g=
mail-s3" style=3D"color:rgb(231,254,226)">,</span>0.19<span class=3D"gmail-=
s3" style=3D"color:rgb(231,254,226)">,</span>0.1<span class=3D"gmail-s3" st=
yle=3D"color:rgb(231,254,226)">,</span>0.19<span class=3D"gmail-s3" style=
=3D"color:rgb(231,254,226)">,</span>0.01<span class=3D"gmail-s3" style=3D"c=
olor:rgb(231,254,226)">}};</span></p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Helvetica;background-color:rgb(0,0,0);m=
in-height:14px"><br></p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// Th=
e sum of probabilities are 1, so we can conceptually partition</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// th=
e [0,1] interval in 10 intervals of lengths proba[i].</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span><span class=3D"gmail-s5" style=3D"color:rgb(0,193,0)">//</span><=
/p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// we=
 only need to store the upper bounds of these intervals,</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// be=
cause the lower bound of interval n is the upper bound of interval n-1,</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// an=
d because we know that the first lower bound is 0:</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span>Intervals intervals;</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">int</s=
pan> i =3D <span class=3D"gmail-s6" style=3D"color:rgb(212,212,212)">0</spa=
n>;</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">float<=
/span> upperbound =3D <span class=3D"gmail-s6" style=3D"color:rgb(212,212,2=
12)">0.f</span>;</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">for</s=
pan>(<span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">auto</span> =
p : proba) {</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span>upperbound +=3D p;</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span>intervals.emplace(upperbound,i);</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </spa=
n></span>// Note that the last key, due to floating point rounding errors, =
will be</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </spa=
n></span>// close to 1.f but not necessarilly 1.f.</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span>++i;</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span>}</p>
<p class=3D"gmail-p8" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0);min-height:14px"><span class=3D"gmail-Apple-converted-spa=
ce">=C2=A0=C2=A0 =C2=A0</span></p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// Th=
en, to sample ints, we generate a random numbers between 0 and 1,</p>
<p class=3D"gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);background-col=
or:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"><s=
pan class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// an=
d lookup the correponding interval:</p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Helvetica;background-color:rgb(0,0,0);m=
in-height:14px"><br></p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span><span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">for</s=
pan>(<span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">auto</span> =
v : vector&lt;<span class=3D"gmail-s4" style=3D"color:rgb(255,178,109)">flo=
at</span>&gt;{{</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span><span class=3D"gmail-s6" style=3D"color:rgb(212,21=
2,212)">0.f</span>,</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span><span class=3D"gmail-s6" style=3D"color:rgb(212,21=
2,212)">0.00001f</span>,</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span><span class=3D"gmail-s6" style=3D"color:rgb(212,21=
2,212)">0.02f</span>,</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span><span class=3D"gmail-s6" style=3D"color:rgb(212,21=
2,212)">0.5f</span>,</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span><span class=3D"gmail-s6" style=3D"color:rgb(212,21=
2,212)">0.98f</span>,</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span><span class=3D"gmail-s6" style=3D"color:rgb(212,21=
2,212)">1.f</span>,</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span><span class=3D"gmail-s6" style=3D"color:rgb(212,21=
2,212)">1.00001f</span>}})</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span>{</p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span>cout &lt;&lt; sample(intervals, v) &lt;&lt; endl;<=
/p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=
=A0 </span>}</p>
<p class=3D"gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Helvetica;background-color:rgb(0,0,0);m=
in-height:14px"><br></p>
<p class=3D"gmail-p4" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(255,178,109);background=
-color:rgb(0,0,0)"><span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)=
"><span class=3D"gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>r=
eturn<span class=3D"gmail-s3" style=3D"color:rgb(231,254,226)"> </span><spa=
n class=3D"gmail-s6" style=3D"color:rgb(212,212,212)">0</span><span class=
=3D"gmail-s3" style=3D"color:rgb(231,254,226)">;</span></p>
<p class=3D"gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)">}</p><p class=3D"gmail-p5" style=3D"margin:0px;font-vari=
ant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-s=
tretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb=
(231,254,226);background-color:rgb(0,0,0)"><br></p>


</div><div><br></div><div>Olivier</div><div><br></div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr">Le=C2=A0mer. 18 juil. 2018 =C3=A0=C2=A019=
:53, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com">hodges.r@gmai=
l.com</a>&gt; a =C3=A9crit=C2=A0:<br></div><blockquote class=3D"gmail_quote=
" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><=
div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Wed,=
 18 Jul 2018 at 14:10, Olivier Sohn &lt;<a href=3D"mailto:olivier.sohn@gmai=
l.com" target=3D"_blank">olivier.sohn@gmail.com</a>&gt; wrote:<br></div><bl=
ockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #=
ccc solid;padding-left:1ex"><div dir=3D"ltr">Hello,<div><br></div><div>I re=
cently encountered an algorithm where I needed to access the map&#39;s last=
 element, knowing the map was non-empty. But I found that there is no strai=
ghtforward way to access the last (or first) element of a map.</div></div><=
/blockquote><div><br></div><div>Without wishing to sound too controversial,=
 I am wondering whether this algorithm could have been expressed in a diffe=
rent way.</div><div><br></div><div>Wanting a reference to the &quot;back&qu=
ot; of a container suggests a need to access the least/greatest/earliest/la=
test of a group of things.=C2=A0</div><div><br></div><div>Are you able to s=
hare the motivating use case, as there are other tools for this in the stan=
dard library?<br></div><div>=C2=A0</div><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div=
 dir=3D"ltr"><div><br></div><div>So as suggested by this process :=C2=A0<a =
href=3D"https://isocpp.org/std/submit-a-proposal" target=3D"_blank">https:/=
/isocpp.org/std/submit-a-proposal</a> I&#39;d like to propose the following=
: add the front() and back() methods, with const and non const versions, to=
 std::(multi)map and std::(multi)set, to return a (const-) reference to the=
 last / first element(s).</div><div><br></div><div>Like with other containe=
rs implementing these methods, no guaranty would be given when the containe=
r is empty.</div><div><br></div><div>What do you think?</div><div><br></div=
><div>Cheers,</div><div>Olivier</div><div><br></div></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-=
45df-9536-e0bfab52962b%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3=
ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfooter"=
 target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-propo=
sals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com</=
a>.<br>
</blockquote></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsi=
bvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pdx_=
xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gmail.com</a>.<br />

--0000000000005a10ad05715690d1--

.


Author: Richard Hodges <hodges.r@gmail.com>
Date: Thu, 19 Jul 2018 12:32:26 +0100
Raw View
--0000000000004838ab05715889b4
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Thu, 19 Jul 2018 at 10:11, Olivier S. <olivier.sohn@gmail.com> wrote:

> Hello,
>
> This is the motivating example:
>

I think I'd be inclined to transform the probabilities into an array of
limits and perform the upper_bound on that:

//
// Created by rhodges on 19/07/18.
//

#include <cassert>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <type_traits>
#include <array>


/// Convert a container of probabilities into a container of ascending
interval limits
template<class Container>
auto to_limits(Container container) {
    using container_type =3D std::decay_t<Container>;
    using value_type =3D typename container_type::value_type;
    auto to_total =3D [total =3D value_type(0)](auto &&x) mutable {
        total +=3D x;
        return total;
    };

    std::transform(std::begin(container), std::end(container),
                   std::begin(container),
                   to_total);

    return container;
};

template<class Container>
struct assert_not_empty {
    assert_not_empty(Container const &container) noexcept {
        assert(!container.empty());
    }
};

template<class Type, std::size_t N>
struct assert_not_empty<std::array<Type, N>> {
    constexpr assert_not_empty(std::array<Type, N> const &container) noexce=
pt {
        static_assert(!container.empty());
    }
};


/// given an ascending container of limits, choose one with
probability equal to 1.0 / max(limits)
/// @returns an integer containing the index of the chosen limit
template<class Container, class Value>
int sample_limits(Container &&totals, Value &&prob) {

    // precondition
    assert_not_empty{totals};

    // convert prob (range 0-1) into a total.

    auto target =3D prob * totals.back();

    auto cap =3D [&totals](auto iter) {
        if (iter =3D=3D std::end(totals))
            return std::prev(iter);
        else
            return iter;
    };

    auto first =3D std::cbegin(totals);
    auto last =3D std::cend(totals);

    auto index =3D std::distance(first,
                               cap(std::lower_bound(first,
                                                    last,
                                                    target)));
    return int(index);
}

int main() {

    // The goal is to sample among ints from 0 to 9, with different
probability for each int.
    // The probability of i is proba[i] / sum(proba).
    std::array<float, 10> proba{{0.01, 0.05, 0.01, 0.15, 0.1, 0.19,
0.19, 0.1, 0.19, 0.01}};
    auto limits =3D to_limits(proba);
    for (auto v : std::vector<float>{{
                                             0.f,
                                             0.00001f,
                                             0.02f,
                                             0.5f,
                                             0.98f,
                                             1.f,  // on upper bound
                                             1.00001f  // beyond upper boun=
d
                                     }}) {
        std::cout << sample_limits(limits, v) << std::endl;
    }

    return 0;
}




>
> #include <cassert>
>
> #include <iostream>
>
> #include <map>
>
> #include <vector>
>
>
> using namespace std;
>
>
> using Intervals =3D map<float,int>;
>
>
> int sample(Intervals const & intervals, float randomFloat) {
>
>     assert(!intervals.empty());
>
>     auto it =3D intervals.lower_bound(randomFloat);
>
>     if(it =3D=3D intervals.end()) {
>
>         // we return the last element of the map:
>
>         return crbegin(intervals)->second;
>
>         // this is what I would prefer writing:
>
>         // return intervals.back();
>
>     }
>
>     return it->second;
>
> }
>
>
> int main() {
>
>     // The goal is to sample among ints from 0 to 9, with different
> probability for each int.
>
>
>     // The probability of i is proba[i].
>
>     std::array<float,10> proba {{0.01,0.05,0.01,0.15,0.1,0.19,0.19,0.1,
> 0.19,0.01}};
>
>
>     // The sum of probabilities are 1, so we can conceptually partition
>
>     // the [0,1] interval in 10 intervals of lengths proba[i].
>
>     //
>
>     // we only need to store the upper bounds of these intervals,
>
>     // because the lower bound of interval n is the upper bound of
> interval n-1,
>
>     // and because we know that the first lower bound is 0:
>
>     Intervals intervals;
>
>     int i =3D 0;
>
>     float upperbound =3D 0.f;
>
>     for(auto p : proba) {
>
>         upperbound +=3D p;
>
>         intervals.emplace(upperbound,i);
>
>         // Note that the last key, due to floating point rounding errors,
> will be
>
>         // close to 1.f but not necessarilly 1.f.
>
>         ++i;
>
>     }
>
>
>
>     // Then, to sample ints, we generate a random numbers between 0 and 1=
,
>
>     // and lookup the correponding interval:
>
>
>     for(auto v : vector<float>{{
>
>         0.f,
>
>         0.00001f,
>
>         0.02f,
>
>         0.5f,
>
>         0.98f,
>
>         1.f,
>
>         1.00001f}})
>
>     {
>
>         cout << sample(intervals, v) << endl;
>
>     }
>
>
>     return 0;
>
> }
>
>
>
> Olivier
>
>
> Le mer. 18 juil. 2018 =C3=A0 19:53, Richard Hodges <hodges.r@gmail.com> a
> =C3=A9crit :
>
>>
>>
>> On Wed, 18 Jul 2018 at 14:10, Olivier Sohn <olivier.sohn@gmail.com>
>> wrote:
>>
>>> Hello,
>>>
>>> I recently encountered an algorithm where I needed to access the map's
>>> last element, knowing the map was non-empty. But I found that there is =
no
>>> straightforward way to access the last (or first) element of a map.
>>>
>>
>> Without wishing to sound too controversial, I am wondering whether this
>> algorithm could have been expressed in a different way.
>>
>> Wanting a reference to the "back" of a container suggests a need to
>> access the least/greatest/earliest/latest of a group of things.
>>
>> Are you able to share the motivating use case, as there are other tools
>> for this in the standard library?
>>
>>
>>>
>>> So as suggested by this process :
>>> https://isocpp.org/std/submit-a-proposal I'd like to propose the
>>> following: add the front() and back() methods, with const and non const
>>> versions, to std::(multi)map and std::(multi)set, to return a (const-)
>>> reference to the last / first element(s).
>>>
>>> Like with other containers implementing these methods, no guaranty woul=
d
>>> be given when the container is empty.
>>>
>>> What do you think?
>>>
>>> Cheers,
>>> Olivier
>>>
>>> --
>>> 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.
>>> To view this discussion on the web visit
>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6=
337-45df-9536-e0bfab52962b%40isocpp.org
>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-=
6337-45df-9536-e0bfab52962b%40isocpp.org?utm_medium=3Demail&utm_source=3Dfo=
oter>
>>> .
>>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe
>> .
>> To unsubscribe from this group and all its topics, send an email to
>> std-proposals+unsubscribe@isocpp.org.
>> To post to this group, send email to std-proposals@isocpp.org.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNV=
YrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHN=
VYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Dema=
il&utm_source=3Dfooter>
>> .
>>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pdx=
_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gmail.com
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pd=
x_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=3D=
email&utm_source=3Dfooter>
> .
>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJC=
x7-pDfYuA%40mail.gmail.com.

--0000000000004838ab05715889b4
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Thu=
, 19 Jul 2018 at 10:11, Olivier S. &lt;<a href=3D"mailto:olivier.sohn@gmail=
..com">olivier.sohn@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"g=
mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=
eft:1ex"><div dir=3D"ltr">Hello,<div><br></div><div>This is the motivating =
example:</div></div></blockquote><div><br></div><div>I think I&#39;d be inc=
lined to transform the probabilities into an array of limits and perform th=
e upper_bound on that:</div><div><br></div><div><pre style=3D"background-co=
lor:rgb(43,43,43);color:rgb(169,183,198);font-family:&quot;DejaVu Sans Mono=
&quot;;font-size:11.3pt"><span style=3D"color:rgb(128,128,128)">//<br></spa=
n><span style=3D"color:rgb(128,128,128)">// Created by rhodges on 19/07/18.=
<br></span><span style=3D"color:rgb(128,128,128)">//<br></span><span style=
=3D"color:rgb(128,128,128)"><br></span><span style=3D"color:rgb(187,181,41)=
">#include </span><span style=3D"color:rgb(106,135,89)">&lt;cassert&gt;<br>=
</span><span style=3D"color:rgb(187,181,41)">#include </span><span style=3D=
"color:rgb(106,135,89)">&lt;iostream&gt;<br></span><span style=3D"color:rgb=
(187,181,41)">#include </span><span style=3D"color:rgb(106,135,89)">&lt;vec=
tor&gt;<br></span><span style=3D"color:rgb(187,181,41)">#include </span><sp=
an style=3D"color:rgb(106,135,89)">&lt;algorithm&gt;<br></span><span style=
=3D"color:rgb(187,181,41)">#include </span><span style=3D"color:rgb(106,135=
,89)">&lt;iterator&gt;<br></span><span style=3D"color:rgb(187,181,41)">#inc=
lude </span><span style=3D"color:rgb(106,135,89)">&lt;type_traits&gt;<br></=
span><span style=3D"color:rgb(187,181,41)">#include </span><span style=3D"c=
olor:rgb(106,135,89)">&lt;array&gt;<br></span><span style=3D"color:rgb(106,=
135,89)"><br></span><span style=3D"color:rgb(106,135,89)"><br></span><span =
style=3D"color:rgb(128,128,128)">/// Convert a container of probabilities i=
nto a container of ascending interval limits<br></span><span style=3D"color=
:rgb(204,120,50);font-weight:bold">template</span>&lt;<span style=3D"color:=
rgb(204,120,50);font-weight:bold">class </span><span style=3D"color:rgb(185=
,188,209)">Container</span>&gt;<br><span style=3D"color:rgb(204,120,50);fon=
t-weight:bold">auto </span><span style=3D"color:rgb(255,198,109)">to_limits=
</span>(<span style=3D"color:rgb(185,188,209)">Container </span>container) =
{<br>    <span style=3D"color:rgb(204,120,50);font-weight:bold">using </spa=
n><span style=3D"color:rgb(185,188,209)">container_type </span>=3D <span st=
yle=3D"color:rgb(181,182,227)">std</span>::<span style=3D"color:rgb(185,188=
,209)">decay_t</span>&lt;<span style=3D"color:rgb(185,188,209)">Container</=
span>&gt;<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"c=
olor:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50);font-=
weight:bold">using </span><span style=3D"color:rgb(185,188,209)">value_type=
 </span>=3D <span style=3D"color:rgb(204,120,50);font-weight:bold">typename=
 </span><span style=3D"color:rgb(185,188,209)">container_type</span>::<span=
 style=3D"color:rgb(185,188,209)">value_type</span><span style=3D"color:rgb=
(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    </span>=
<span style=3D"color:rgb(204,120,50);font-weight:bold">auto </span>to_total=
 =3D [total =3D <span style=3D"color:rgb(185,188,209)">value_type</span>(<s=
pan style=3D"color:rgb(104,151,187)">0</span>)](<span style=3D"color:rgb(20=
4,120,50);font-weight:bold">auto </span>&amp;&amp;x) <span style=3D"color:r=
gb(204,120,50);font-weight:bold">mutable </span>{<br>        total +=3D x<s=
pan style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(20=
4,120,50)">        </span><span style=3D"color:rgb(204,120,50);font-weight:=
bold">return </span>total<span style=3D"color:rgb(204,120,50)">;<br></span>=
<span style=3D"color:rgb(204,120,50)">    </span>}<span style=3D"color:rgb(=
204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><=
span style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(18=
1,182,227)">std</span>::transform(<span style=3D"color:rgb(181,182,227)">st=
d</span>::begin(container)<span style=3D"color:rgb(204,120,50)">, </span><s=
pan style=3D"color:rgb(181,182,227)">std</span>::end(container)<span style=
=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)=
">                   </span><span style=3D"color:rgb(181,182,227)">std</spa=
n>::begin(container)<span style=3D"color:rgb(204,120,50)">,<br></span><span=
 style=3D"color:rgb(204,120,50)">                   </span>to_total)<span s=
tyle=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120=
,50)"><br></span><span style=3D"color:rgb(204,120,50)">    </span><span sty=
le=3D"color:rgb(204,120,50);font-weight:bold">return </span>container<span =
style=3D"color:rgb(204,120,50)">;<br></span>}<span style=3D"color:rgb(204,1=
20,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><span =
style=3D"color:rgb(204,120,50);font-weight:bold">template</span>&lt;<span s=
tyle=3D"color:rgb(204,120,50);font-weight:bold">class </span><span style=3D=
"color:rgb(185,188,209)">Container</span>&gt;<br><span style=3D"color:rgb(2=
04,120,50);font-weight:bold">struct </span><span style=3D"color:rgb(181,182=
,227)">assert_not_empty </span>{<br>    <span style=3D"color:rgb(255,198,10=
9)">assert_not_empty</span>(<span style=3D"color:rgb(185,188,209)">Containe=
r </span><span style=3D"color:rgb(204,120,50);font-weight:bold">const </spa=
n>&amp;container) <span style=3D"color:rgb(204,120,50);font-weight:bold">no=
except </span>{<br>        <span style=3D"color:rgb(144,139,37)">assert</sp=
an>(!container.<span style=3D"color:rgb(152,118,170);font-style:italic">emp=
ty</span>())<span style=3D"color:rgb(204,120,50)">;<br></span><span style=
=3D"color:rgb(204,120,50)">    </span>}<br>}<span style=3D"color:rgb(204,12=
0,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><span s=
tyle=3D"color:rgb(204,120,50);font-weight:bold">template</span>&lt;<span st=
yle=3D"color:rgb(204,120,50);font-weight:bold">class </span><span style=3D"=
color:rgb(185,188,209)">Type</span><span style=3D"color:rgb(204,120,50)">, =
</span><span style=3D"color:rgb(181,182,227)">std</span>::<span style=3D"co=
lor:rgb(185,188,209)">size_t </span><span style=3D"color:rgb(152,118,170);f=
ont-style:italic">N</span>&gt;<br><span style=3D"color:rgb(204,120,50);font=
-weight:bold">struct </span><span style=3D"color:rgb(181,182,227)">assert_n=
ot_empty</span>&lt;<span style=3D"color:rgb(181,182,227)">std</span>::<span=
 style=3D"color:rgb(181,182,227)">array</span>&lt;<span style=3D"color:rgb(=
185,188,209)">Type</span><span style=3D"color:rgb(204,120,50)">, </span><sp=
an style=3D"color:rgb(152,118,170);font-style:italic">N</span>&gt;&gt; {<br=
>    <span style=3D"color:rgb(204,120,50);font-weight:bold">constexpr </spa=
n><span style=3D"color:rgb(255,198,109)">assert_not_empty</span>(<span styl=
e=3D"color:rgb(181,182,227)">std</span>::<span style=3D"color:rgb(181,182,2=
27)">array</span>&lt;<span style=3D"color:rgb(185,188,209)">Type</span><spa=
n style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(152,118=
,170);font-style:italic">N</span>&gt; <span style=3D"color:rgb(204,120,50);=
font-weight:bold">const </span>&amp;container) <span style=3D"color:rgb(204=
,120,50);font-weight:bold">noexcept </span>{<br>        <span style=3D"colo=
r:rgb(204,120,50);font-weight:bold">static_assert</span>(!container.empty()=
)<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb=
(204,120,50)">    </span>}<br>}<span style=3D"color:rgb(204,120,50)">;<br><=
/span><span style=3D"color:rgb(204,120,50)"><br></span><span style=3D"color=
:rgb(204,120,50)"><br></span><span style=3D"color:rgb(128,128,128)">/// giv=
en an ascending container of limits, choose one with probability equal to 1=
..0 / max(limits)<br></span><span style=3D"color:rgb(128,128,128)">/// @retu=
rns an integer containing the index of the chosen limit<br></span><span sty=
le=3D"color:rgb(204,120,50);font-weight:bold">template</span>&lt;<span styl=
e=3D"color:rgb(204,120,50);font-weight:bold">class </span><span style=3D"co=
lor:rgb(185,188,209)">Container</span><span style=3D"color:rgb(204,120,50)"=
>, </span><span style=3D"color:rgb(204,120,50);font-weight:bold">class </sp=
an><span style=3D"color:rgb(185,188,209)">Value</span>&gt;<br><span style=
=3D"color:rgb(204,120,50);font-weight:bold">int </span><span style=3D"color=
:rgb(255,198,109)">sample_limits</span>(<span style=3D"color:rgb(185,188,20=
9)">Container </span>&amp;&amp;totals<span style=3D"color:rgb(204,120,50)">=
, </span><span style=3D"color:rgb(185,188,209)">Value </span>&amp;&amp;prob=
) {<br><br>    <span style=3D"color:rgb(128,128,128)">// precondition<br></=
span><span style=3D"color:rgb(128,128,128)">    </span>assert_not_empty{tot=
als}<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:=
rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50)">    </spa=
n><span style=3D"color:rgb(128,128,128)">// convert prob (range 0-1) into a=
 total.<br></span><span style=3D"color:rgb(128,128,128)"><br></span><span s=
tyle=3D"color:rgb(128,128,128)">    </span><span style=3D"color:rgb(204,120=
,50);font-weight:bold">auto </span>target =3D prob * totals.<span style=3D"=
color:rgb(152,118,170);font-style:italic">back</span>()<span style=3D"color=
:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></s=
pan><span style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:r=
gb(204,120,50);font-weight:bold">auto </span>cap =3D [&amp;totals](<span st=
yle=3D"color:rgb(204,120,50);font-weight:bold">auto </span>iter) {<br>     =
   <span style=3D"color:rgb(204,120,50);font-weight:bold">if </span>(iter =
=3D=3D <span style=3D"color:rgb(181,182,227)">std</span>::end(totals))<br> =
           <span style=3D"color:rgb(204,120,50);font-weight:bold">return </=
span><span style=3D"color:rgb(181,182,227)">std</span>::prev(iter)<span sty=
le=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,5=
0)">        </span><span style=3D"color:rgb(204,120,50);font-weight:bold">e=
lse<br></span><span style=3D"color:rgb(204,120,50);font-weight:bold">      =
      return </span>iter<span style=3D"color:rgb(204,120,50)">;<br></span><=
span style=3D"color:rgb(204,120,50)">    </span>}<span style=3D"color:rgb(2=
04,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><s=
pan style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204=
,120,50);font-weight:bold">auto </span>first =3D <span style=3D"color:rgb(1=
81,182,227)">std</span>::cbegin(totals)<span style=3D"color:rgb(204,120,50)=
">;<br></span><span style=3D"color:rgb(204,120,50)">    </span><span style=
=3D"color:rgb(204,120,50);font-weight:bold">auto </span>last =3D <span styl=
e=3D"color:rgb(181,182,227)">std</span>::cend(totals)<span style=3D"color:r=
gb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></spa=
n><span style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb=
(204,120,50);font-weight:bold">auto </span>index =3D <span style=3D"color:r=
gb(181,182,227)">std</span>::distance(first<span style=3D"color:rgb(204,120=
,50)">,<br></span><span style=3D"color:rgb(204,120,50)">                   =
            </span>cap(<span style=3D"color:rgb(181,182,227)">std</span>::l=
ower_bound(first<span style=3D"color:rgb(204,120,50)">,<br></span><span sty=
le=3D"color:rgb(204,120,50)">                                              =
      </span>last<span style=3D"color:rgb(204,120,50)">,<br></span><span st=
yle=3D"color:rgb(204,120,50)">                                             =
       </span>target)))<span style=3D"color:rgb(204,120,50)">;<br></span><s=
pan style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204=
,120,50);font-weight:bold">return int</span>(index)<span style=3D"color:rgb=
(204,120,50)">;<br></span>}<br><br><span style=3D"color:rgb(204,120,50);fon=
t-weight:bold">int </span><span style=3D"color:rgb(255,198,109)">main</span=
>() {<br><br>    <span style=3D"color:rgb(128,128,128)">// The goal is to s=
ample among ints from 0 to 9, with different probability for each int.<br><=
/span><span style=3D"color:rgb(128,128,128)">    // The probability of i is=
 proba[i] / sum(proba).<br></span><span style=3D"color:rgb(128,128,128)">  =
  </span><span style=3D"color:rgb(181,182,227)">std</span>::<span style=3D"=
color:rgb(181,182,227)">array</span>&lt;<span style=3D"color:rgb(204,120,50=
);font-weight:bold">float</span><span style=3D"color:rgb(204,120,50)">, </s=
pan><span style=3D"color:rgb(104,151,187)">10</span>&gt; proba{{<span style=
=3D"color:rgb(104,151,187)">0.01</span><span style=3D"color:rgb(204,120,50)=
">, </span><span style=3D"color:rgb(104,151,187)">0.05</span><span style=3D=
"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.0=
1</span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color=
:rgb(104,151,187)">0.15</span><span style=3D"color:rgb(204,120,50)">, </spa=
n><span style=3D"color:rgb(104,151,187)">0.1</span><span style=3D"color:rgb=
(204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.19</span><s=
pan style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,1=
51,187)">0.19</span><span style=3D"color:rgb(204,120,50)">, </span><span st=
yle=3D"color:rgb(104,151,187)">0.1</span><span style=3D"color:rgb(204,120,5=
0)">, </span><span style=3D"color:rgb(104,151,187)">0.19</span><span style=
=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">=
0.01</span>}}<span style=3D"color:rgb(204,120,50)">;<br></span><span style=
=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50);=
font-weight:bold">auto </span>limits =3D to_limits(proba)<span style=3D"col=
or:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    <=
/span><span style=3D"color:rgb(204,120,50);font-weight:bold">for </span>(<s=
pan style=3D"color:rgb(204,120,50);font-weight:bold">auto </span>v : <span =
style=3D"color:rgb(181,182,227)">std</span>::vector&lt;<span style=3D"color=
:rgb(204,120,50);font-weight:bold">float</span>&gt;{{<br>                  =
                           <span style=3D"color:rgb(104,151,187)">0.f</span=
><span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:rgb=
(204,120,50)">                                             </span><span sty=
le=3D"color:rgb(104,151,187)">0.00001f</span><span style=3D"color:rgb(204,1=
20,50)">,<br></span><span style=3D"color:rgb(204,120,50)">                 =
                            </span><span style=3D"color:rgb(104,151,187)">0=
..02f</span><span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D=
"color:rgb(204,120,50)">                                             </span=
><span style=3D"color:rgb(104,151,187)">0.5f</span><span style=3D"color:rgb=
(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">           =
                                  </span><span style=3D"color:rgb(104,151,1=
87)">0.98f</span><span style=3D"color:rgb(204,120,50)">,<br></span><span st=
yle=3D"color:rgb(204,120,50)">                                             =
</span><span style=3D"color:rgb(104,151,187)">1.f</span><span style=3D"colo=
r:rgb(204,120,50)">,  </span><span style=3D"color:rgb(128,128,128)">// on u=
pper bound<br></span><span style=3D"color:rgb(128,128,128)">               =
                              </span><span style=3D"color:rgb(104,151,187)"=
>1.00001f  </span><span style=3D"color:rgb(128,128,128)">// beyond upper bo=
und<br></span><span style=3D"color:rgb(128,128,128)">                      =
               </span>}}) {<br>        <span style=3D"color:rgb(181,182,227=
)">std</span>::cout <span style=3D"color:rgb(95,140,138)">&lt;&lt; </span>s=
ample_limits(limits<span style=3D"color:rgb(204,120,50)">, </span>v) <span =
style=3D"color:rgb(95,140,138)">&lt;&lt; </span><span style=3D"color:rgb(18=
1,182,227)">std</span>::endl<span style=3D"color:rgb(204,120,50)">;<br></sp=
an><span style=3D"color:rgb(204,120,50)">    </span>}<br>    <br>    <span =
style=3D"color:rgb(204,120,50);font-weight:bold">return </span><span style=
=3D"color:rgb(104,151,187)">0</span><span style=3D"color:rgb(204,120,50)">;=
<br></span>}<br><br></pre><br></div><div>=C2=A0</div><blockquote class=3D"g=
mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=
eft:1ex"><div dir=3D"ltr"><div><br></div><div>




<span></span>





<p class=3D"m_2612803408380067011gmail-p1" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(81=
,182,255);background-color:rgb(0,0,0)">#include <span class=3D"m_2612803408=
380067011gmail-s1" style=3D"color:rgb(180,38,26)">&lt;cassert&gt;</span></p=
>
<p class=3D"m_2612803408380067011gmail-p2" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(18=
0,38,26);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011g=
mail-s2" style=3D"color:rgb(81,182,255)">#include </span>&lt;iostream&gt;</=
p>
<p class=3D"m_2612803408380067011gmail-p1" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(81=
,182,255);background-color:rgb(0,0,0)">#include <span class=3D"m_2612803408=
380067011gmail-s1" style=3D"color:rgb(180,38,26)">&lt;map&gt;</span></p>
<p class=3D"m_2612803408380067011gmail-p1" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(81=
,182,255);background-color:rgb(0,0,0)">#include <span class=3D"m_2612803408=
380067011gmail-s1" style=3D"color:rgb(180,38,26)">&lt;vector&gt;</span></p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p4" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(25=
5,178,109);background-color:rgb(0,0,0)">using<span class=3D"m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"> </span>namespace<span cla=
ss=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)"> std;=
</span></p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s4" style=3D"color:rgb(255,178,109)">using</span> Intervals =3D map&=
lt;<span class=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178=
,109)">float</span>,<span class=3D"m_2612803408380067011gmail-s4" style=3D"=
color:rgb(255,178,109)">int</span>&gt;;</p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s4" style=3D"color:rgb(255,178,109)">int</span> sample(Intervals <sp=
an class=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)"=
>const</span> &amp; intervals, <span class=3D"m_2612803408380067011gmail-s4=
" style=3D"color:rgb(255,178,109)">float</span> randomFloat) {</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>assert(!intervals.empty(=
));</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">auto</span> it =3D i=
ntervals.lower_bound(randomFloat);</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">if</span>(it =3D=3D =
intervals.end()) {</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// =
we return the last element of the map:</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span clas=
s=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">return=
</span> crbegin(intervals)-&gt;second;</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// =
this is what I would prefer writing:</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// =
return intervals.back();</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>}</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">return</span> it-&gt=
;second;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)">}</p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s4" style=3D"color:rgb(255,178,109)">int</span> main() {</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// The goal is to=
 sample among ints from 0 to 9, with different probability for each int.</p=
>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// The probabilit=
y of i is proba[i].</p>
<p class=3D"m_2612803408380067011gmail-p7" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(21=
2,212,212);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_26128034083800=
67011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>std::array&lt;</span=
><span class=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,1=
09)">float</span><span class=3D"m_2612803408380067011gmail-s3" style=3D"col=
or:rgb(231,254,226)">,</span>10<span class=3D"m_2612803408380067011gmail-s3=
" style=3D"color:rgb(231,254,226)">&gt; proba {{</span>0.01<span class=3D"m=
_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.05=
<span class=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,22=
6)">,</span>0.01<span class=3D"m_2612803408380067011gmail-s3" style=3D"colo=
r:rgb(231,254,226)">,</span>0.15<span class=3D"m_2612803408380067011gmail-s=
3" style=3D"color:rgb(231,254,226)">,</span>0.1<span class=3D"m_26128034083=
80067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.19<span class=
=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span=
>0.19<span class=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,2=
54,226)">,</span>0.1<span class=3D"m_2612803408380067011gmail-s3" style=3D"=
color:rgb(231,254,226)">,</span>0.19<span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)">,</span>0.01<span class=3D"m_261280=
3408380067011gmail-s3" style=3D"color:rgb(231,254,226)">}};</span></p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// The sum of pro=
babilities are 1, so we can conceptually partition</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// the [0,1] inte=
rval in 10 intervals of lengths proba[i].</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"m_2612803=
408380067011gmail-s5" style=3D"color:rgb(0,193,0)">//</span></p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// we only need t=
o store the upper bounds of these intervals,</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// because the lo=
wer bound of interval n is the upper bound of interval n-1,</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// and because we=
 know that the first lower bound is 0:</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>Intervals intervals;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">int</span> i =3D <sp=
an class=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)"=
>0</span>;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">float</span> upperbo=
und =3D <span class=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(21=
2,212,212)">0.f</span>;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">for</span>(<span cla=
ss=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">auto<=
/span> p : proba) {</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>upperbound=
 +=3D p;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>intervals.=
emplace(upperbound,i);</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// =
Note that the last key, due to floating point rounding errors, will be</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// =
close to 1.f but not necessarilly 1.f.</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>++i;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>}</p>
<p class=3D"m_2612803408380067011gmail-p8" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0);min-height:14px"><span class=3D"m_26=
12803408380067011gmail-Apple-converted-space">=C2=A0=C2=A0 =C2=A0</span></p=
>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// Then, to sampl=
e ints, we generate a random numbers between 0 and 1,</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>// and lookup the=
 correponding interval:</p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">for</span>(<span cla=
ss=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">auto<=
/span> v : vector&lt;<span class=3D"m_2612803408380067011gmail-s4" style=3D=
"color:rgb(255,178,109)">float</span>&gt;{{</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.f</s=
pan>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.0000=
1f</span>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.02f<=
/span>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.5f</=
span>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.98f<=
/span>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">1.f</s=
pan>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">1.0000=
1f</span>}})</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>{</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>cout &lt;&=
lt; sample(intervals, v) &lt;&lt; endl;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>}</p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p4" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(25=
5,178,109);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_26128034083800=
67011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span></span>return<span c=
lass=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)"> </=
span><span class=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,2=
12,212)">0</span><span class=3D"m_2612803408380067011gmail-s3" style=3D"col=
or:rgb(231,254,226)">;</span></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)">}</p><p class=3D"m_2612803408380067=
011gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-e=
ast-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line=
-height:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rg=
b(0,0,0)"><br></p>


</div><div><br></div><div>Olivier</div><div><br></div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr">Le=C2=A0mer. 18 juil. 2018 =C3=A0=C2=A019=
:53, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com" target=3D"_bl=
ank">hodges.r@gmail.com</a>&gt; a =C3=A9crit=C2=A0:<br></div><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;p=
adding-left:1ex"><div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr">On Wed, 18 Jul 2018 at 14:10, Olivier Sohn &lt;<a href=3D"mailto=
:olivier.sohn@gmail.com" target=3D"_blank">olivier.sohn@gmail.com</a>&gt; w=
rote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hello,<div><=
br></div><div>I recently encountered an algorithm where I needed to access =
the map&#39;s last element, knowing the map was non-empty. But I found that=
 there is no straightforward way to access the last (or first) element of a=
 map.</div></div></blockquote><div><br></div><div>Without wishing to sound =
too controversial, I am wondering whether this algorithm could have been ex=
pressed in a different way.</div><div><br></div><div>Wanting a reference to=
 the &quot;back&quot; of a container suggests a need to access the least/gr=
eatest/earliest/latest of a group of things.=C2=A0</div><div><br></div><div=
>Are you able to share the motivating use case, as there are other tools fo=
r this in the standard library?<br></div><div>=C2=A0</div><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex"><div dir=3D"ltr"><div><br></div><div>So as suggested by this =
process :=C2=A0<a href=3D"https://isocpp.org/std/submit-a-proposal" target=
=3D"_blank">https://isocpp.org/std/submit-a-proposal</a> I&#39;d like to pr=
opose the following: add the front() and back() methods, with const and non=
 const versions, to std::(multi)map and std::(multi)set, to return a (const=
-) reference to the last / first element(s).</div><div><br></div><div>Like =
with other containers implementing these methods, no guaranty would be give=
n when the container is empty.</div><div><br></div><div>What do you think?<=
/div><div><br></div><div>Cheers,</div><div>Olivier</div><div><br></div></di=
v>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-=
45df-9536-e0bfab52962b%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3=
ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfooter"=
 target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-propo=
sals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com</=
a>.<br>
</blockquote></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsi=
bvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfoo=
ter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-p=
roposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gma=
il.com</a>.<br>
</blockquote></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPU=
Wj%3DvBNSJCx7-pDfYuA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3haNKo=
0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail.gmail.com</a>.<br />

--0000000000004838ab05715889b4--

.


Author: "Olivier S." <olivier.sohn@gmail.com>
Date: Thu, 19 Jul 2018 14:19:56 +0200
Raw View
--Apple-Mail-0BE7AA43-7E37-4182-8C93-718E9FAB5F8B
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

That would work too but if we want to sample some strings instead if ints t=
hen we have to use a map. (Or a vector of pairs but that complicates the im=
plementation a bit)

Olivier.=20

> On 19 juil. 2018, at 13:32, Richard Hodges <hodges.r@gmail.com> wrote:
>=20
>=20
>=20
>> On Thu, 19 Jul 2018 at 10:11, Olivier S. <olivier.sohn@gmail.com> wrote:
>> Hello,
>>=20
>> This is the motivating example:
>=20
> I think I'd be inclined to transform the probabilities into an array of l=
imits and perform the upper_bound on that:
>=20
> //
> // Created by rhodges on 19/07/18.
> //
>=20
> #include <cassert>
> #include <iostream>
> #include <vector>
> #include <algorithm>
> #include <iterator>
> #include <type_traits>
> #include <array>
>=20
>=20
> /// Convert a container of probabilities into a container of ascending in=
terval limits
> template<class Container>
> auto to_limits(Container container) {
>     using container_type =3D std::decay_t<Container>;
>     using value_type =3D typename container_type::value_type;
>     auto to_total =3D [total =3D value_type(0)](auto &&x) mutable {
>         total +=3D x;
>         return total;
>     };
>=20
>     std::transform(std::begin(container), std::end(container),
>                    std::begin(container),
>                    to_total);
>=20
>     return container;
> };
>=20
> template<class Container>
> struct assert_not_empty {
>     assert_not_empty(Container const &container) noexcept {
>         assert(!container.empty());
>     }
> };
>=20
> template<class Type, std::size_t N>
> struct assert_not_empty<std::array<Type, N>> {
>     constexpr assert_not_empty(std::array<Type, N> const &container) noex=
cept {
>         static_assert(!container.empty());
>     }
> };
>=20
>=20
> /// given an ascending container of limits, choose one with probability e=
qual to 1.0 / max(limits)
> /// @returns an integer containing the index of the chosen limit
> template<class Container, class Value>
> int sample_limits(Container &&totals, Value &&prob) {
>=20
>     // precondition
>     assert_not_empty{totals};
>=20
>     // convert prob (range 0-1) into a total.
>=20
>     auto target =3D prob * totals.back();
>=20
>     auto cap =3D [&totals](auto iter) {
>         if (iter =3D=3D std::end(totals))
>             return std::prev(iter);
>         else
>             return iter;
>     };
>=20
>     auto first =3D std::cbegin(totals);
>     auto last =3D std::cend(totals);
>=20
>     auto index =3D std::distance(first,
>                                cap(std::lower_bound(first,
>                                                     last,
>                                                     target)));
>     return int(index);
> }
>=20
> int main() {
>=20
>     // The goal is to sample among ints from 0 to 9, with different proba=
bility for each int.
>     // The probability of i is proba[i] / sum(proba).
>     std::array<float, 10> proba{{0.01, 0.05, 0.01, 0.15, 0.1, 0.19, 0.19,=
 0.1, 0.19, 0.01}};
>     auto limits =3D to_limits(proba);
>     for (auto v : std::vector<float>{{
>                                              0.f,
>                                              0.00001f,
>                                              0.02f,
>                                              0.5f,
>                                              0.98f,
>                                              1.f,  // on upper bound
>                                              1.00001f  // beyond upper bo=
und
>                                      }}) {
>         std::cout << sample_limits(limits, v) << std::endl;
>     }
>    =20
>     return 0;
> }
>=20
>=20
> =20
>>=20
>> #include <cassert>
>> #include <iostream>
>> #include <map>
>> #include <vector>
>>=20
>> using namespace std;
>>=20
>> using Intervals =3D map<float,int>;
>>=20
>> int sample(Intervals const & intervals, float randomFloat) {
>>     assert(!intervals.empty());
>>     auto it =3D intervals.lower_bound(randomFloat);
>>     if(it =3D=3D intervals.end()) {
>>         // we return the last element of the map:
>>         return crbegin(intervals)->second;
>>         // this is what I would prefer writing:
>>         // return intervals.back();
>>     }
>>     return it->second;
>> }
>>=20
>> int main() {
>>     // The goal is to sample among ints from 0 to 9, with different prob=
ability for each int.
>>=20
>>     // The probability of i is proba[i].
>>     std::array<float,10> proba {{0.01,0.05,0.01,0.15,0.1,0.19,0.19,0.1,0=
..19,0.01}};
>>=20
>>     // The sum of probabilities are 1, so we can conceptually partition
>>     // the [0,1] interval in 10 intervals of lengths proba[i].
>>     //
>>     // we only need to store the upper bounds of these intervals,
>>     // because the lower bound of interval n is the upper bound of inter=
val n-1,
>>     // and because we know that the first lower bound is 0:
>>     Intervals intervals;
>>     int i =3D 0;
>>     float upperbound =3D 0.f;
>>     for(auto p : proba) {
>>         upperbound +=3D p;
>>         intervals.emplace(upperbound,i);
>>         // Note that the last key, due to floating point rounding errors=
, will be
>>         // close to 1.f but not necessarilly 1.f.
>>         ++i;
>>     }
>>    =20
>>     // Then, to sample ints, we generate a random numbers between 0 and =
1,
>>     // and lookup the correponding interval:
>>=20
>>     for(auto v : vector<float>{{
>>         0.f,
>>         0.00001f,
>>         0.02f,
>>         0.5f,
>>         0.98f,
>>         1.f,
>>         1.00001f}})
>>     {
>>         cout << sample(intervals, v) << endl;
>>     }
>>=20
>>     return 0;
>> }
>>=20
>>=20
>> Olivier
>>=20
>>=20
>>> Le mer. 18 juil. 2018 =C3=A0 19:53, Richard Hodges <hodges.r@gmail.com>=
 a =C3=A9crit :
>>>=20
>>>=20
>>>> On Wed, 18 Jul 2018 at 14:10, Olivier Sohn <olivier.sohn@gmail.com> wr=
ote:
>>>> Hello,
>>>>=20
>>>> I recently encountered an algorithm where I needed to access the map's=
 last element, knowing the map was non-empty. But I found that there is no =
straightforward way to access the last (or first) element of a map.
>>>=20
>>> Without wishing to sound too controversial, I am wondering whether this=
 algorithm could have been expressed in a different way.
>>>=20
>>> Wanting a reference to the "back" of a container suggests a need to acc=
ess the least/greatest/earliest/latest of a group of things.=20
>>>=20
>>> Are you able to share the motivating use case, as there are other tools=
 for this in the standard library?
>>> =20
>>>>=20
>>>> So as suggested by this process : https://isocpp.org/std/submit-a-prop=
osal I'd like to propose the following: add the front() and back() methods,=
 with const and non const versions, to std::(multi)map and std::(multi)set,=
 to return a (const-) reference to the last / first element(s).
>>>>=20
>>>> Like with other containers implementing these methods, no guaranty wou=
ld be given when the container is empty.
>>>>=20
>>>> What do you think?
>>>>=20
>>>> Cheers,
>>>> Olivier
>>>>=20
>>>> --=20
>>>> You received this message because you are subscribed to the Google Gro=
ups "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.
>>>> To view this discussion on the web visit https://groups.google.com/a/i=
socpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%40isoc=
pp.org.
>>>=20
>>> --=20
>>> You received this message because you are subscribed to a topic in the =
Google Groups "ISO C++ Standard - Future Proposals" group.
>>> To unsubscribe from this topic, visit https://groups.google.com/a/isocp=
p.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to std=
-proposals+unsubscribe@isocpp.org.
>>> To post to this group, send email to std-proposals@isocpp.org.
>>> To view this discussion on the web visit https://groups.google.com/a/is=
ocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_=
DXX7cfQ%40mail.gmail.com.
>>=20
>> --=20
>> You received this message because you are subscribed to the Google Group=
s "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n email to std-proposals+unsubscribe@isocpp.org.
>> To post to this group, send email to std-proposals@isocpp.org.
>> To view this discussion on the web visit https://groups.google.com/a/iso=
cpp.org/d/msgid/std-proposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S=
8%3D%2Bzeg%40mail.gmail.com.
>=20
> --=20
> You received this message because you are subscribed to a topic in the Go=
ogle Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.=
org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to std-p=
roposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> To view this discussion on the web visit https://groups.google.com/a/isoc=
pp.org/d/msgid/std-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNS=
JCx7-pDfYuA%40mail.gmail.com.

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/583D12C6-E7F5-4C90-AD0E-CFEAC5F95CD6%40gmail.com=
..

--Apple-Mail-0BE7AA43-7E37-4182-8C93-718E9FAB5F8B
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=
=3Dutf-8"></head><body dir=3D"auto"><div></div><div>That would work too but=
 if we want to sample some strings instead if ints then we have to use a ma=
p. (Or a vector of pairs but that complicates the implementation a bit)</di=
v><div><br></div><div>Olivier.&nbsp;</div><div><br>On 19 juil. 2018, at 13:=
32, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com">hodges.r@gmail=
..com</a>&gt; wrote:<br><br></div><blockquote type=3D"cite"><div><div dir=3D=
"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Thu, 19 Jul 20=
18 at 10:11, Olivier S. &lt;<a href=3D"mailto:olivier.sohn@gmail.com">olivi=
er.sohn@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote"=
 style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><d=
iv dir=3D"ltr">Hello,<div><br></div><div>This is the motivating example:</d=
iv></div></blockquote><div><br></div><div>I think I'd be inclined to transf=
orm the probabilities into an array of limits and perform the upper_bound o=
n that:</div><div><br></div><div><pre style=3D"background-color:rgb(43,43,4=
3);color:rgb(169,183,198);font-family:&quot;DejaVu Sans Mono&quot;;font-siz=
e:11.3pt"><span style=3D"color:rgb(128,128,128)">//<br></span><span style=
=3D"color:rgb(128,128,128)">// Created by rhodges on 19/07/18.<br></span><s=
pan style=3D"color:rgb(128,128,128)">//<br></span><span style=3D"color:rgb(=
128,128,128)"><br></span><span style=3D"color:rgb(187,181,41)">#include </s=
pan><span style=3D"color:rgb(106,135,89)">&lt;cassert&gt;<br></span><span s=
tyle=3D"color:rgb(187,181,41)">#include </span><span style=3D"color:rgb(106=
,135,89)">&lt;iostream&gt;<br></span><span style=3D"color:rgb(187,181,41)">=
#include </span><span style=3D"color:rgb(106,135,89)">&lt;vector&gt;<br></s=
pan><span style=3D"color:rgb(187,181,41)">#include </span><span style=3D"co=
lor:rgb(106,135,89)">&lt;algorithm&gt;<br></span><span style=3D"color:rgb(1=
87,181,41)">#include </span><span style=3D"color:rgb(106,135,89)">&lt;itera=
tor&gt;<br></span><span style=3D"color:rgb(187,181,41)">#include </span><sp=
an style=3D"color:rgb(106,135,89)">&lt;type_traits&gt;<br></span><span styl=
e=3D"color:rgb(187,181,41)">#include </span><span style=3D"color:rgb(106,13=
5,89)">&lt;array&gt;<br></span><span style=3D"color:rgb(106,135,89)"><br></=
span><span style=3D"color:rgb(106,135,89)"><br></span><span style=3D"color:=
rgb(128,128,128)">/// Convert a container of probabilities into a container=
 of ascending interval limits<br></span><span style=3D"color:rgb(204,120,50=
);font-weight:bold">template</span>&lt;<span style=3D"color:rgb(204,120,50)=
;font-weight:bold">class </span><span style=3D"color:rgb(185,188,209)">Cont=
ainer</span>&gt;<br><span style=3D"color:rgb(204,120,50);font-weight:bold">=
auto </span><span style=3D"color:rgb(255,198,109)">to_limits</span>(<span s=
tyle=3D"color:rgb(185,188,209)">Container </span>container) {<br>    <span =
style=3D"color:rgb(204,120,50);font-weight:bold">using </span><span style=
=3D"color:rgb(185,188,209)">container_type </span>=3D <span style=3D"color:=
rgb(181,182,227)">std</span>::<span style=3D"color:rgb(185,188,209)">decay_=
t</span>&lt;<span style=3D"color:rgb(185,188,209)">Container</span>&gt;<spa=
n style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,=
120,50)">    </span><span style=3D"color:rgb(204,120,50);font-weight:bold">=
using </span><span style=3D"color:rgb(185,188,209)">value_type </span>=3D <=
span style=3D"color:rgb(204,120,50);font-weight:bold">typename </span><span=
 style=3D"color:rgb(185,188,209)">container_type</span>::<span style=3D"col=
or:rgb(185,188,209)">value_type</span><span style=3D"color:rgb(204,120,50)"=
>;<br></span><span style=3D"color:rgb(204,120,50)">    </span><span style=
=3D"color:rgb(204,120,50);font-weight:bold">auto </span>to_total =3D [total=
 =3D <span style=3D"color:rgb(185,188,209)">value_type</span>(<span style=
=3D"color:rgb(104,151,187)">0</span>)](<span style=3D"color:rgb(204,120,50)=
;font-weight:bold">auto </span>&amp;&amp;x) <span style=3D"color:rgb(204,12=
0,50);font-weight:bold">mutable </span>{<br>        total +=3D x<span style=
=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)=
">        </span><span style=3D"color:rgb(204,120,50);font-weight:bold">ret=
urn </span>total<span style=3D"color:rgb(204,120,50)">;<br></span><span sty=
le=3D"color:rgb(204,120,50)">    </span>}<span style=3D"color:rgb(204,120,5=
0)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><span styl=
e=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(181,182,227=
)">std</span>::transform(<span style=3D"color:rgb(181,182,227)">std</span>:=
:begin(container)<span style=3D"color:rgb(204,120,50)">, </span><span style=
=3D"color:rgb(181,182,227)">std</span>::end(container)<span style=3D"color:=
rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">        =
           </span><span style=3D"color:rgb(181,182,227)">std</span>::begin(=
container)<span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"=
color:rgb(204,120,50)">                   </span>to_total)<span style=3D"co=
lor:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br>=
</span><span style=3D"color:rgb(204,120,50)">    </span><span style=3D"colo=
r:rgb(204,120,50);font-weight:bold">return </span>container<span style=3D"c=
olor:rgb(204,120,50)">;<br></span>}<span style=3D"color:rgb(204,120,50)">;<=
br></span><span style=3D"color:rgb(204,120,50)"><br></span><span style=3D"c=
olor:rgb(204,120,50);font-weight:bold">template</span>&lt;<span style=3D"co=
lor:rgb(204,120,50);font-weight:bold">class </span><span style=3D"color:rgb=
(185,188,209)">Container</span>&gt;<br><span style=3D"color:rgb(204,120,50)=
;font-weight:bold">struct </span><span style=3D"color:rgb(181,182,227)">ass=
ert_not_empty </span>{<br>    <span style=3D"color:rgb(255,198,109)">assert=
_not_empty</span>(<span style=3D"color:rgb(185,188,209)">Container </span><=
span style=3D"color:rgb(204,120,50);font-weight:bold">const </span>&amp;con=
tainer) <span style=3D"color:rgb(204,120,50);font-weight:bold">noexcept </s=
pan>{<br>        <span style=3D"color:rgb(144,139,37)">assert</span>(!conta=
iner.<span style=3D"color:rgb(152,118,170);font-style:italic">empty</span>(=
))<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rg=
b(204,120,50)">    </span>}<br>}<span style=3D"color:rgb(204,120,50)">;<br>=
</span><span style=3D"color:rgb(204,120,50)"><br></span><span style=3D"colo=
r:rgb(204,120,50);font-weight:bold">template</span>&lt;<span style=3D"color=
:rgb(204,120,50);font-weight:bold">class </span><span style=3D"color:rgb(18=
5,188,209)">Type</span><span style=3D"color:rgb(204,120,50)">, </span><span=
 style=3D"color:rgb(181,182,227)">std</span>::<span style=3D"color:rgb(185,=
188,209)">size_t </span><span style=3D"color:rgb(152,118,170);font-style:it=
alic">N</span>&gt;<br><span style=3D"color:rgb(204,120,50);font-weight:bold=
">struct </span><span style=3D"color:rgb(181,182,227)">assert_not_empty</sp=
an>&lt;<span style=3D"color:rgb(181,182,227)">std</span>::<span style=3D"co=
lor:rgb(181,182,227)">array</span>&lt;<span style=3D"color:rgb(185,188,209)=
">Type</span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"=
color:rgb(152,118,170);font-style:italic">N</span>&gt;&gt; {<br>    <span s=
tyle=3D"color:rgb(204,120,50);font-weight:bold">constexpr </span><span styl=
e=3D"color:rgb(255,198,109)">assert_not_empty</span>(<span style=3D"color:r=
gb(181,182,227)">std</span>::<span style=3D"color:rgb(181,182,227)">array</=
span>&lt;<span style=3D"color:rgb(185,188,209)">Type</span><span style=3D"c=
olor:rgb(204,120,50)">, </span><span style=3D"color:rgb(152,118,170);font-s=
tyle:italic">N</span>&gt; <span style=3D"color:rgb(204,120,50);font-weight:=
bold">const </span>&amp;container) <span style=3D"color:rgb(204,120,50);fon=
t-weight:bold">noexcept </span>{<br>        <span style=3D"color:rgb(204,12=
0,50);font-weight:bold">static_assert</span>(!container.empty())<span style=
=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)=
">    </span>}<br>}<span style=3D"color:rgb(204,120,50)">;<br></span><span =
style=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120=
,50)"><br></span><span style=3D"color:rgb(128,128,128)">/// given an ascend=
ing container of limits, choose one with probability equal to 1.0 / max(lim=
its)<br></span><span style=3D"color:rgb(128,128,128)">/// @returns an integ=
er containing the index of the chosen limit<br></span><span style=3D"color:=
rgb(204,120,50);font-weight:bold">template</span>&lt;<span style=3D"color:r=
gb(204,120,50);font-weight:bold">class </span><span style=3D"color:rgb(185,=
188,209)">Container</span><span style=3D"color:rgb(204,120,50)">, </span><s=
pan style=3D"color:rgb(204,120,50);font-weight:bold">class </span><span sty=
le=3D"color:rgb(185,188,209)">Value</span>&gt;<br><span style=3D"color:rgb(=
204,120,50);font-weight:bold">int </span><span style=3D"color:rgb(255,198,1=
09)">sample_limits</span>(<span style=3D"color:rgb(185,188,209)">Container =
</span>&amp;&amp;totals<span style=3D"color:rgb(204,120,50)">, </span><span=
 style=3D"color:rgb(185,188,209)">Value </span>&amp;&amp;prob) {<br><br>   =
 <span style=3D"color:rgb(128,128,128)">// precondition<br></span><span sty=
le=3D"color:rgb(128,128,128)">    </span>assert_not_empty{totals}<span styl=
e=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50=
)"><br></span><span style=3D"color:rgb(204,120,50)">    </span><span style=
=3D"color:rgb(128,128,128)">// convert prob (range 0-1) into a total.<br></=
span><span style=3D"color:rgb(128,128,128)"><br></span><span style=3D"color=
:rgb(128,128,128)">    </span><span style=3D"color:rgb(204,120,50);font-wei=
ght:bold">auto </span>target =3D prob * totals.<span style=3D"color:rgb(152=
,118,170);font-style:italic">back</span>()<span style=3D"color:rgb(204,120,=
50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><span sty=
le=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50=
);font-weight:bold">auto </span>cap =3D [&amp;totals](<span style=3D"color:=
rgb(204,120,50);font-weight:bold">auto </span>iter) {<br>        <span styl=
e=3D"color:rgb(204,120,50);font-weight:bold">if </span>(iter =3D=3D <span s=
tyle=3D"color:rgb(181,182,227)">std</span>::end(totals))<br>            <sp=
an style=3D"color:rgb(204,120,50);font-weight:bold">return </span><span sty=
le=3D"color:rgb(181,182,227)">std</span>::prev(iter)<span style=3D"color:rg=
b(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">        </=
span><span style=3D"color:rgb(204,120,50);font-weight:bold">else<br></span>=
<span style=3D"color:rgb(204,120,50);font-weight:bold">            return <=
/span>iter<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"=
color:rgb(204,120,50)">    </span>}<span style=3D"color:rgb(204,120,50)">;<=
br></span><span style=3D"color:rgb(204,120,50)"><br></span><span style=3D"c=
olor:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50);font-=
weight:bold">auto </span>first =3D <span style=3D"color:rgb(181,182,227)">s=
td</span>::cbegin(totals)<span style=3D"color:rgb(204,120,50)">;<br></span>=
<span style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(2=
04,120,50);font-weight:bold">auto </span>last =3D <span style=3D"color:rgb(=
181,182,227)">std</span>::cend(totals)<span style=3D"color:rgb(204,120,50)"=
>;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><span style=
=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50);=
font-weight:bold">auto </span>index =3D <span style=3D"color:rgb(181,182,22=
7)">std</span>::distance(first<span style=3D"color:rgb(204,120,50)">,<br></=
span><span style=3D"color:rgb(204,120,50)">                               <=
/span>cap(<span style=3D"color:rgb(181,182,227)">std</span>::lower_bound(fi=
rst<span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:r=
gb(204,120,50)">                                                    </span>=
last<span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:=
rgb(204,120,50)">                                                    </span=
>target)))<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"=
color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50);font=
-weight:bold">return int</span>(index)<span style=3D"color:rgb(204,120,50)"=
>;<br></span>}<br><br><span style=3D"color:rgb(204,120,50);font-weight:bold=
">int </span><span style=3D"color:rgb(255,198,109)">main</span>() {<br><br>=
    <span style=3D"color:rgb(128,128,128)">// The goal is to sample among i=
nts from 0 to 9, with different probability for each int.<br></span><span s=
tyle=3D"color:rgb(128,128,128)">    // The probability of i is proba[i] / s=
um(proba).<br></span><span style=3D"color:rgb(128,128,128)">    </span><spa=
n style=3D"color:rgb(181,182,227)">std</span>::<span style=3D"color:rgb(181=
,182,227)">array</span>&lt;<span style=3D"color:rgb(204,120,50);font-weight=
:bold">float</span><span style=3D"color:rgb(204,120,50)">, </span><span sty=
le=3D"color:rgb(104,151,187)">10</span>&gt; proba{{<span style=3D"color:rgb=
(104,151,187)">0.01</span><span style=3D"color:rgb(204,120,50)">, </span><s=
pan style=3D"color:rgb(104,151,187)">0.05</span><span style=3D"color:rgb(20=
4,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.01</span><span=
 style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,=
187)">0.15</span><span style=3D"color:rgb(204,120,50)">, </span><span style=
=3D"color:rgb(104,151,187)">0.1</span><span style=3D"color:rgb(204,120,50)"=
>, </span><span style=3D"color:rgb(104,151,187)">0.19</span><span style=3D"=
color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.19=
</span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:=
rgb(104,151,187)">0.1</span><span style=3D"color:rgb(204,120,50)">, </span>=
<span style=3D"color:rgb(104,151,187)">0.19</span><span style=3D"color:rgb(=
204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.01</span>}}<=
span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(2=
04,120,50)">    </span><span style=3D"color:rgb(204,120,50);font-weight:bol=
d">auto </span>limits =3D to_limits(proba)<span style=3D"color:rgb(204,120,=
50)">;<br></span><span style=3D"color:rgb(204,120,50)">    </span><span sty=
le=3D"color:rgb(204,120,50);font-weight:bold">for </span>(<span style=3D"co=
lor:rgb(204,120,50);font-weight:bold">auto </span>v : <span style=3D"color:=
rgb(181,182,227)">std</span>::vector&lt;<span style=3D"color:rgb(204,120,50=
);font-weight:bold">float</span>&gt;{{<br>                                 =
            <span style=3D"color:rgb(104,151,187)">0.f</span><span style=3D=
"color:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)"> =
                                            </span><span style=3D"color:rgb=
(104,151,187)">0.00001f</span><span style=3D"color:rgb(204,120,50)">,<br></=
span><span style=3D"color:rgb(204,120,50)">                                =
             </span><span style=3D"color:rgb(104,151,187)">0.02f</span><spa=
n style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,=
120,50)">                                             </span><span style=3D=
"color:rgb(104,151,187)">0.5f</span><span style=3D"color:rgb(204,120,50)">,=
<br></span><span style=3D"color:rgb(204,120,50)">                          =
                   </span><span style=3D"color:rgb(104,151,187)">0.98f</spa=
n><span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:rg=
b(204,120,50)">                                             </span><span st=
yle=3D"color:rgb(104,151,187)">1.f</span><span style=3D"color:rgb(204,120,5=
0)">,  </span><span style=3D"color:rgb(128,128,128)">// on upper bound<br><=
/span><span style=3D"color:rgb(128,128,128)">                              =
               </span><span style=3D"color:rgb(104,151,187)">1.00001f  </sp=
an><span style=3D"color:rgb(128,128,128)">// beyond upper bound<br></span><=
span style=3D"color:rgb(128,128,128)">                                     =
</span>}}) {<br>        <span style=3D"color:rgb(181,182,227)">std</span>::=
cout <span style=3D"color:rgb(95,140,138)">&lt;&lt; </span>sample_limits(li=
mits<span style=3D"color:rgb(204,120,50)">, </span>v) <span style=3D"color:=
rgb(95,140,138)">&lt;&lt; </span><span style=3D"color:rgb(181,182,227)">std=
</span>::endl<span style=3D"color:rgb(204,120,50)">;<br></span><span style=
=3D"color:rgb(204,120,50)">    </span>}<br>    <br>    <span style=3D"color=
:rgb(204,120,50);font-weight:bold">return </span><span style=3D"color:rgb(1=
04,151,187)">0</span><span style=3D"color:rgb(204,120,50)">;<br></span>}<br=
><br></pre><br></div><div>&nbsp;</div><blockquote class=3D"gmail_quote" sty=
le=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div d=
ir=3D"ltr"><div><br></div><div>




<span></span>





<p class=3D"m_2612803408380067011gmail-p1" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(81=
,182,255);background-color:rgb(0,0,0)">#include <span class=3D"m_2612803408=
380067011gmail-s1" style=3D"color:rgb(180,38,26)">&lt;cassert&gt;</span></p=
>
<p class=3D"m_2612803408380067011gmail-p2" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(18=
0,38,26);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011g=
mail-s2" style=3D"color:rgb(81,182,255)">#include </span>&lt;iostream&gt;</=
p>
<p class=3D"m_2612803408380067011gmail-p1" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(81=
,182,255);background-color:rgb(0,0,0)">#include <span class=3D"m_2612803408=
380067011gmail-s1" style=3D"color:rgb(180,38,26)">&lt;map&gt;</span></p>
<p class=3D"m_2612803408380067011gmail-p1" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(81=
,182,255);background-color:rgb(0,0,0)">#include <span class=3D"m_2612803408=
380067011gmail-s1" style=3D"color:rgb(180,38,26)">&lt;vector&gt;</span></p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p4" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(25=
5,178,109);background-color:rgb(0,0,0)">using<span class=3D"m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"> </span>namespace<span cla=
ss=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)"> std;=
</span></p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s4" style=3D"color:rgb(255,178,109)">using</span> Intervals =3D map&=
lt;<span class=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178=
,109)">float</span>,<span class=3D"m_2612803408380067011gmail-s4" style=3D"=
color:rgb(255,178,109)">int</span>&gt;;</p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s4" style=3D"color:rgb(255,178,109)">int</span> sample(Intervals <sp=
an class=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)"=
>const</span> &amp; intervals, <span class=3D"m_2612803408380067011gmail-s4=
" style=3D"color:rgb(255,178,109)">float</span> randomFloat) {</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span>assert(!intervals.empty(=
));</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">auto</span> it =3D i=
ntervals.lower_bound(randomFloat);</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">if</span>(it =3D=3D =
intervals.end()) {</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span></span>// =
we return the last element of the map:</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span><span clas=
s=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">return=
</span> crbegin(intervals)-&gt;second;</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span></span>// =
this is what I would prefer writing:</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span></span>// =
return intervals.back();</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span>}</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">return</span> it-&gt=
;second;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)">}</p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s4" style=3D"color:rgb(255,178,109)">int</span> main() {</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>// The goal is to=
 sample among ints from 0 to 9, with different probability for each int.</p=
>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>// The probabilit=
y of i is proba[i].</p>
<p class=3D"m_2612803408380067011gmail-p7" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(21=
2,212,212);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_26128034083800=
67011gmail-Apple-converted-space">&nbsp; &nbsp; </span>std::array&lt;</span=
><span class=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,1=
09)">float</span><span class=3D"m_2612803408380067011gmail-s3" style=3D"col=
or:rgb(231,254,226)">,</span>10<span class=3D"m_2612803408380067011gmail-s3=
" style=3D"color:rgb(231,254,226)">&gt; proba {{</span>0.01<span class=3D"m=
_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.05=
<span class=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,22=
6)">,</span>0.01<span class=3D"m_2612803408380067011gmail-s3" style=3D"colo=
r:rgb(231,254,226)">,</span>0.15<span class=3D"m_2612803408380067011gmail-s=
3" style=3D"color:rgb(231,254,226)">,</span>0.1<span class=3D"m_26128034083=
80067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.19<span class=
=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span=
>0.19<span class=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,2=
54,226)">,</span>0.1<span class=3D"m_2612803408380067011gmail-s3" style=3D"=
color:rgb(231,254,226)">,</span>0.19<span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)">,</span>0.01<span class=3D"m_261280=
3408380067011gmail-s3" style=3D"color:rgb(231,254,226)">}};</span></p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>// The sum of pro=
babilities are 1, so we can conceptually partition</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>// the [0,1] inte=
rval in 10 intervals of lengths proba[i].</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class=3D"m_2612803=
408380067011gmail-s5" style=3D"color:rgb(0,193,0)">//</span></p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>// we only need t=
o store the upper bounds of these intervals,</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>// because the lo=
wer bound of interval n is the upper bound of interval n-1,</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>// and because we=
 know that the first lower bound is 0:</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span>Intervals intervals;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">int</span> i =3D <sp=
an class=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)"=
>0</span>;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">float</span> upperbo=
und =3D <span class=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(21=
2,212,212)">0.f</span>;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">for</span>(<span cla=
ss=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">auto<=
/span> p : proba) {</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span>upperbound=
 +=3D p;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span>intervals.=
emplace(upperbound,i);</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span></span>// =
Note that the last key, due to floating point rounding errors, will be</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span></span>// =
close to 1.f but not necessarilly 1.f.</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span>++i;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span>}</p>
<p class=3D"m_2612803408380067011gmail-p8" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0);min-height:14px"><span class=3D"m_26=
12803408380067011gmail-Apple-converted-space">&nbsp;&nbsp; &nbsp;</span></p=
>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>// Then, to sampl=
e ints, we generate a random numbers between 0 and 1,</p>
<p class=3D"m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,=
193,0);background-color:rgb(0,0,0)"><span class=3D"m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>// and lookup the=
 correponding interval:</p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span><span class=3D"m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">for</span>(<span cla=
ss=3D"m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">auto<=
/span> v : vector&lt;<span class=3D"m_2612803408380067011gmail-s4" style=3D=
"color:rgb(255,178,109)">float</span>&gt;{{</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.f</s=
pan>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.0000=
1f</span>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.02f<=
/span>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.5f</=
span>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.98f<=
/span>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">1.f</s=
pan>,</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span><span clas=
s=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">1.0000=
1f</span>}})</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span>{</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; &nbsp; &nbsp; </span>cout &lt;&=
lt; sample(intervals, v) &lt;&lt; endl;</p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-Apple-converted-space">&nbsp; &nbsp; </span>}</p>
<p class=3D"m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Helvetica;backgrou=
nd-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_2612803408380067011gmail-p4" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(25=
5,178,109);background-color:rgb(0,0,0)"><span class=3D"m_261280340838006701=
1gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_26128034083800=
67011gmail-Apple-converted-space">&nbsp; &nbsp; </span></span>return<span c=
lass=3D"m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)"> </=
span><span class=3D"m_2612803408380067011gmail-s6" style=3D"color:rgb(212,2=
12,212)">0</span><span class=3D"m_2612803408380067011gmail-s3" style=3D"col=
or:rgb(231,254,226)">;</span></p>
<p class=3D"m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant=
-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stre=
tch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(23=
1,254,226);background-color:rgb(0,0,0)">}</p><p class=3D"m_2612803408380067=
011gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-e=
ast-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line=
-height:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rg=
b(0,0,0)"><br></p>


</div><div><br></div><div>Olivier</div><div><br></div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr">Le&nbsp;mer. 18 juil. 2018 =C3=A0&nbsp;19=
:53, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com" target=3D"_bl=
ank">hodges.r@gmail.com</a>&gt; a =C3=A9crit&nbsp;:<br></div><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;p=
adding-left:1ex"><div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr">On Wed, 18 Jul 2018 at 14:10, Olivier Sohn &lt;<a href=3D"mailto=
:olivier.sohn@gmail.com" target=3D"_blank">olivier.sohn@gmail.com</a>&gt; w=
rote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hello,<div><=
br></div><div>I recently encountered an algorithm where I needed to access =
the map's last element, knowing the map was non-empty. But I found that the=
re is no straightforward way to access the last (or first) element of a map=
..</div></div></blockquote><div><br></div><div>Without wishing to sound too =
controversial, I am wondering whether this algorithm could have been expres=
sed in a different way.</div><div><br></div><div>Wanting a reference to the=
 "back" of a container suggests a need to access the least/greatest/earlies=
t/latest of a group of things.&nbsp;</div><div><br></div><div>Are you able =
to share the motivating use case, as there are other tools for this in the =
standard library?<br></div><div>&nbsp;</div><blockquote class=3D"gmail_quot=
e" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">=
<div dir=3D"ltr"><div><br></div><div>So as suggested by this process :&nbsp=
;<a href=3D"https://isocpp.org/std/submit-a-proposal" target=3D"_blank">htt=
ps://isocpp.org/std/submit-a-proposal</a> I'd like to propose the following=
: add the front() and back() methods, with const and non const versions, to=
 std::(multi)map and std::(multi)set, to return a (const-) reference to the=
 last / first element(s).</div><div><br></div><div>Like with other containe=
rs implementing these methods, no guaranty would be given when the containe=
r is empty.</div><div><br></div><div>What do you think?</div><div><br></div=
><div>Cheers,</div><div>Olivier</div><div><br></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" 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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-=
45df-9536-e0bfab52962b%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups "ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3=
ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfooter"=
 target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-propo=
sals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com</=
a>.<br>
</blockquote></div>

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" 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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsi=
bvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfoo=
ter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-p=
roposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gma=
il.com</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups "ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe">https://groups.=
google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe</a>.<=
br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+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>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPU=
Wj%3DvBNSJCx7-pDfYuA%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Df=
ooter">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3h=
aNKo0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail.gmail.com</a>.<br=
>
</div></blockquote></body></html>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7F5-4C90-AD0E-CFEAC5F95CD6%=
40gmail.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7F5-4C90-AD0E-CFEAC5F95CD6%=
40gmail.com</a>.<br />

--Apple-Mail-0BE7AA43-7E37-4182-8C93-718E9FAB5F8B--

.


Author: Richard Hodges <hodges.r@gmail.com>
Date: Thu, 19 Jul 2018 14:47:20 +0200
Raw View
--0000000000002ab940057159958f
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Thu, 19 Jul 2018 at 14:20, Olivier S. <olivier.sohn@gmail.com> wrote:

> That would work too but if we want to sample some strings instead if ints
> then we have to use a map. (Or a vector of pairs but that complicates the
> implementation a bit)
>
>
Hi Oliver,

I think the salient point is that regardless of the actual data, the only
action we are interested in is finding the index matching the selected
probability. Having done that we can use the index to find the real data in
whatever data structure is using it.

If you were using a std::multimap<float,
std::reference_wrapper<std::string>> as the totals container, that would
work too. You might have to change the call to lower_bound, that's all.

In this case you'd want to return either the iterator or the dereferenced
iterator rather than an index I guess.

It's worth remembering that since values are stored in the totals container
in ascending order, as would be in the indecies (in the given case), this
argues strongly against a map or multimap since:
1. you pay the LogN cost of insertion, which you do not with an array or
vector,
2. in the given examples, we're storing ascending indexes as the
mapped_type in the map. Since they're ascending and consecutive we may as
well use the indecies of an array and avoid paying the cost of  a lookup.

I would still go for the array version, even if I had to store a pair (or
tuple). All it would require would be a custom predicate on the lower_bound
call.


> Olivier.
>
> On 19 juil. 2018, at 13:32, Richard Hodges <hodges.r@gmail.com> wrote:
>
>
>
> On Thu, 19 Jul 2018 at 10:11, Olivier S. <olivier.sohn@gmail.com> wrote:
>
>> Hello,
>>
>> This is the motivating example:
>>
>
> I think I'd be inclined to transform the probabilities into an array of
> limits and perform the upper_bound on that:
>
> //
> // Created by rhodges on 19/07/18.
> //
>
> #include <cassert>
> #include <iostream>
> #include <vector>
> #include <algorithm>
> #include <iterator>
> #include <type_traits>
> #include <array>
>
>
> /// Convert a container of probabilities into a container of ascending in=
terval limits
> template<class Container>
> auto to_limits(Container container) {
>     using container_type =3D std::decay_t<Container>;
>     using value_type =3D typename container_type::value_type;
>     auto to_total =3D [total =3D value_type(0)](auto &&x) mutable {
>         total +=3D x;
>         return total;
>     };
>
>     std::transform(std::begin(container), std::end(container),
>                    std::begin(container),
>                    to_total);
>
>     return container;
> };
>
> template<class Container>
> struct assert_not_empty {
>     assert_not_empty(Container const &container) noexcept {
>         assert(!container.empty());
>     }
> };
>
> template<class Type, std::size_t N>
> struct assert_not_empty<std::array<Type, N>> {
>     constexpr assert_not_empty(std::array<Type, N> const &container) noex=
cept {
>         static_assert(!container.empty());
>     }
> };
>
>
> /// given an ascending container of limits, choose one with probability e=
qual to 1.0 / max(limits)
> /// @returns an integer containing the index of the chosen limit
> template<class Container, class Value>
> int sample_limits(Container &&totals, Value &&prob) {
>
>     // precondition
>     assert_not_empty{totals};
>
>     // convert prob (range 0-1) into a total.
>
>     auto target =3D prob * totals.back();
>
>     auto cap =3D [&totals](auto iter) {
>         if (iter =3D=3D std::end(totals))
>             return std::prev(iter);
>         else
>             return iter;
>     };
>
>     auto first =3D std::cbegin(totals);
>     auto last =3D std::cend(totals);
>
>     auto index =3D std::distance(first,
>                                cap(std::lower_bound(first,
>                                                     last,
>                                                     target)));
>     return int(index);
> }
>
> int main() {
>
>     // The goal is to sample among ints from 0 to 9, with different proba=
bility for each int.
>     // The probability of i is proba[i] / sum(proba).
>     std::array<float, 10> proba{{0.01, 0.05, 0.01, 0.15, 0.1, 0.19, 0.19,=
 0.1, 0.19, 0.01}};
>     auto limits =3D to_limits(proba);
>     for (auto v : std::vector<float>{{
>                                              0.f,
>                                              0.00001f,
>                                              0.02f,
>                                              0.5f,
>                                              0.98f,
>                                              1.f,  // on upper bound
>                                              1.00001f  // beyond upper bo=
und
>                                      }}) {
>         std::cout << sample_limits(limits, v) << std::endl;
>     }
>
>     return 0;
> }
>
>
>
>
>>
>> #include <cassert>
>>
>> #include <iostream>
>>
>> #include <map>
>>
>> #include <vector>
>>
>>
>> using namespace std;
>>
>>
>> using Intervals =3D map<float,int>;
>>
>>
>> int sample(Intervals const & intervals, float randomFloat) {
>>
>>     assert(!intervals.empty());
>>
>>     auto it =3D intervals.lower_bound(randomFloat);
>>
>>     if(it =3D=3D intervals.end()) {
>>
>>         // we return the last element of the map:
>>
>>         return crbegin(intervals)->second;
>>
>>         // this is what I would prefer writing:
>>
>>         // return intervals.back();
>>
>>     }
>>
>>     return it->second;
>>
>> }
>>
>>
>> int main() {
>>
>>     // The goal is to sample among ints from 0 to 9, with different
>> probability for each int.
>>
>>
>>     // The probability of i is proba[i].
>>
>>     std::array<float,10> proba {{0.01,0.05,0.01,0.15,0.1,0.19,0.19,0.1,
>> 0.19,0.01}};
>>
>>
>>     // The sum of probabilities are 1, so we can conceptually partition
>>
>>     // the [0,1] interval in 10 intervals of lengths proba[i].
>>
>>     //
>>
>>     // we only need to store the upper bounds of these intervals,
>>
>>     // because the lower bound of interval n is the upper bound of
>> interval n-1,
>>
>>     // and because we know that the first lower bound is 0:
>>
>>     Intervals intervals;
>>
>>     int i =3D 0;
>>
>>     float upperbound =3D 0.f;
>>
>>     for(auto p : proba) {
>>
>>         upperbound +=3D p;
>>
>>         intervals.emplace(upperbound,i);
>>
>>         // Note that the last key, due to floating point rounding
>> errors, will be
>>
>>         // close to 1.f but not necessarilly 1.f.
>>
>>         ++i;
>>
>>     }
>>
>>
>>
>>     // Then, to sample ints, we generate a random numbers between 0 and
>> 1,
>>
>>     // and lookup the correponding interval:
>>
>>
>>     for(auto v : vector<float>{{
>>
>>         0.f,
>>
>>         0.00001f,
>>
>>         0.02f,
>>
>>         0.5f,
>>
>>         0.98f,
>>
>>         1.f,
>>
>>         1.00001f}})
>>
>>     {
>>
>>         cout << sample(intervals, v) << endl;
>>
>>     }
>>
>>
>>     return 0;
>>
>> }
>>
>>
>>
>> Olivier
>>
>>
>> Le mer. 18 juil. 2018 =C3=A0 19:53, Richard Hodges <hodges.r@gmail.com> =
a
>> =C3=A9crit :
>>
>>>
>>>
>>> On Wed, 18 Jul 2018 at 14:10, Olivier Sohn <olivier.sohn@gmail.com>
>>> wrote:
>>>
>>>> Hello,
>>>>
>>>> I recently encountered an algorithm where I needed to access the map's
>>>> last element, knowing the map was non-empty. But I found that there is=
 no
>>>> straightforward way to access the last (or first) element of a map.
>>>>
>>>
>>> Without wishing to sound too controversial, I am wondering whether this
>>> algorithm could have been expressed in a different way.
>>>
>>> Wanting a reference to the "back" of a container suggests a need to
>>> access the least/greatest/earliest/latest of a group of things.
>>>
>>> Are you able to share the motivating use case, as there are other tools
>>> for this in the standard library?
>>>
>>>
>>>>
>>>> So as suggested by this process :
>>>> https://isocpp.org/std/submit-a-proposal I'd like to propose the
>>>> following: add the front() and back() methods, with const and non cons=
t
>>>> versions, to std::(multi)map and std::(multi)set, to return a (const-)
>>>> reference to the last / first element(s).
>>>>
>>>> Like with other containers implementing these methods, no guaranty
>>>> would be given when the container is empty.
>>>>
>>>> What do you think?
>>>>
>>>> Cheers,
>>>> Olivier
>>>>
>>>> --
>>>> 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.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-=
6337-45df-9536-e0bfab52962b%40isocpp.org
>>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e=
-6337-45df-9536-e0bfab52962b%40isocpp.org?utm_medium=3Demail&utm_source=3Df=
ooter>
>>>> .
>>>>
>>> --
>>> You received this message because you are subscribed to a topic in the
>>> Google Groups "ISO C++ Standard - Future Proposals" group.
>>> To unsubscribe from this topic, visit
>>> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSk=
Q/unsubscribe
>>> .
>>> To unsubscribe from this group and all its topics, send an email to
>>> std-proposals+unsubscribe@isocpp.org.
>>> To post to this group, send email to std-proposals@isocpp.org.
>>> To view this discussion on the web visit
>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHN=
VYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com
>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbH=
NVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Dem=
ail&utm_source=3Dfooter>
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Group=
s
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n
>> email to std-proposals+unsubscribe@isocpp.org.
>> To post to this group, send email to std-proposals@isocpp.org.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pd=
x_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gmail.com
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80t5P=
dx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=
=3Demail&utm_source=3Dfooter>
>> .
>>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/=
unsubscribe
> .
> To unsubscribe from this group and all its topics, send an email to
> std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3haNKo0=
QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail.gmail.com
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3haNKo=
0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail.gmail.com?utm_medium=
=3Demail&utm_source=3Dfooter>
> .
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7F=
5-4C90-AD0E-CFEAC5F95CD6%40gmail.com
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7=
F5-4C90-AD0E-CFEAC5F95CD6%40gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r>
> .
>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CALvx3hYvMBhNXTPdBHsLEvbvvU71azApw%2BSQiGmZNZd4p=
s%2BWhw%40mail.gmail.com.

--0000000000002ab940057159958f
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Thu=
, 19 Jul 2018 at 14:20, Olivier S. &lt;<a href=3D"mailto:olivier.sohn@gmail=
..com">olivier.sohn@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"g=
mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=
eft:1ex"><div dir=3D"auto"><div></div><div>That would work too but if we wa=
nt to sample some strings instead if ints then we have to use a map. (Or a =
vector of pairs but that complicates the implementation a bit)</div><div><b=
r></div></div></blockquote><div><br></div><div>Hi Oliver,</div><div><br></d=
iv><div>I think the salient point is that regardless of the actual data, th=
e only action we are interested in is finding the index matching the select=
ed probability. Having done that we can use the index to find the real data=
 in whatever data structure is using it.</div><div><br></div><div>If you we=
re using a std::multimap&lt;float, std::reference_wrapper&lt;std::string&gt=
;&gt; as the totals container, that would work too. You might have to chang=
e the call to lower_bound, that&#39;s all.</div><div><br></div><div>In this=
 case you&#39;d want to return either the iterator or the dereferenced iter=
ator rather than an index I guess.</div><div><br></div><div>It&#39;s worth =
remembering that since values are stored in the totals container in ascendi=
ng order, as would be in the indecies (in the given case), this argues stro=
ngly against a map or multimap since:</div><div>1. you pay the LogN cost of=
 insertion, which you do not with an array or vector,</div><div>2. in the g=
iven examples, we&#39;re storing ascending indexes as the mapped_type in th=
e map. Since they&#39;re ascending and consecutive we may as well use the i=
ndecies of an array and avoid paying the cost of=C2=A0 a lookup.</div><div>=
<br></div><div>I would still go for the array version, even if I had to sto=
re a pair (or tuple). All it would require would be a custom predicate on t=
he lower_bound call.</div><div>=C2=A0<br></div><blockquote class=3D"gmail_q=
uote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1e=
x"><div dir=3D"auto"><div></div><div>Olivier.=C2=A0</div><div><br>On 19 jui=
l. 2018, at 13:32, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com"=
 target=3D"_blank">hodges.r@gmail.com</a>&gt; wrote:<br><br></div><blockquo=
te type=3D"cite"><div><div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><=
div dir=3D"ltr">On Thu, 19 Jul 2018 at 10:11, Olivier S. &lt;<a href=3D"mai=
lto:olivier.sohn@gmail.com" target=3D"_blank">olivier.sohn@gmail.com</a>&gt=
; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hello,<di=
v><br></div><div>This is the motivating example:</div></div></blockquote><d=
iv><br></div><div>I think I&#39;d be inclined to transform the probabilitie=
s into an array of limits and perform the upper_bound on that:</div><div><b=
r></div><div><pre style=3D"background-color:rgb(43,43,43);color:rgb(169,183=
,198);font-family:&quot;DejaVu Sans Mono&quot;;font-size:11.3pt"><span styl=
e=3D"color:rgb(128,128,128)">//<br></span><span style=3D"color:rgb(128,128,=
128)">// Created by rhodges on 19/07/18.<br></span><span style=3D"color:rgb=
(128,128,128)">//<br></span><span style=3D"color:rgb(128,128,128)"><br></sp=
an><span style=3D"color:rgb(187,181,41)">#include </span><span style=3D"col=
or:rgb(106,135,89)">&lt;cassert&gt;<br></span><span style=3D"color:rgb(187,=
181,41)">#include </span><span style=3D"color:rgb(106,135,89)">&lt;iostream=
&gt;<br></span><span style=3D"color:rgb(187,181,41)">#include </span><span =
style=3D"color:rgb(106,135,89)">&lt;vector&gt;<br></span><span style=3D"col=
or:rgb(187,181,41)">#include </span><span style=3D"color:rgb(106,135,89)">&=
lt;algorithm&gt;<br></span><span style=3D"color:rgb(187,181,41)">#include <=
/span><span style=3D"color:rgb(106,135,89)">&lt;iterator&gt;<br></span><spa=
n style=3D"color:rgb(187,181,41)">#include </span><span style=3D"color:rgb(=
106,135,89)">&lt;type_traits&gt;<br></span><span style=3D"color:rgb(187,181=
,41)">#include </span><span style=3D"color:rgb(106,135,89)">&lt;array&gt;<b=
r></span><span style=3D"color:rgb(106,135,89)"><br></span><span style=3D"co=
lor:rgb(106,135,89)"><br></span><span style=3D"color:rgb(128,128,128)">/// =
Convert a container of probabilities into a container of ascending interval=
 limits<br></span><span style=3D"color:rgb(204,120,50);font-weight:bold">te=
mplate</span>&lt;<span style=3D"color:rgb(204,120,50);font-weight:bold">cla=
ss </span><span style=3D"color:rgb(185,188,209)">Container</span>&gt;<br><s=
pan style=3D"color:rgb(204,120,50);font-weight:bold">auto </span><span styl=
e=3D"color:rgb(255,198,109)">to_limits</span>(<span style=3D"color:rgb(185,=
188,209)">Container </span>container) {<br>    <span style=3D"color:rgb(204=
,120,50);font-weight:bold">using </span><span style=3D"color:rgb(185,188,20=
9)">container_type </span>=3D <span style=3D"color:rgb(181,182,227)">std</s=
pan>::<span style=3D"color:rgb(185,188,209)">decay_t</span>&lt;<span style=
=3D"color:rgb(185,188,209)">Container</span>&gt;<span style=3D"color:rgb(20=
4,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    </span><sp=
an style=3D"color:rgb(204,120,50);font-weight:bold">using </span><span styl=
e=3D"color:rgb(185,188,209)">value_type </span>=3D <span style=3D"color:rgb=
(204,120,50);font-weight:bold">typename </span><span style=3D"color:rgb(185=
,188,209)">container_type</span>::<span style=3D"color:rgb(185,188,209)">va=
lue_type</span><span style=3D"color:rgb(204,120,50)">;<br></span><span styl=
e=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50)=
;font-weight:bold">auto </span>to_total =3D [total =3D <span style=3D"color=
:rgb(185,188,209)">value_type</span>(<span style=3D"color:rgb(104,151,187)"=
>0</span>)](<span style=3D"color:rgb(204,120,50);font-weight:bold">auto </s=
pan>&amp;&amp;x) <span style=3D"color:rgb(204,120,50);font-weight:bold">mut=
able </span>{<br>        total +=3D x<span style=3D"color:rgb(204,120,50)">=
;<br></span><span style=3D"color:rgb(204,120,50)">        </span><span styl=
e=3D"color:rgb(204,120,50);font-weight:bold">return </span>total<span style=
=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)=
">    </span>}<span style=3D"color:rgb(204,120,50)">;<br></span><span style=
=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50)"=
>    </span><span style=3D"color:rgb(181,182,227)">std</span>::transform(<s=
pan style=3D"color:rgb(181,182,227)">std</span>::begin(container)<span styl=
e=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(181,182,227)"=
>std</span>::end(container)<span style=3D"color:rgb(204,120,50)">,<br></spa=
n><span style=3D"color:rgb(204,120,50)">                   </span><span sty=
le=3D"color:rgb(181,182,227)">std</span>::begin(container)<span style=3D"co=
lor:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">    =
               </span>to_total)<span style=3D"color:rgb(204,120,50)">;<br><=
/span><span style=3D"color:rgb(204,120,50)"><br></span><span style=3D"color=
:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50);font-weig=
ht:bold">return </span>container<span style=3D"color:rgb(204,120,50)">;<br>=
</span>}<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"co=
lor:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50);font-w=
eight:bold">template</span>&lt;<span style=3D"color:rgb(204,120,50);font-we=
ight:bold">class </span><span style=3D"color:rgb(185,188,209)">Container</s=
pan>&gt;<br><span style=3D"color:rgb(204,120,50);font-weight:bold">struct <=
/span><span style=3D"color:rgb(181,182,227)">assert_not_empty </span>{<br> =
   <span style=3D"color:rgb(255,198,109)">assert_not_empty</span>(<span sty=
le=3D"color:rgb(185,188,209)">Container </span><span style=3D"color:rgb(204=
,120,50);font-weight:bold">const </span>&amp;container) <span style=3D"colo=
r:rgb(204,120,50);font-weight:bold">noexcept </span>{<br>        <span styl=
e=3D"color:rgb(144,139,37)">assert</span>(!container.<span style=3D"color:r=
gb(152,118,170);font-style:italic">empty</span>())<span style=3D"color:rgb(=
204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    </span>}=
<br>}<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color=
:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50);font-weig=
ht:bold">template</span>&lt;<span style=3D"color:rgb(204,120,50);font-weigh=
t:bold">class </span><span style=3D"color:rgb(185,188,209)">Type</span><spa=
n style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(181,182=
,227)">std</span>::<span style=3D"color:rgb(185,188,209)">size_t </span><sp=
an style=3D"color:rgb(152,118,170);font-style:italic">N</span>&gt;<br><span=
 style=3D"color:rgb(204,120,50);font-weight:bold">struct </span><span style=
=3D"color:rgb(181,182,227)">assert_not_empty</span>&lt;<span style=3D"color=
:rgb(181,182,227)">std</span>::<span style=3D"color:rgb(181,182,227)">array=
</span>&lt;<span style=3D"color:rgb(185,188,209)">Type</span><span style=3D=
"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(152,118,170);font=
-style:italic">N</span>&gt;&gt; {<br>    <span style=3D"color:rgb(204,120,5=
0);font-weight:bold">constexpr </span><span style=3D"color:rgb(255,198,109)=
">assert_not_empty</span>(<span style=3D"color:rgb(181,182,227)">std</span>=
::<span style=3D"color:rgb(181,182,227)">array</span>&lt;<span style=3D"col=
or:rgb(185,188,209)">Type</span><span style=3D"color:rgb(204,120,50)">, </s=
pan><span style=3D"color:rgb(152,118,170);font-style:italic">N</span>&gt; <=
span style=3D"color:rgb(204,120,50);font-weight:bold">const </span>&amp;con=
tainer) <span style=3D"color:rgb(204,120,50);font-weight:bold">noexcept </s=
pan>{<br>        <span style=3D"color:rgb(204,120,50);font-weight:bold">sta=
tic_assert</span>(!container.empty())<span style=3D"color:rgb(204,120,50)">=
;<br></span><span style=3D"color:rgb(204,120,50)">    </span>}<br>}<span st=
yle=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,=
50)"><br></span><span style=3D"color:rgb(204,120,50)"><br></span><span styl=
e=3D"color:rgb(128,128,128)">/// given an ascending container of limits, ch=
oose one with probability equal to 1.0 / max(limits)<br></span><span style=
=3D"color:rgb(128,128,128)">/// @returns an integer containing the index of=
 the chosen limit<br></span><span style=3D"color:rgb(204,120,50);font-weigh=
t:bold">template</span>&lt;<span style=3D"color:rgb(204,120,50);font-weight=
:bold">class </span><span style=3D"color:rgb(185,188,209)">Container</span>=
<span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(204=
,120,50);font-weight:bold">class </span><span style=3D"color:rgb(185,188,20=
9)">Value</span>&gt;<br><span style=3D"color:rgb(204,120,50);font-weight:bo=
ld">int </span><span style=3D"color:rgb(255,198,109)">sample_limits</span>(=
<span style=3D"color:rgb(185,188,209)">Container </span>&amp;&amp;totals<sp=
an style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(185,18=
8,209)">Value </span>&amp;&amp;prob) {<br><br>    <span style=3D"color:rgb(=
128,128,128)">// precondition<br></span><span style=3D"color:rgb(128,128,12=
8)">    </span>assert_not_empty{totals}<span style=3D"color:rgb(204,120,50)=
">;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><span style=
=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(128,128,128)=
">// convert prob (range 0-1) into a total.<br></span><span style=3D"color:=
rgb(128,128,128)"><br></span><span style=3D"color:rgb(128,128,128)">    </s=
pan><span style=3D"color:rgb(204,120,50);font-weight:bold">auto </span>targ=
et =3D prob * totals.<span style=3D"color:rgb(152,118,170);font-style:itali=
c">back</span>()<span style=3D"color:rgb(204,120,50)">;<br></span><span sty=
le=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50=
)">    </span><span style=3D"color:rgb(204,120,50);font-weight:bold">auto <=
/span>cap =3D [&amp;totals](<span style=3D"color:rgb(204,120,50);font-weigh=
t:bold">auto </span>iter) {<br>        <span style=3D"color:rgb(204,120,50)=
;font-weight:bold">if </span>(iter =3D=3D <span style=3D"color:rgb(181,182,=
227)">std</span>::end(totals))<br>            <span style=3D"color:rgb(204,=
120,50);font-weight:bold">return </span><span style=3D"color:rgb(181,182,22=
7)">std</span>::prev(iter)<span style=3D"color:rgb(204,120,50)">;<br></span=
><span style=3D"color:rgb(204,120,50)">        </span><span style=3D"color:=
rgb(204,120,50);font-weight:bold">else<br></span><span style=3D"color:rgb(2=
04,120,50);font-weight:bold">            return </span>iter<span style=3D"c=
olor:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">   =
 </span>}<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"c=
olor:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50)">    =
</span><span style=3D"color:rgb(204,120,50);font-weight:bold">auto </span>f=
irst =3D <span style=3D"color:rgb(181,182,227)">std</span>::cbegin(totals)<=
span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(2=
04,120,50)">    </span><span style=3D"color:rgb(204,120,50);font-weight:bol=
d">auto </span>last =3D <span style=3D"color:rgb(181,182,227)">std</span>::=
cend(totals)<span style=3D"color:rgb(204,120,50)">;<br></span><span style=
=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50)"=
>    </span><span style=3D"color:rgb(204,120,50);font-weight:bold">auto </s=
pan>index =3D <span style=3D"color:rgb(181,182,227)">std</span>::distance(f=
irst<span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:=
rgb(204,120,50)">                               </span>cap(<span style=3D"c=
olor:rgb(181,182,227)">std</span>::lower_bound(first<span style=3D"color:rg=
b(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">          =
                                          </span>last<span style=3D"color:r=
gb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">         =
                                           </span>target)))<span style=3D"c=
olor:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">   =
 </span><span style=3D"color:rgb(204,120,50);font-weight:bold">return int</=
span>(index)<span style=3D"color:rgb(204,120,50)">;<br></span>}<br><br><spa=
n style=3D"color:rgb(204,120,50);font-weight:bold">int </span><span style=
=3D"color:rgb(255,198,109)">main</span>() {<br><br>    <span style=3D"color=
:rgb(128,128,128)">// The goal is to sample among ints from 0 to 9, with di=
fferent probability for each int.<br></span><span style=3D"color:rgb(128,12=
8,128)">    // The probability of i is proba[i] / sum(proba).<br></span><sp=
an style=3D"color:rgb(128,128,128)">    </span><span style=3D"color:rgb(181=
,182,227)">std</span>::<span style=3D"color:rgb(181,182,227)">array</span>&=
lt;<span style=3D"color:rgb(204,120,50);font-weight:bold">float</span><span=
 style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,=
187)">10</span>&gt; proba{{<span style=3D"color:rgb(104,151,187)">0.01</spa=
n><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(1=
04,151,187)">0.05</span><span style=3D"color:rgb(204,120,50)">, </span><spa=
n style=3D"color:rgb(104,151,187)">0.01</span><span style=3D"color:rgb(204,=
120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.15</span><span s=
tyle=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,18=
7)">0.1</span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D=
"color:rgb(104,151,187)">0.19</span><span style=3D"color:rgb(204,120,50)">,=
 </span><span style=3D"color:rgb(104,151,187)">0.19</span><span style=3D"co=
lor:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.1</s=
pan><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb=
(104,151,187)">0.19</span><span style=3D"color:rgb(204,120,50)">, </span><s=
pan style=3D"color:rgb(104,151,187)">0.01</span>}}<span style=3D"color:rgb(=
204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    </span><=
span style=3D"color:rgb(204,120,50);font-weight:bold">auto </span>limits =
=3D to_limits(proba)<span style=3D"color:rgb(204,120,50)">;<br></span><span=
 style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,12=
0,50);font-weight:bold">for </span>(<span style=3D"color:rgb(204,120,50);fo=
nt-weight:bold">auto </span>v : <span style=3D"color:rgb(181,182,227)">std<=
/span>::vector&lt;<span style=3D"color:rgb(204,120,50);font-weight:bold">fl=
oat</span>&gt;{{<br>                                             <span styl=
e=3D"color:rgb(104,151,187)">0.f</span><span style=3D"color:rgb(204,120,50)=
">,<br></span><span style=3D"color:rgb(204,120,50)">                       =
                      </span><span style=3D"color:rgb(104,151,187)">0.00001=
f</span><span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"co=
lor:rgb(204,120,50)">                                             </span><s=
pan style=3D"color:rgb(104,151,187)">0.02f</span><span style=3D"color:rgb(2=
04,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">             =
                                </span><span style=3D"color:rgb(104,151,187=
)">0.5f</span><span style=3D"color:rgb(204,120,50)">,<br></span><span style=
=3D"color:rgb(204,120,50)">                                             </s=
pan><span style=3D"color:rgb(104,151,187)">0.98f</span><span style=3D"color=
:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">       =
                                      </span><span style=3D"color:rgb(104,1=
51,187)">1.f</span><span style=3D"color:rgb(204,120,50)">,  </span><span st=
yle=3D"color:rgb(128,128,128)">// on upper bound<br></span><span style=3D"c=
olor:rgb(128,128,128)">                                             </span>=
<span style=3D"color:rgb(104,151,187)">1.00001f  </span><span style=3D"colo=
r:rgb(128,128,128)">// beyond upper bound<br></span><span style=3D"color:rg=
b(128,128,128)">                                     </span>}}) {<br>      =
  <span style=3D"color:rgb(181,182,227)">std</span>::cout <span style=3D"co=
lor:rgb(95,140,138)">&lt;&lt; </span>sample_limits(limits<span style=3D"col=
or:rgb(204,120,50)">, </span>v) <span style=3D"color:rgb(95,140,138)">&lt;&=
lt; </span><span style=3D"color:rgb(181,182,227)">std</span>::endl<span sty=
le=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,5=
0)">    </span>}<br>    <br>    <span style=3D"color:rgb(204,120,50);font-w=
eight:bold">return </span><span style=3D"color:rgb(104,151,187)">0</span><s=
pan style=3D"color:rgb(204,120,50)">;<br></span>}<br><br></pre><br></div><d=
iv>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><br></d=
iv><div>




<span></span>





<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p1" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(81,182,255);background-color:rgb(0,0,0)">#include <span =
class=3D"m_5052467569217688009m_2612803408380067011gmail-s1" style=3D"color=
:rgb(180,38,26)">&lt;cassert&gt;</span></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p2" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(180,38,26);background-color:rgb(0,0,0)"><span class=3D"m=
_5052467569217688009m_2612803408380067011gmail-s2" style=3D"color:rgb(81,18=
2,255)">#include </span>&lt;iostream&gt;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p1" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(81,182,255);background-color:rgb(0,0,0)">#include <span =
class=3D"m_5052467569217688009m_2612803408380067011gmail-s1" style=3D"color=
:rgb(180,38,26)">&lt;map&gt;</span></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p1" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(81,182,255);background-color:rgb(0,0,0)">#include <span =
class=3D"m_5052467569217688009m_2612803408380067011gmail-s1" style=3D"color=
:rgb(180,38,26)">&lt;vector&gt;</span></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p3" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Helvetica;background-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p4" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(255,178,109);background-color:rgb(0,0,0)">using<span cla=
ss=3D"m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"> </span>namespace<span class=3D"m_5052467569217688009m_2612=
803408380067011gmail-s3" style=3D"color:rgb(231,254,226)"> std;</span></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p3" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Helvetica;background-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color:rgb(255=
,178,109)">using</span> Intervals =3D map&lt;<span class=3D"m_5052467569217=
688009m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">float=
</span>,<span class=3D"m_5052467569217688009m_2612803408380067011gmail-s4" =
style=3D"color:rgb(255,178,109)">int</span>&gt;;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p3" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Helvetica;background-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color:rgb(255=
,178,109)">int</span> sample(Intervals <span class=3D"m_5052467569217688009=
m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">const</span=
> &amp; intervals, <span class=3D"m_5052467569217688009m_261280340838006701=
1gmail-s4" style=3D"color:rgb(255,178,109)">float</span> randomFloat) {</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span>assert(!intervals.empty());</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_2612803408380067011=
gmail-s4" style=3D"color:rgb(255,178,109)">auto</span> it =3D intervals.low=
er_bound(randomFloat);</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_2612803408380067011=
gmail-s4" style=3D"color:rgb(255,178,109)">if</span>(it =3D=3D intervals.en=
d()) {</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// we return th=
e last element of the map:</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_26128=
03408380067011gmail-s4" style=3D"color:rgb(255,178,109)">return</span> crbe=
gin(intervals)-&gt;second;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// this is what=
 I would prefer writing:</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// return inter=
vals.back();</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span>}</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_2612803408380067011=
gmail-s4" style=3D"color:rgb(255,178,109)">return</span> it-&gt;second;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)">}</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p3" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Helvetica;background-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color:rgb(255=
,178,109)">int</span> main() {</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 </span></span>// The goal is to sample amon=
g ints from 0 to 9, with different probability for each int.</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p3" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Helvetica;background-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 </span></span>// The probability of i is pr=
oba[i].</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p7" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(212,212,212);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231=
,254,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-A=
pple-converted-space">=C2=A0 =C2=A0 </span>std::array&lt;</span><span class=
=3D"m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color:rgb(=
255,178,109)">float</span><span class=3D"m_5052467569217688009m_26128034083=
80067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>10<span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231=
,254,226)">&gt; proba {{</span>0.01<span class=3D"m_5052467569217688009m_26=
12803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.05<sp=
an class=3D"m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"co=
lor:rgb(231,254,226)">,</span>0.01<span class=3D"m_5052467569217688009m_261=
2803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.15<spa=
n class=3D"m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"col=
or:rgb(231,254,226)">,</span>0.1<span class=3D"m_5052467569217688009m_26128=
03408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.19<span =
class=3D"m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color=
:rgb(231,254,226)">,</span>0.19<span class=3D"m_5052467569217688009m_261280=
3408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.1<span cl=
ass=3D"m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:r=
gb(231,254,226)">,</span>0.19<span class=3D"m_5052467569217688009m_26128034=
08380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.01<span cla=
ss=3D"m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)">}};</span></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p3" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Helvetica;background-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 </span></span>// The sum of probabilities a=
re 1, so we can conceptually partition</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 </span></span>// the [0,1] interval in 10 i=
ntervals of lengths proba[i].</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_2612803408380067011=
gmail-s5" style=3D"color:rgb(0,193,0)">//</span></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 </span></span>// we only need to store the =
upper bounds of these intervals,</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 </span></span>// because the lower bound of=
 interval n is the upper bound of interval n-1,</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 </span></span>// and because we know that t=
he first lower bound is 0:</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span>Intervals intervals;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_2612803408380067011=
gmail-s4" style=3D"color:rgb(255,178,109)">int</span> i =3D <span class=3D"=
m_5052467569217688009m_2612803408380067011gmail-s6" style=3D"color:rgb(212,=
212,212)">0</span>;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_2612803408380067011=
gmail-s4" style=3D"color:rgb(255,178,109)">float</span> upperbound =3D <spa=
n class=3D"m_5052467569217688009m_2612803408380067011gmail-s6" style=3D"col=
or:rgb(212,212,212)">0.f</span>;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_2612803408380067011=
gmail-s4" style=3D"color:rgb(255,178,109)">for</span>(<span class=3D"m_5052=
467569217688009m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,10=
9)">auto</span> p : proba) {</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span>upperbound +=3D p;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span>intervals.emplace(upperbound,i);</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// Note that th=
e last key, due to floating point rounding errors, will be</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// close to 1.f=
 but not necessarilly 1.f.</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span>++i;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span>}</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p8" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0);min-height:14px=
"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple-conv=
erted-space">=C2=A0=C2=A0 =C2=A0</span></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 </span></span>// Then, to sample ints, we g=
enerate a random numbers between 0 and 1,</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p6" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0)"><span class=3D"m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-Apple=
-converted-space">=C2=A0 =C2=A0 </span></span>// and lookup the correpondin=
g interval:</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p3" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Helvetica;background-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_2612803408380067011=
gmail-s4" style=3D"color:rgb(255,178,109)">for</span>(<span class=3D"m_5052=
467569217688009m_2612803408380067011gmail-s4" style=3D"color:rgb(255,178,10=
9)">auto</span> v : vector&lt;<span class=3D"m_5052467569217688009m_2612803=
408380067011gmail-s4" style=3D"color:rgb(255,178,109)">float</span>&gt;{{</=
p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_26128=
03408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.f</span>,</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_26128=
03408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.00001f</span>,</=
p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_26128=
03408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.02f</span>,</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_26128=
03408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.5f</span>,</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_26128=
03408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0.98f</span>,</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_26128=
03408380067011gmail-s6" style=3D"color:rgb(212,212,212)">1.f</span>,</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_5052467569217688009m_26128=
03408380067011gmail-s6" style=3D"color:rgb(212,212,212)">1.00001f</span>}})=
</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span>{</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </span>cout &lt;&lt; sample(intervals, v) &lt;&lt;=
 endl;</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=
=A0 =C2=A0 </span>}</p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p3" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Helvetica;background-color:rgb(0,0,0);min-height:14px"><br></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p4" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(255,178,109);background-color:rgb(0,0,0)"><span class=3D=
"m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231=
,254,226)"><span class=3D"m_5052467569217688009m_2612803408380067011gmail-A=
pple-converted-space">=C2=A0 =C2=A0 </span></span>return<span class=3D"m_50=
52467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,=
226)"> </span><span class=3D"m_5052467569217688009m_2612803408380067011gmai=
l-s6" style=3D"color:rgb(212,212,212)">0</span><span class=3D"m_50524675692=
17688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">;</=
span></p>
<p class=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)">}</p><p class=
=3D"m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"margin:0px=
;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:nor=
mal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo=
;color:rgb(231,254,226);background-color:rgb(0,0,0)"><br></p>


</div><div><br></div><div>Olivier</div><div><br></div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr">Le=C2=A0mer. 18 juil. 2018 =C3=A0=C2=A019=
:53, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com" target=3D"_bl=
ank">hodges.r@gmail.com</a>&gt; a =C3=A9crit=C2=A0:<br></div><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;p=
adding-left:1ex"><div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr">On Wed, 18 Jul 2018 at 14:10, Olivier Sohn &lt;<a href=3D"mailto=
:olivier.sohn@gmail.com" target=3D"_blank">olivier.sohn@gmail.com</a>&gt; w=
rote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hello,<div><=
br></div><div>I recently encountered an algorithm where I needed to access =
the map&#39;s last element, knowing the map was non-empty. But I found that=
 there is no straightforward way to access the last (or first) element of a=
 map.</div></div></blockquote><div><br></div><div>Without wishing to sound =
too controversial, I am wondering whether this algorithm could have been ex=
pressed in a different way.</div><div><br></div><div>Wanting a reference to=
 the &quot;back&quot; of a container suggests a need to access the least/gr=
eatest/earliest/latest of a group of things.=C2=A0</div><div><br></div><div=
>Are you able to share the motivating use case, as there are other tools fo=
r this in the standard library?<br></div><div>=C2=A0</div><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex"><div dir=3D"ltr"><div><br></div><div>So as suggested by this =
process :=C2=A0<a href=3D"https://isocpp.org/std/submit-a-proposal" target=
=3D"_blank">https://isocpp.org/std/submit-a-proposal</a> I&#39;d like to pr=
opose the following: add the front() and back() methods, with const and non=
 const versions, to std::(multi)map and std::(multi)set, to return a (const=
-) reference to the last / first element(s).</div><div><br></div><div>Like =
with other containers implementing these methods, no guaranty would be give=
n when the container is empty.</div><div><br></div><div>What do you think?<=
/div><div><br></div><div>Cheers,</div><div>Olivier</div><div><br></div></di=
v>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-=
45df-9536-e0bfab52962b%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3=
ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfooter"=
 target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-propo=
sals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com</=
a>.<br>
</blockquote></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsi=
bvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfoo=
ter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-p=
roposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gma=
il.com</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPU=
Wj%3DvBNSJCx7-pDfYuA%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Df=
ooter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std=
-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail=
..gmail.com</a>.<br>
</div></blockquote></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7F5-4C90-AD0E-CFEAC5F95CD6%=
40gmail.com?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7F5-4=
C90-AD0E-CFEAC5F95CD6%40gmail.com</a>.<br>
</blockquote></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hYvMBhNXTPdBHsLEvbvvU71azApw%2B=
SQiGmZNZd4ps%2BWhw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hYvMBhN=
XTPdBHsLEvbvvU71azApw%2BSQiGmZNZd4ps%2BWhw%40mail.gmail.com</a>.<br />

--0000000000002ab940057159958f--

.


Author: "Olivier S." <olivier.sohn@gmail.com>
Date: Thu, 19 Jul 2018 15:37:03 +0200
Raw View
--000000000000026b3c05715a47ef
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Sure, there are other ways to implement that, but this example shows that
there a legitimate uses for back and front in a map, to return references
to elements.


Le jeu. 19 juil. 2018 =C3=A0 14:47, Richard Hodges <hodges.r@gmail.com> a =
=C3=A9crit :

>
>
> On Thu, 19 Jul 2018 at 14:20, Olivier S. <olivier.sohn@gmail.com> wrote:
>
>> That would work too but if we want to sample some strings instead if int=
s
>> then we have to use a map. (Or a vector of pairs but that complicates th=
e
>> implementation a bit)
>>
>>
> Hi Oliver,
>
> I think the salient point is that regardless of the actual data, the only
> action we are interested in is finding the index matching the selected
> probability. Having done that we can use the index to find the real data =
in
> whatever data structure is using it.
>
> If you were using a std::multimap<float,
> std::reference_wrapper<std::string>> as the totals container, that would
> work too. You might have to change the call to lower_bound, that's all.
>
> In this case you'd want to return either the iterator or the dereferenced
> iterator rather than an index I guess.
>
> It's worth remembering that since values are stored in the totals
> container in ascending order, as would be in the indecies (in the given
> case), this argues strongly against a map or multimap since:
> 1. you pay the LogN cost of insertion, which you do not with an array or
> vector,
> 2. in the given examples, we're storing ascending indexes as the
> mapped_type in the map. Since they're ascending and consecutive we may as
> well use the indecies of an array and avoid paying the cost of  a lookup.
>
> I would still go for the array version, even if I had to store a pair (or
> tuple). All it would require would be a custom predicate on the lower_bou=
nd
> call.
>
>
>> Olivier.
>>
>> On 19 juil. 2018, at 13:32, Richard Hodges <hodges.r@gmail.com> wrote:
>>
>>
>>
>> On Thu, 19 Jul 2018 at 10:11, Olivier S. <olivier.sohn@gmail.com> wrote:
>>
>>> Hello,
>>>
>>> This is the motivating example:
>>>
>>
>> I think I'd be inclined to transform the probabilities into an array of
>> limits and perform the upper_bound on that:
>>
>> //
>> // Created by rhodges on 19/07/18.
>> //
>>
>> #include <cassert>
>> #include <iostream>
>> #include <vector>
>> #include <algorithm>
>> #include <iterator>
>> #include <type_traits>
>> #include <array>
>>
>>
>> /// Convert a container of probabilities into a container of ascending i=
nterval limits
>> template<class Container>
>> auto to_limits(Container container) {
>>     using container_type =3D std::decay_t<Container>;
>>     using value_type =3D typename container_type::value_type;
>>     auto to_total =3D [total =3D value_type(0)](auto &&x) mutable {
>>         total +=3D x;
>>         return total;
>>     };
>>
>>     std::transform(std::begin(container), std::end(container),
>>                    std::begin(container),
>>                    to_total);
>>
>>     return container;
>> };
>>
>> template<class Container>
>> struct assert_not_empty {
>>     assert_not_empty(Container const &container) noexcept {
>>         assert(!container.empty());
>>     }
>> };
>>
>> template<class Type, std::size_t N>
>> struct assert_not_empty<std::array<Type, N>> {
>>     constexpr assert_not_empty(std::array<Type, N> const &container) noe=
xcept {
>>         static_assert(!container.empty());
>>     }
>> };
>>
>>
>> /// given an ascending container of limits, choose one with probability =
equal to 1.0 / max(limits)
>> /// @returns an integer containing the index of the chosen limit
>> template<class Container, class Value>
>> int sample_limits(Container &&totals, Value &&prob) {
>>
>>     // precondition
>>     assert_not_empty{totals};
>>
>>     // convert prob (range 0-1) into a total.
>>
>>     auto target =3D prob * totals.back();
>>
>>     auto cap =3D [&totals](auto iter) {
>>         if (iter =3D=3D std::end(totals))
>>             return std::prev(iter);
>>         else
>>             return iter;
>>     };
>>
>>     auto first =3D std::cbegin(totals);
>>     auto last =3D std::cend(totals);
>>
>>     auto index =3D std::distance(first,
>>                                cap(std::lower_bound(first,
>>                                                     last,
>>                                                     target)));
>>     return int(index);
>> }
>>
>> int main() {
>>
>>     // The goal is to sample among ints from 0 to 9, with different prob=
ability for each int.
>>     // The probability of i is proba[i] / sum(proba).
>>     std::array<float, 10> proba{{0.01, 0.05, 0.01, 0.15, 0.1, 0.19, 0.19=
, 0.1, 0.19, 0.01}};
>>     auto limits =3D to_limits(proba);
>>     for (auto v : std::vector<float>{{
>>                                              0.f,
>>                                              0.00001f,
>>                                              0.02f,
>>                                              0.5f,
>>                                              0.98f,
>>                                              1.f,  // on upper bound
>>                                              1.00001f  // beyond upper b=
ound
>>                                      }}) {
>>         std::cout << sample_limits(limits, v) << std::endl;
>>     }
>>
>>     return 0;
>> }
>>
>>
>>
>>
>>>
>>> #include <cassert>
>>>
>>> #include <iostream>
>>>
>>> #include <map>
>>>
>>> #include <vector>
>>>
>>>
>>> using namespace std;
>>>
>>>
>>> using Intervals =3D map<float,int>;
>>>
>>>
>>> int sample(Intervals const & intervals, float randomFloat) {
>>>
>>>     assert(!intervals.empty());
>>>
>>>     auto it =3D intervals.lower_bound(randomFloat);
>>>
>>>     if(it =3D=3D intervals.end()) {
>>>
>>>         // we return the last element of the map:
>>>
>>>         return crbegin(intervals)->second;
>>>
>>>         // this is what I would prefer writing:
>>>
>>>         // return intervals.back();
>>>
>>>     }
>>>
>>>     return it->second;
>>>
>>> }
>>>
>>>
>>> int main() {
>>>
>>>     // The goal is to sample among ints from 0 to 9, with different
>>> probability for each int.
>>>
>>>
>>>     // The probability of i is proba[i].
>>>
>>>     std::array<float,10> proba {{0.01,0.05,0.01,0.15,0.1,0.19,0.19,0.1,
>>> 0.19,0.01}};
>>>
>>>
>>>     // The sum of probabilities are 1, so we can conceptually partition
>>>
>>>     // the [0,1] interval in 10 intervals of lengths proba[i].
>>>
>>>     //
>>>
>>>     // we only need to store the upper bounds of these intervals,
>>>
>>>     // because the lower bound of interval n is the upper bound of
>>> interval n-1,
>>>
>>>     // and because we know that the first lower bound is 0:
>>>
>>>     Intervals intervals;
>>>
>>>     int i =3D 0;
>>>
>>>     float upperbound =3D 0.f;
>>>
>>>     for(auto p : proba) {
>>>
>>>         upperbound +=3D p;
>>>
>>>         intervals.emplace(upperbound,i);
>>>
>>>         // Note that the last key, due to floating point rounding
>>> errors, will be
>>>
>>>         // close to 1.f but not necessarilly 1.f.
>>>
>>>         ++i;
>>>
>>>     }
>>>
>>>
>>>
>>>     // Then, to sample ints, we generate a random numbers between 0 and
>>> 1,
>>>
>>>     // and lookup the correponding interval:
>>>
>>>
>>>     for(auto v : vector<float>{{
>>>
>>>         0.f,
>>>
>>>         0.00001f,
>>>
>>>         0.02f,
>>>
>>>         0.5f,
>>>
>>>         0.98f,
>>>
>>>         1.f,
>>>
>>>         1.00001f}})
>>>
>>>     {
>>>
>>>         cout << sample(intervals, v) << endl;
>>>
>>>     }
>>>
>>>
>>>     return 0;
>>>
>>> }
>>>
>>>
>>>
>>> Olivier
>>>
>>>
>>> Le mer. 18 juil. 2018 =C3=A0 19:53, Richard Hodges <hodges.r@gmail.com>=
 a
>>> =C3=A9crit :
>>>
>>>>
>>>>
>>>> On Wed, 18 Jul 2018 at 14:10, Olivier Sohn <olivier.sohn@gmail.com>
>>>> wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I recently encountered an algorithm where I needed to access the map'=
s
>>>>> last element, knowing the map was non-empty. But I found that there i=
s no
>>>>> straightforward way to access the last (or first) element of a map.
>>>>>
>>>>
>>>> Without wishing to sound too controversial, I am wondering whether thi=
s
>>>> algorithm could have been expressed in a different way.
>>>>
>>>> Wanting a reference to the "back" of a container suggests a need to
>>>> access the least/greatest/earliest/latest of a group of things.
>>>>
>>>> Are you able to share the motivating use case, as there are other tool=
s
>>>> for this in the standard library?
>>>>
>>>>
>>>>>
>>>>> So as suggested by this process :
>>>>> https://isocpp.org/std/submit-a-proposal I'd like to propose the
>>>>> following: add the front() and back() methods, with const and non con=
st
>>>>> versions, to std::(multi)map and std::(multi)set, to return a (const-=
)
>>>>> reference to the last / first element(s).
>>>>>
>>>>> Like with other containers implementing these methods, no guaranty
>>>>> would be given when the container is empty.
>>>>>
>>>>> What do you think?
>>>>>
>>>>> Cheers,
>>>>> Olivier
>>>>>
>>>>> --
>>>>> 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, sen=
d
>>>>> an email to std-proposals+unsubscribe@isocpp.org.
>>>>> To post to this group, send email to std-proposals@isocpp.org.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e=
-6337-45df-9536-e0bfab52962b%40isocpp.org
>>>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4=
e-6337-45df-9536-e0bfab52962b%40isocpp.org?utm_medium=3Demail&utm_source=3D=
footer>
>>>>> .
>>>>>
>>>> --
>>>> You received this message because you are subscribed to a topic in the
>>>> Google Groups "ISO C++ Standard - Future Proposals" group.
>>>> To unsubscribe from this topic, visit
>>>> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KS=
kQ/unsubscribe
>>>> .
>>>> To unsubscribe from this group and all its topics, send an email to
>>>> std-proposals+unsubscribe@isocpp.org.
>>>> To post to this group, send email to std-proposals@isocpp.org.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbH=
NVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com
>>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hb=
HNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3De=
mail&utm_source=3Dfooter>
>>>> .
>>>>
>>> --
>>> 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.
>>> To view this discussion on the web visit
>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80t5P=
dx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gmail.com
>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80t5=
Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=
=3Demail&utm_source=3Dfooter>
>>> .
>>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe
>> .
>> To unsubscribe from this group and all its topics, send an email to
>> std-proposals+unsubscribe@isocpp.org.
>> To post to this group, send email to std-proposals@isocpp.org.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3haNKo=
0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail.gmail.com
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3haNK=
o0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail.gmail.com?utm_medium=
=3Demail&utm_source=3Dfooter>
>> .
>>
>> --
>> You received this message because you are subscribed to the Google Group=
s
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n
>> email to std-proposals+unsubscribe@isocpp.org.
>> To post to this group, send email to std-proposals@isocpp.org.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7=
F5-4C90-AD0E-CFEAC5F95CD6%40gmail.com
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/583D12C6-E=
7F5-4C90-AD0E-CFEAC5F95CD6%40gmail.com?utm_medium=3Demail&utm_source=3Dfoot=
er>
>> .
>>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/=
unsubscribe
> .
> To unsubscribe from this group and all its topics, send an email to
> std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hYvMBh=
NXTPdBHsLEvbvvU71azApw%2BSQiGmZNZd4ps%2BWhw%40mail.gmail.com
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hYvMB=
hNXTPdBHsLEvbvvU71azApw%2BSQiGmZNZd4ps%2BWhw%40mail.gmail.com?utm_medium=3D=
email&utm_source=3Dfooter>
> .
>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CANAj80uD9%2BBA6-dC66ixmTvoqcsAknOLvjZsT59d0HnkU=
yd0cA%40mail.gmail.com.

--000000000000026b3c05715a47ef
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Sure, there are other ways to implement that, but this exa=
mple shows that there a legitimate uses for back and front in a map, to ret=
urn references to elements.<div><br></div></div><br><div class=3D"gmail_quo=
te"><div dir=3D"ltr">Le=C2=A0jeu. 19 juil. 2018 =C3=A0=C2=A014:47, Richard =
Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com">hodges.r@gmail.com</a>&gt;=
 a =C3=A9crit=C2=A0:<br></div><blockquote class=3D"gmail_quote" style=3D"ma=
rgin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"lt=
r"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Thu, 19 Jul 2018 =
at 14:20, Olivier S. &lt;<a href=3D"mailto:olivier.sohn@gmail.com" target=
=3D"_blank">olivier.sohn@gmail.com</a>&gt; wrote:<br></div><blockquote clas=
s=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pad=
ding-left:1ex"><div dir=3D"auto"><div></div><div>That would work too but if=
 we want to sample some strings instead if ints then we have to use a map. =
(Or a vector of pairs but that complicates the implementation a bit)</div><=
div><br></div></div></blockquote><div><br></div><div>Hi Oliver,</div><div><=
br></div><div>I think the salient point is that regardless of the actual da=
ta, the only action we are interested in is finding the index matching the =
selected probability. Having done that we can use the index to find the rea=
l data in whatever data structure is using it.</div><div><br></div><div>If =
you were using a std::multimap&lt;float, std::reference_wrapper&lt;std::str=
ing&gt;&gt; as the totals container, that would work too. You might have to=
 change the call to lower_bound, that&#39;s all.</div><div><br></div><div>I=
n this case you&#39;d want to return either the iterator or the dereference=
d iterator rather than an index I guess.</div><div><br></div><div>It&#39;s =
worth remembering that since values are stored in the totals container in a=
scending order, as would be in the indecies (in the given case), this argue=
s strongly against a map or multimap since:</div><div>1. you pay the LogN c=
ost of insertion, which you do not with an array or vector,</div><div>2. in=
 the given examples, we&#39;re storing ascending indexes as the mapped_type=
 in the map. Since they&#39;re ascending and consecutive we may as well use=
 the indecies of an array and avoid paying the cost of=C2=A0 a lookup.</div=
><div><br></div><div>I would still go for the array version, even if I had =
to store a pair (or tuple). All it would require would be a custom predicat=
e on the lower_bound call.</div><div>=C2=A0<br></div><blockquote class=3D"g=
mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=
eft:1ex"><div dir=3D"auto"><div></div><div>Olivier.=C2=A0</div><div><br>On =
19 juil. 2018, at 13:32, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmai=
l.com" target=3D"_blank">hodges.r@gmail.com</a>&gt; wrote:<br><br></div><bl=
ockquote type=3D"cite"><div><div dir=3D"ltr"><br><br><div class=3D"gmail_qu=
ote"><div dir=3D"ltr">On Thu, 19 Jul 2018 at 10:11, Olivier S. &lt;<a href=
=3D"mailto:olivier.sohn@gmail.com" target=3D"_blank">olivier.sohn@gmail.com=
</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:=
0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">He=
llo,<div><br></div><div>This is the motivating example:</div></div></blockq=
uote><div><br></div><div>I think I&#39;d be inclined to transform the proba=
bilities into an array of limits and perform the upper_bound on that:</div>=
<div><br></div><div><pre style=3D"background-color:rgb(43,43,43);color:rgb(=
169,183,198);font-family:&quot;DejaVu Sans Mono&quot;;font-size:11.3pt"><sp=
an style=3D"color:rgb(128,128,128)">//<br></span><span style=3D"color:rgb(1=
28,128,128)">// Created by rhodges on 19/07/18.<br></span><span style=3D"co=
lor:rgb(128,128,128)">//<br></span><span style=3D"color:rgb(128,128,128)"><=
br></span><span style=3D"color:rgb(187,181,41)">#include </span><span style=
=3D"color:rgb(106,135,89)">&lt;cassert&gt;<br></span><span style=3D"color:r=
gb(187,181,41)">#include </span><span style=3D"color:rgb(106,135,89)">&lt;i=
ostream&gt;<br></span><span style=3D"color:rgb(187,181,41)">#include </span=
><span style=3D"color:rgb(106,135,89)">&lt;vector&gt;<br></span><span style=
=3D"color:rgb(187,181,41)">#include </span><span style=3D"color:rgb(106,135=
,89)">&lt;algorithm&gt;<br></span><span style=3D"color:rgb(187,181,41)">#in=
clude </span><span style=3D"color:rgb(106,135,89)">&lt;iterator&gt;<br></sp=
an><span style=3D"color:rgb(187,181,41)">#include </span><span style=3D"col=
or:rgb(106,135,89)">&lt;type_traits&gt;<br></span><span style=3D"color:rgb(=
187,181,41)">#include </span><span style=3D"color:rgb(106,135,89)">&lt;arra=
y&gt;<br></span><span style=3D"color:rgb(106,135,89)"><br></span><span styl=
e=3D"color:rgb(106,135,89)"><br></span><span style=3D"color:rgb(128,128,128=
)">/// Convert a container of probabilities into a container of ascending i=
nterval limits<br></span><span style=3D"color:rgb(204,120,50);font-weight:b=
old">template</span>&lt;<span style=3D"color:rgb(204,120,50);font-weight:bo=
ld">class </span><span style=3D"color:rgb(185,188,209)">Container</span>&gt=
;<br><span style=3D"color:rgb(204,120,50);font-weight:bold">auto </span><sp=
an style=3D"color:rgb(255,198,109)">to_limits</span>(<span style=3D"color:r=
gb(185,188,209)">Container </span>container) {<br>    <span style=3D"color:=
rgb(204,120,50);font-weight:bold">using </span><span style=3D"color:rgb(185=
,188,209)">container_type </span>=3D <span style=3D"color:rgb(181,182,227)"=
>std</span>::<span style=3D"color:rgb(185,188,209)">decay_t</span>&lt;<span=
 style=3D"color:rgb(185,188,209)">Container</span>&gt;<span style=3D"color:=
rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    </sp=
an><span style=3D"color:rgb(204,120,50);font-weight:bold">using </span><spa=
n style=3D"color:rgb(185,188,209)">value_type </span>=3D <span style=3D"col=
or:rgb(204,120,50);font-weight:bold">typename </span><span style=3D"color:r=
gb(185,188,209)">container_type</span>::<span style=3D"color:rgb(185,188,20=
9)">value_type</span><span style=3D"color:rgb(204,120,50)">;<br></span><spa=
n style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,1=
20,50);font-weight:bold">auto </span>to_total =3D [total =3D <span style=3D=
"color:rgb(185,188,209)">value_type</span>(<span style=3D"color:rgb(104,151=
,187)">0</span>)](<span style=3D"color:rgb(204,120,50);font-weight:bold">au=
to </span>&amp;&amp;x) <span style=3D"color:rgb(204,120,50);font-weight:bol=
d">mutable </span>{<br>        total +=3D x<span style=3D"color:rgb(204,120=
,50)">;<br></span><span style=3D"color:rgb(204,120,50)">        </span><spa=
n style=3D"color:rgb(204,120,50);font-weight:bold">return </span>total<span=
 style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,1=
20,50)">    </span>}<span style=3D"color:rgb(204,120,50)">;<br></span><span=
 style=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,12=
0,50)">    </span><span style=3D"color:rgb(181,182,227)">std</span>::transf=
orm(<span style=3D"color:rgb(181,182,227)">std</span>::begin(container)<spa=
n style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(181,182=
,227)">std</span>::end(container)<span style=3D"color:rgb(204,120,50)">,<br=
></span><span style=3D"color:rgb(204,120,50)">                   </span><sp=
an style=3D"color:rgb(181,182,227)">std</span>::begin(container)<span style=
=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)=
">                   </span>to_total)<span style=3D"color:rgb(204,120,50)">=
;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><span style=3D=
"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50);fon=
t-weight:bold">return </span>container<span style=3D"color:rgb(204,120,50)"=
>;<br></span>}<span style=3D"color:rgb(204,120,50)">;<br></span><span style=
=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50);=
font-weight:bold">template</span>&lt;<span style=3D"color:rgb(204,120,50);f=
ont-weight:bold">class </span><span style=3D"color:rgb(185,188,209)">Contai=
ner</span>&gt;<br><span style=3D"color:rgb(204,120,50);font-weight:bold">st=
ruct </span><span style=3D"color:rgb(181,182,227)">assert_not_empty </span>=
{<br>    <span style=3D"color:rgb(255,198,109)">assert_not_empty</span>(<sp=
an style=3D"color:rgb(185,188,209)">Container </span><span style=3D"color:r=
gb(204,120,50);font-weight:bold">const </span>&amp;container) <span style=
=3D"color:rgb(204,120,50);font-weight:bold">noexcept </span>{<br>        <s=
pan style=3D"color:rgb(144,139,37)">assert</span>(!container.<span style=3D=
"color:rgb(152,118,170);font-style:italic">empty</span>())<span style=3D"co=
lor:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    =
</span>}<br>}<span style=3D"color:rgb(204,120,50)">;<br></span><span style=
=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50);=
font-weight:bold">template</span>&lt;<span style=3D"color:rgb(204,120,50);f=
ont-weight:bold">class </span><span style=3D"color:rgb(185,188,209)">Type</=
span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rg=
b(181,182,227)">std</span>::<span style=3D"color:rgb(185,188,209)">size_t <=
/span><span style=3D"color:rgb(152,118,170);font-style:italic">N</span>&gt;=
<br><span style=3D"color:rgb(204,120,50);font-weight:bold">struct </span><s=
pan style=3D"color:rgb(181,182,227)">assert_not_empty</span>&lt;<span style=
=3D"color:rgb(181,182,227)">std</span>::<span style=3D"color:rgb(181,182,22=
7)">array</span>&lt;<span style=3D"color:rgb(185,188,209)">Type</span><span=
 style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(152,118,=
170);font-style:italic">N</span>&gt;&gt; {<br>    <span style=3D"color:rgb(=
204,120,50);font-weight:bold">constexpr </span><span style=3D"color:rgb(255=
,198,109)">assert_not_empty</span>(<span style=3D"color:rgb(181,182,227)">s=
td</span>::<span style=3D"color:rgb(181,182,227)">array</span>&lt;<span sty=
le=3D"color:rgb(185,188,209)">Type</span><span style=3D"color:rgb(204,120,5=
0)">, </span><span style=3D"color:rgb(152,118,170);font-style:italic">N</sp=
an>&gt; <span style=3D"color:rgb(204,120,50);font-weight:bold">const </span=
>&amp;container) <span style=3D"color:rgb(204,120,50);font-weight:bold">noe=
xcept </span>{<br>        <span style=3D"color:rgb(204,120,50);font-weight:=
bold">static_assert</span>(!container.empty())<span style=3D"color:rgb(204,=
120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    </span>}<br>=
}<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb=
(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50)"><br></span><=
span style=3D"color:rgb(128,128,128)">/// given an ascending container of l=
imits, choose one with probability equal to 1.0 / max(limits)<br></span><sp=
an style=3D"color:rgb(128,128,128)">/// @returns an integer containing the =
index of the chosen limit<br></span><span style=3D"color:rgb(204,120,50);fo=
nt-weight:bold">template</span>&lt;<span style=3D"color:rgb(204,120,50);fon=
t-weight:bold">class </span><span style=3D"color:rgb(185,188,209)">Containe=
r</span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color=
:rgb(204,120,50);font-weight:bold">class </span><span style=3D"color:rgb(18=
5,188,209)">Value</span>&gt;<br><span style=3D"color:rgb(204,120,50);font-w=
eight:bold">int </span><span style=3D"color:rgb(255,198,109)">sample_limits=
</span>(<span style=3D"color:rgb(185,188,209)">Container </span>&amp;&amp;t=
otals<span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rg=
b(185,188,209)">Value </span>&amp;&amp;prob) {<br><br>    <span style=3D"co=
lor:rgb(128,128,128)">// precondition<br></span><span style=3D"color:rgb(12=
8,128,128)">    </span>assert_not_empty{totals}<span style=3D"color:rgb(204=
,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><spa=
n style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(128,1=
28,128)">// convert prob (range 0-1) into a total.<br></span><span style=3D=
"color:rgb(128,128,128)"><br></span><span style=3D"color:rgb(128,128,128)">=
    </span><span style=3D"color:rgb(204,120,50);font-weight:bold">auto </sp=
an>target =3D prob * totals.<span style=3D"color:rgb(152,118,170);font-styl=
e:italic">back</span>()<span style=3D"color:rgb(204,120,50)">;<br></span><s=
pan style=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204=
,120,50)">    </span><span style=3D"color:rgb(204,120,50);font-weight:bold"=
>auto </span>cap =3D [&amp;totals](<span style=3D"color:rgb(204,120,50);fon=
t-weight:bold">auto </span>iter) {<br>        <span style=3D"color:rgb(204,=
120,50);font-weight:bold">if </span>(iter =3D=3D <span style=3D"color:rgb(1=
81,182,227)">std</span>::end(totals))<br>            <span style=3D"color:r=
gb(204,120,50);font-weight:bold">return </span><span style=3D"color:rgb(181=
,182,227)">std</span>::prev(iter)<span style=3D"color:rgb(204,120,50)">;<br=
></span><span style=3D"color:rgb(204,120,50)">        </span><span style=3D=
"color:rgb(204,120,50);font-weight:bold">else<br></span><span style=3D"colo=
r:rgb(204,120,50);font-weight:bold">            return </span>iter<span sty=
le=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,5=
0)">    </span>}<span style=3D"color:rgb(204,120,50)">;<br></span><span sty=
le=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50=
)">    </span><span style=3D"color:rgb(204,120,50);font-weight:bold">auto <=
/span>first =3D <span style=3D"color:rgb(181,182,227)">std</span>::cbegin(t=
otals)<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"colo=
r:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,120,50);font-wei=
ght:bold">auto </span>last =3D <span style=3D"color:rgb(181,182,227)">std</=
span>::cend(totals)<span style=3D"color:rgb(204,120,50)">;<br></span><span =
style=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120=
,50)">    </span><span style=3D"color:rgb(204,120,50);font-weight:bold">aut=
o </span>index =3D <span style=3D"color:rgb(181,182,227)">std</span>::dista=
nce(first<span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"c=
olor:rgb(204,120,50)">                               </span>cap(<span style=
=3D"color:rgb(181,182,227)">std</span>::lower_bound(first<span style=3D"col=
or:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">     =
                                               </span>last<span style=3D"co=
lor:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">    =
                                                </span>target)))<span style=
=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)=
">    </span><span style=3D"color:rgb(204,120,50);font-weight:bold">return =
int</span>(index)<span style=3D"color:rgb(204,120,50)">;<br></span>}<br><br=
><span style=3D"color:rgb(204,120,50);font-weight:bold">int </span><span st=
yle=3D"color:rgb(255,198,109)">main</span>() {<br><br>    <span style=3D"co=
lor:rgb(128,128,128)">// The goal is to sample among ints from 0 to 9, with=
 different probability for each int.<br></span><span style=3D"color:rgb(128=
,128,128)">    // The probability of i is proba[i] / sum(proba).<br></span>=
<span style=3D"color:rgb(128,128,128)">    </span><span style=3D"color:rgb(=
181,182,227)">std</span>::<span style=3D"color:rgb(181,182,227)">array</spa=
n>&lt;<span style=3D"color:rgb(204,120,50);font-weight:bold">float</span><s=
pan style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,1=
51,187)">10</span>&gt; proba{{<span style=3D"color:rgb(104,151,187)">0.01</=
span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rg=
b(104,151,187)">0.05</span><span style=3D"color:rgb(204,120,50)">, </span><=
span style=3D"color:rgb(104,151,187)">0.01</span><span style=3D"color:rgb(2=
04,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.15</span><spa=
n style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151=
,187)">0.1</span><span style=3D"color:rgb(204,120,50)">, </span><span style=
=3D"color:rgb(104,151,187)">0.19</span><span style=3D"color:rgb(204,120,50)=
">, </span><span style=3D"color:rgb(104,151,187)">0.19</span><span style=3D=
"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.1=
</span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:=
rgb(104,151,187)">0.19</span><span style=3D"color:rgb(204,120,50)">, </span=
><span style=3D"color:rgb(104,151,187)">0.01</span>}}<span style=3D"color:r=
gb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    </spa=
n><span style=3D"color:rgb(204,120,50);font-weight:bold">auto </span>limits=
 =3D to_limits(proba)<span style=3D"color:rgb(204,120,50)">;<br></span><spa=
n style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,1=
20,50);font-weight:bold">for </span>(<span style=3D"color:rgb(204,120,50);f=
ont-weight:bold">auto </span>v : <span style=3D"color:rgb(181,182,227)">std=
</span>::vector&lt;<span style=3D"color:rgb(204,120,50);font-weight:bold">f=
loat</span>&gt;{{<br>                                             <span sty=
le=3D"color:rgb(104,151,187)">0.f</span><span style=3D"color:rgb(204,120,50=
)">,<br></span><span style=3D"color:rgb(204,120,50)">                      =
                       </span><span style=3D"color:rgb(104,151,187)">0.0000=
1f</span><span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"c=
olor:rgb(204,120,50)">                                             </span><=
span style=3D"color:rgb(104,151,187)">0.02f</span><span style=3D"color:rgb(=
204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">            =
                                 </span><span style=3D"color:rgb(104,151,18=
7)">0.5f</span><span style=3D"color:rgb(204,120,50)">,<br></span><span styl=
e=3D"color:rgb(204,120,50)">                                             </=
span><span style=3D"color:rgb(104,151,187)">0.98f</span><span style=3D"colo=
r:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">      =
                                       </span><span style=3D"color:rgb(104,=
151,187)">1.f</span><span style=3D"color:rgb(204,120,50)">,  </span><span s=
tyle=3D"color:rgb(128,128,128)">// on upper bound<br></span><span style=3D"=
color:rgb(128,128,128)">                                             </span=
><span style=3D"color:rgb(104,151,187)">1.00001f  </span><span style=3D"col=
or:rgb(128,128,128)">// beyond upper bound<br></span><span style=3D"color:r=
gb(128,128,128)">                                     </span>}}) {<br>     =
   <span style=3D"color:rgb(181,182,227)">std</span>::cout <span style=3D"c=
olor:rgb(95,140,138)">&lt;&lt; </span>sample_limits(limits<span style=3D"co=
lor:rgb(204,120,50)">, </span>v) <span style=3D"color:rgb(95,140,138)">&lt;=
&lt; </span><span style=3D"color:rgb(181,182,227)">std</span>::endl<span st=
yle=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,=
50)">    </span>}<br>    <br>    <span style=3D"color:rgb(204,120,50);font-=
weight:bold">return </span><span style=3D"color:rgb(104,151,187)">0</span><=
span style=3D"color:rgb(204,120,50)">;<br></span>}<br><br></pre><br></div><=
div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8e=
x;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><br></=
div><div>




<span></span>





<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p1" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(81,182,255);background-color:rgb(0,=
0,0)">#include <span class=3D"m_1012224171286952715m_5052467569217688009m_2=
612803408380067011gmail-s1" style=3D"color:rgb(180,38,26)">&lt;cassert&gt;<=
/span></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p2" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(180,38,26);background-color:rgb(0,0=
,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083=
80067011gmail-s2" style=3D"color:rgb(81,182,255)">#include </span>&lt;iostr=
eam&gt;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p1" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(81,182,255);background-color:rgb(0,=
0,0)">#include <span class=3D"m_1012224171286952715m_5052467569217688009m_2=
612803408380067011gmail-s1" style=3D"color:rgb(180,38,26)">&lt;map&gt;</spa=
n></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p1" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(81,182,255);background-color:rgb(0,=
0,0)">#include <span class=3D"m_1012224171286952715m_5052467569217688009m_2=
612803408380067011gmail-s1" style=3D"color:rgb(180,38,26)">&lt;vector&gt;</=
span></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Helvetica;background-color:rgb(0,0,0);min-height:14=
px"><br></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p4" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(255,178,109);background-color:rgb(0=
,0,0)">using<span class=3D"m_1012224171286952715m_5052467569217688009m_2612=
803408380067011gmail-s3" style=3D"color:rgb(231,254,226)"> </span>namespace=
<span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067=
011gmail-s3" style=3D"color:rgb(231,254,226)"> std;</span></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Helvetica;background-color:rgb(0,0,0);min-height:14=
px"><br></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-s4" style=3D"color:rgb(255,178,109)">using</span> Intervals=
 =3D map&lt;<span class=3D"m_1012224171286952715m_5052467569217688009m_2612=
803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">float</span>,<spa=
n class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011g=
mail-s4" style=3D"color:rgb(255,178,109)">int</span>&gt;;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Helvetica;background-color:rgb(0,0,0);min-height:14=
px"><br></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-s4" style=3D"color:rgb(255,178,109)">int</span> sample(Inte=
rvals <span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408=
380067011gmail-s4" style=3D"color:rgb(255,178,109)">const</span> &amp; inte=
rvals, <span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-s4" style=3D"color:rgb(255,178,109)">float</span> randomFlo=
at) {</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>assert(!interva=
ls.empty());</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"=
m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s4" st=
yle=3D"color:rgb(255,178,109)">auto</span> it =3D intervals.lower_bound(ran=
domFloat);</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"=
m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s4" st=
yle=3D"color:rgb(255,178,109)">if</span>(it =3D=3D intervals.end()) {</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// we return the last eleme=
nt of the map:</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><=
span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083800670=
11gmail-s4" style=3D"color:rgb(255,178,109)">return</span> crbegin(interval=
s)-&gt;second;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// this is what I would pre=
fer writing:</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// return intervals.back();=
</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>}</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"=
m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s4" st=
yle=3D"color:rgb(255,178,109)">return</span> it-&gt;second;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)">}</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Helvetica;background-color:rgb(0,0,0);min-height:14=
px"><br></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-s4" style=3D"color:rgb(255,178,109)">int</span> main() {</p=
>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span></span>// The goal is to sample among ints from =
0 to 9, with different probability for each int.</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Helvetica;background-color:rgb(0,0,0);min-height:14=
px"><br></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span></span>// The probability of i is proba[i].</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p7" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(212,212,212);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_10122=
24171286952715m_5052467569217688009m_2612803408380067011gmail-Apple-convert=
ed-space">=C2=A0 =C2=A0 </span>std::array&lt;</span><span class=3D"m_101222=
4171286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"c=
olor:rgb(255,178,109)">float</span><span class=3D"m_1012224171286952715m_50=
52467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,=
226)">,</span>10<span class=3D"m_1012224171286952715m_5052467569217688009m_=
2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">&gt; proba {{=
</span>0.01<span class=3D"m_1012224171286952715m_5052467569217688009m_26128=
03408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.05<span =
class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)">,</span>0.01<span class=3D"m_101222=
4171286952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"c=
olor:rgb(231,254,226)">,</span>0.15<span class=3D"m_1012224171286952715m_50=
52467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,=
226)">,</span>0.1<span class=3D"m_1012224171286952715m_5052467569217688009m=
_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.19=
<span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067=
011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.19<span class=3D"m_=
1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s3" styl=
e=3D"color:rgb(231,254,226)">,</span>0.1<span class=3D"m_101222417128695271=
5m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231=
,254,226)">,</span>0.19<span class=3D"m_1012224171286952715m_50524675692176=
88009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</spa=
n>0.01<span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408=
380067011gmail-s3" style=3D"color:rgb(231,254,226)">}};</span></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Helvetica;background-color:rgb(0,0,0);min-height:14=
px"><br></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span></span>// The sum of probabilities are 1, so we =
can conceptually partition</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span></span>// the [0,1] interval in 10 intervals of =
lengths proba[i].</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"=
m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s5" st=
yle=3D"color:rgb(0,193,0)">//</span></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span></span>// we only need to store the upper bounds=
 of these intervals,</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span></span>// because the lower bound of interval n =
is the upper bound of interval n-1,</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span></span>// and because we know that the first low=
er bound is 0:</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>Intervals inter=
vals;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"=
m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s4" st=
yle=3D"color:rgb(255,178,109)">int</span> i =3D <span class=3D"m_1012224171=
286952715m_5052467569217688009m_2612803408380067011gmail-s6" style=3D"color=
:rgb(212,212,212)">0</span>;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"=
m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s4" st=
yle=3D"color:rgb(255,178,109)">float</span> upperbound =3D <span class=3D"m=
_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" sty=
le=3D"color:rgb(212,212,212)">0.f</span>;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"=
m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s4" st=
yle=3D"color:rgb(255,178,109)">for</span>(<span class=3D"m_1012224171286952=
715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color:rgb(2=
55,178,109)">auto</span> p : proba) {</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>u=
pperbound +=3D p;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>i=
ntervals.emplace(upperbound,i);</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// Note that the last key, =
due to floating point rounding errors, will be</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span>// close to 1.f but not nec=
essarilly 1.f.</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>+=
+i;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>}</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p8" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0);min-height:14px"><span class=3D"m_1012224171286952715m_50524675692176=
88009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=C2=A0 =C2=A0<=
/span></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span></span>// Then, to sample ints, we generate a ra=
ndom numbers between 0 and 1,</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p6" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(0,193,0);background-color:rgb(0,0,0=
)"><span class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380=
067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span></span>// and lookup the correponding interval:<=
/p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Helvetica;background-color:rgb(0,0,0);min-height:14=
px"><br></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span><span class=3D"=
m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s4" st=
yle=3D"color:rgb(255,178,109)">for</span>(<span class=3D"m_1012224171286952=
715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color:rgb(2=
55,178,109)">auto</span> v : vector&lt;<span class=3D"m_1012224171286952715=
m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color:rgb(255,=
178,109)">float</span>&gt;{{</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><=
span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083800670=
11gmail-s6" style=3D"color:rgb(212,212,212)">0.f</span>,</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><=
span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083800670=
11gmail-s6" style=3D"color:rgb(212,212,212)">0.00001f</span>,</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><=
span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083800670=
11gmail-s6" style=3D"color:rgb(212,212,212)">0.02f</span>,</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><=
span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083800670=
11gmail-s6" style=3D"color:rgb(212,212,212)">0.5f</span>,</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><=
span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083800670=
11gmail-s6" style=3D"color:rgb(212,212,212)">0.98f</span>,</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><=
span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083800670=
11gmail-s6" style=3D"color:rgb(212,212,212)">1.f</span>,</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><=
span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083800670=
11gmail-s6" style=3D"color:rgb(212,212,212)">1.00001f</span>}})</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>{</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>c=
out &lt;&lt; sample(intervals, v) &lt;&lt; endl;</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-Apple-converted-space">=C2=A0 =C2=A0 </span>}</p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p3" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Helvetica;background-color:rgb(0,0,0);min-height:14=
px"><br></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p4" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(255,178,109);background-color:rgb(0=
,0,0)"><span class=3D"m_1012224171286952715m_5052467569217688009m_261280340=
8380067011gmail-s3" style=3D"color:rgb(231,254,226)"><span class=3D"m_10122=
24171286952715m_5052467569217688009m_2612803408380067011gmail-Apple-convert=
ed-space">=C2=A0 =C2=A0 </span></span>return<span class=3D"m_10122241712869=
52715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb=
(231,254,226)"> </span><span class=3D"m_1012224171286952715m_50524675692176=
88009m_2612803408380067011gmail-s6" style=3D"color:rgb(212,212,212)">0</spa=
n><span class=3D"m_1012224171286952715m_5052467569217688009m_26128034083800=
67011gmail-s3" style=3D"color:rgb(231,254,226)">;</span></p>
<p class=3D"m_1012224171286952715m_5052467569217688009m_2612803408380067011=
gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-variant-east=
-asian:normal;font-weight:normal;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(231,254,226);background-color:rgb(0=
,0,0)">}</p><p class=3D"m_1012224171286952715m_5052467569217688009m_2612803=
408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:normal;font-=
variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:=
12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);background=
-color:rgb(0,0,0)"><br></p>


</div><div><br></div><div>Olivier</div><div><br></div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr">Le=C2=A0mer. 18 juil. 2018 =C3=A0=C2=A019=
:53, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com" target=3D"_bl=
ank">hodges.r@gmail.com</a>&gt; a =C3=A9crit=C2=A0:<br></div><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;p=
adding-left:1ex"><div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr">On Wed, 18 Jul 2018 at 14:10, Olivier Sohn &lt;<a href=3D"mailto=
:olivier.sohn@gmail.com" target=3D"_blank">olivier.sohn@gmail.com</a>&gt; w=
rote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hello,<div><=
br></div><div>I recently encountered an algorithm where I needed to access =
the map&#39;s last element, knowing the map was non-empty. But I found that=
 there is no straightforward way to access the last (or first) element of a=
 map.</div></div></blockquote><div><br></div><div>Without wishing to sound =
too controversial, I am wondering whether this algorithm could have been ex=
pressed in a different way.</div><div><br></div><div>Wanting a reference to=
 the &quot;back&quot; of a container suggests a need to access the least/gr=
eatest/earliest/latest of a group of things.=C2=A0</div><div><br></div><div=
>Are you able to share the motivating use case, as there are other tools fo=
r this in the standard library?<br></div><div>=C2=A0</div><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex"><div dir=3D"ltr"><div><br></div><div>So as suggested by this =
process :=C2=A0<a href=3D"https://isocpp.org/std/submit-a-proposal" target=
=3D"_blank">https://isocpp.org/std/submit-a-proposal</a> I&#39;d like to pr=
opose the following: add the front() and back() methods, with const and non=
 const versions, to std::(multi)map and std::(multi)set, to return a (const=
-) reference to the last / first element(s).</div><div><br></div><div>Like =
with other containers implementing these methods, no guaranty would be give=
n when the container is empty.</div><div><br></div><div>What do you think?<=
/div><div><br></div><div>Cheers,</div><div>Olivier</div><div><br></div></di=
v>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-=
45df-9536-e0bfab52962b%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3=
ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfooter"=
 target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-propo=
sals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com</=
a>.<br>
</blockquote></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsi=
bvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfoo=
ter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-p=
roposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gma=
il.com</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPU=
Wj%3DvBNSJCx7-pDfYuA%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Df=
ooter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std=
-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail=
..gmail.com</a>.<br>
</div></blockquote></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7F5-4C90-AD0E-CFEAC5F95CD6%=
40gmail.com?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7F5-4=
C90-AD0E-CFEAC5F95CD6%40gmail.com</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hYvMBhNXTPdBHsLEvbvvU71azApw%2B=
SQiGmZNZd4ps%2BWhw%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfoo=
ter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-p=
roposals/CALvx3hYvMBhNXTPdBHsLEvbvvU71azApw%2BSQiGmZNZd4ps%2BWhw%40mail.gma=
il.com</a>.<br>
</blockquote></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CANAj80uD9%2BBA6-dC66ixmTvoqcsAknOLvj=
ZsT59d0HnkUyd0cA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80uD9%2BBA=
6-dC66ixmTvoqcsAknOLvjZsT59d0HnkUyd0cA%40mail.gmail.com</a>.<br />

--000000000000026b3c05715a47ef--

.


Author: Richard Hodges <hodges.r@gmail.com>
Date: Fri, 20 Jul 2018 01:48:59 +0200
Raw View
--0000000000006a250c057162d389
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Thu, 19 Jul 2018 at 15:37, Olivier S. <olivier.sohn@gmail.com> wrote:

> Sure, there are other ways to implement that, but this example shows that
> there a legitimate uses for back and front in a map, to return references
> to elements.
>

This is unproven since my sample_limits() function does not use front() or
back(). I remain unconvinced of "legitimate uses". Remember that the STL is
~30 years old. Don't you think that someone would have noticed if there was
a deficiency like this?



>
>
> Le jeu. 19 juil. 2018 =C3=A0 14:47, Richard Hodges <hodges.r@gmail.com> a
> =C3=A9crit :
>
>>
>>
>> On Thu, 19 Jul 2018 at 14:20, Olivier S. <olivier.sohn@gmail.com> wrote:
>>
>>> That would work too but if we want to sample some strings instead if
>>> ints then we have to use a map. (Or a vector of pairs but that complica=
tes
>>> the implementation a bit)
>>>
>>>
>> Hi Oliver,
>>
>> I think the salient point is that regardless of the actual data, the onl=
y
>> action we are interested in is finding the index matching the selected
>> probability. Having done that we can use the index to find the real data=
 in
>> whatever data structure is using it.
>>
>> If you were using a std::multimap<float,
>> std::reference_wrapper<std::string>> as the totals container, that would
>> work too. You might have to change the call to lower_bound, that's all.
>>
>> In this case you'd want to return either the iterator or the dereference=
d
>> iterator rather than an index I guess.
>>
>> It's worth remembering that since values are stored in the totals
>> container in ascending order, as would be in the indecies (in the given
>> case), this argues strongly against a map or multimap since:
>> 1. you pay the LogN cost of insertion, which you do not with an array or
>> vector,
>> 2. in the given examples, we're storing ascending indexes as the
>> mapped_type in the map. Since they're ascending and consecutive we may a=
s
>> well use the indecies of an array and avoid paying the cost of  a lookup=
..
>>
>> I would still go for the array version, even if I had to store a pair (o=
r
>> tuple). All it would require would be a custom predicate on the lower_bo=
und
>> call.
>>
>>
>>> Olivier.
>>>
>>> On 19 juil. 2018, at 13:32, Richard Hodges <hodges.r@gmail.com> wrote:
>>>
>>>
>>>
>>> On Thu, 19 Jul 2018 at 10:11, Olivier S. <olivier.sohn@gmail.com> wrote=
:
>>>
>>>> Hello,
>>>>
>>>> This is the motivating example:
>>>>
>>>
>>> I think I'd be inclined to transform the probabilities into an array of
>>> limits and perform the upper_bound on that:
>>>
>>> //
>>> // Created by rhodges on 19/07/18.
>>> //
>>>
>>> #include <cassert>
>>> #include <iostream>
>>> #include <vector>
>>> #include <algorithm>
>>> #include <iterator>
>>> #include <type_traits>
>>> #include <array>
>>>
>>>
>>> /// Convert a container of probabilities into a container of ascending =
interval limits
>>> template<class Container>
>>> auto to_limits(Container container) {
>>>     using container_type =3D std::decay_t<Container>;
>>>     using value_type =3D typename container_type::value_type;
>>>     auto to_total =3D [total =3D value_type(0)](auto &&x) mutable {
>>>         total +=3D x;
>>>         return total;
>>>     };
>>>
>>>     std::transform(std::begin(container), std::end(container),
>>>                    std::begin(container),
>>>                    to_total);
>>>
>>>     return container;
>>> };
>>>
>>> template<class Container>
>>> struct assert_not_empty {
>>>     assert_not_empty(Container const &container) noexcept {
>>>         assert(!container.empty());
>>>     }
>>> };
>>>
>>> template<class Type, std::size_t N>
>>> struct assert_not_empty<std::array<Type, N>> {
>>>     constexpr assert_not_empty(std::array<Type, N> const &container) no=
except {
>>>         static_assert(!container.empty());
>>>     }
>>> };
>>>
>>>
>>> /// given an ascending container of limits, choose one with probability=
 equal to 1.0 / max(limits)
>>> /// @returns an integer containing the index of the chosen limit
>>> template<class Container, class Value>
>>> int sample_limits(Container &&totals, Value &&prob) {
>>>
>>>     // precondition
>>>     assert_not_empty{totals};
>>>
>>>     // convert prob (range 0-1) into a total.
>>>
>>>     auto target =3D prob * totals.back();
>>>
>>>     auto cap =3D [&totals](auto iter) {
>>>         if (iter =3D=3D std::end(totals))
>>>             return std::prev(iter);
>>>         else
>>>             return iter;
>>>     };
>>>
>>>     auto first =3D std::cbegin(totals);
>>>     auto last =3D std::cend(totals);
>>>
>>>     auto index =3D std::distance(first,
>>>                                cap(std::lower_bound(first,
>>>                                                     last,
>>>                                                     target)));
>>>     return int(index);
>>> }
>>>
>>> int main() {
>>>
>>>     // The goal is to sample among ints from 0 to 9, with different pro=
bability for each int.
>>>     // The probability of i is proba[i] / sum(proba).
>>>     std::array<float, 10> proba{{0.01, 0.05, 0.01, 0.15, 0.1, 0.19, 0.1=
9, 0.1, 0.19, 0.01}};
>>>     auto limits =3D to_limits(proba);
>>>     for (auto v : std::vector<float>{{
>>>                                              0.f,
>>>                                              0.00001f,
>>>                                              0.02f,
>>>                                              0.5f,
>>>                                              0.98f,
>>>                                              1.f,  // on upper bound
>>>                                              1.00001f  // beyond upper =
bound
>>>                                      }}) {
>>>         std::cout << sample_limits(limits, v) << std::endl;
>>>     }
>>>
>>>     return 0;
>>> }
>>>
>>>
>>>
>>>
>>>>
>>>> #include <cassert>
>>>>
>>>> #include <iostream>
>>>>
>>>> #include <map>
>>>>
>>>> #include <vector>
>>>>
>>>>
>>>> using namespace std;
>>>>
>>>>
>>>> using Intervals =3D map<float,int>;
>>>>
>>>>
>>>> int sample(Intervals const & intervals, float randomFloat) {
>>>>
>>>>     assert(!intervals.empty());
>>>>
>>>>     auto it =3D intervals.lower_bound(randomFloat);
>>>>
>>>>     if(it =3D=3D intervals.end()) {
>>>>
>>>>         // we return the last element of the map:
>>>>
>>>>         return crbegin(intervals)->second;
>>>>
>>>>         // this is what I would prefer writing:
>>>>
>>>>         // return intervals.back();
>>>>
>>>>     }
>>>>
>>>>     return it->second;
>>>>
>>>> }
>>>>
>>>>
>>>> int main() {
>>>>
>>>>     // The goal is to sample among ints from 0 to 9, with different
>>>> probability for each int.
>>>>
>>>>
>>>>     // The probability of i is proba[i].
>>>>
>>>>     std::array<float,10> proba {{0.01,0.05,0.01,0.15,0.1,0.19,0.19,0.1=
,
>>>> 0.19,0.01}};
>>>>
>>>>
>>>>     // The sum of probabilities are 1, so we can conceptually partitio=
n
>>>>
>>>>     // the [0,1] interval in 10 intervals of lengths proba[i].
>>>>
>>>>     //
>>>>
>>>>     // we only need to store the upper bounds of these intervals,
>>>>
>>>>     // because the lower bound of interval n is the upper bound of
>>>> interval n-1,
>>>>
>>>>     // and because we know that the first lower bound is 0:
>>>>
>>>>     Intervals intervals;
>>>>
>>>>     int i =3D 0;
>>>>
>>>>     float upperbound =3D 0.f;
>>>>
>>>>     for(auto p : proba) {
>>>>
>>>>         upperbound +=3D p;
>>>>
>>>>         intervals.emplace(upperbound,i);
>>>>
>>>>         // Note that the last key, due to floating point rounding
>>>> errors, will be
>>>>
>>>>         // close to 1.f but not necessarilly 1.f.
>>>>
>>>>         ++i;
>>>>
>>>>     }
>>>>
>>>>
>>>>
>>>>     // Then, to sample ints, we generate a random numbers between 0
>>>> and 1,
>>>>
>>>>     // and lookup the correponding interval:
>>>>
>>>>
>>>>     for(auto v : vector<float>{{
>>>>
>>>>         0.f,
>>>>
>>>>         0.00001f,
>>>>
>>>>         0.02f,
>>>>
>>>>         0.5f,
>>>>
>>>>         0.98f,
>>>>
>>>>         1.f,
>>>>
>>>>         1.00001f}})
>>>>
>>>>     {
>>>>
>>>>         cout << sample(intervals, v) << endl;
>>>>
>>>>     }
>>>>
>>>>
>>>>     return 0;
>>>>
>>>> }
>>>>
>>>>
>>>>
>>>> Olivier
>>>>
>>>>
>>>> Le mer. 18 juil. 2018 =C3=A0 19:53, Richard Hodges <hodges.r@gmail.com=
> a
>>>> =C3=A9crit :
>>>>
>>>>>
>>>>>
>>>>> On Wed, 18 Jul 2018 at 14:10, Olivier Sohn <olivier.sohn@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> Hello,
>>>>>>
>>>>>> I recently encountered an algorithm where I needed to access the
>>>>>> map's last element, knowing the map was non-empty. But I found that =
there
>>>>>> is no straightforward way to access the last (or first) element of a=
 map.
>>>>>>
>>>>>
>>>>> Without wishing to sound too controversial, I am wondering whether
>>>>> this algorithm could have been expressed in a different way.
>>>>>
>>>>> Wanting a reference to the "back" of a container suggests a need to
>>>>> access the least/greatest/earliest/latest of a group of things.
>>>>>
>>>>> Are you able to share the motivating use case, as there are other
>>>>> tools for this in the standard library?
>>>>>
>>>>>
>>>>>>
>>>>>> So as suggested by this process :
>>>>>> https://isocpp.org/std/submit-a-proposal I'd like to propose the
>>>>>> following: add the front() and back() methods, with const and non co=
nst
>>>>>> versions, to std::(multi)map and std::(multi)set, to return a (const=
-)
>>>>>> reference to the last / first element(s).
>>>>>>
>>>>>> Like with other containers implementing these methods, no guaranty
>>>>>> would be given when the container is empty.
>>>>>>
>>>>>> What do you think?
>>>>>>
>>>>>> Cheers,
>>>>>> Olivier
>>>>>>
>>>>>> --
>>>>>> 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.
>>>>>> To view this discussion on the web visit
>>>>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4=
e-6337-45df-9536-e0bfab52962b%40isocpp.org
>>>>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f=
4e-6337-45df-9536-e0bfab52962b%40isocpp.org?utm_medium=3Demail&utm_source=
=3Dfooter>
>>>>>> .
>>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to a topic in th=
e
>>>>> Google Groups "ISO C++ Standard - Future Proposals" group.
>>>>> To unsubscribe from this topic, visit
>>>>> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6K=
SkQ/unsubscribe
>>>>> .
>>>>> To unsubscribe from this group and all its topics, send an email to
>>>>> std-proposals+unsubscribe@isocpp.org.
>>>>> To post to this group, send email to std-proposals@isocpp.org.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hb=
HNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com
>>>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3h=
bHNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3D=
email&utm_source=3Dfooter>
>>>>> .
>>>>>
>>>> --
>>>> 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.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80t5=
Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gmail.com
>>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80t=
5Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=
=3Demail&utm_source=3Dfooter>
>>>> .
>>>>
>>> --
>>> You received this message because you are subscribed to a topic in the
>>> Google Groups "ISO C++ Standard - Future Proposals" group.
>>> To unsubscribe from this topic, visit
>>> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSk=
Q/unsubscribe
>>> .
>>> To unsubscribe from this group and all its topics, send an email to
>>> std-proposals+unsubscribe@isocpp.org.
>>> To post to this group, send email to std-proposals@isocpp.org.
>>> To view this discussion on the web visit
>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3haNK=
o0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail.gmail.com
>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3haN=
Ko0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail.gmail.com?utm_mediu=
m=3Demail&utm_source=3Dfooter>
>>> .
>>>
>>> --
>>> 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.
>>> To view this discussion on the web visit
>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/583D12C6-E=
7F5-4C90-AD0E-CFEAC5F95CD6%40gmail.com
>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/583D12C6-=
E7F5-4C90-AD0E-CFEAC5F95CD6%40gmail.com?utm_medium=3Demail&utm_source=3Dfoo=
ter>
>>> .
>>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe
>> .
>> To unsubscribe from this group and all its topics, send an email to
>> std-proposals+unsubscribe@isocpp.org.
>> To post to this group, send email to std-proposals@isocpp.org.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hYvMB=
hNXTPdBHsLEvbvvU71azApw%2BSQiGmZNZd4ps%2BWhw%40mail.gmail.com
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hYvM=
BhNXTPdBHsLEvbvvU71azApw%2BSQiGmZNZd4ps%2BWhw%40mail.gmail.com?utm_medium=
=3Demail&utm_source=3Dfooter>
>> .
>>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80uD9%2=
BBA6-dC66ixmTvoqcsAknOLvjZsT59d0HnkUyd0cA%40mail.gmail.com
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANAj80uD9%=
2BBA6-dC66ixmTvoqcsAknOLvjZsT59d0HnkUyd0cA%40mail.gmail.com?utm_medium=3Dem=
ail&utm_source=3Dfooter>
> .
>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CALvx3hZCDa%2BG-5mUObDsk9A3t4%2B6v0teiKe-V9iuOpA=
-smCwVw%40mail.gmail.com.

--0000000000006a250c057162d389
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Thu=
, 19 Jul 2018 at 15:37, Olivier S. &lt;<a href=3D"mailto:olivier.sohn@gmail=
..com">olivier.sohn@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"g=
mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=
eft:1ex"><div dir=3D"ltr">Sure, there are other ways to implement that, but=
 this example shows that there a legitimate uses for back and front in a ma=
p, to return references to elements.</div></blockquote><div><br></div><div>=
This is unproven since my sample_limits() function does not use front() or =
back(). I remain unconvinced of &quot;legitimate uses&quot;. Remember that =
the STL is ~30 years old. Don&#39;t you think that someone would have notic=
ed if there was a deficiency like this?</div><div><br></div><div>=C2=A0</di=
v><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:=
1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><br></div></div><br>=
<div class=3D"gmail_quote"><div dir=3D"ltr">Le=C2=A0jeu. 19 juil. 2018 =C3=
=A0=C2=A014:47, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com" ta=
rget=3D"_blank">hodges.r@gmail.com</a>&gt; a =C3=A9crit=C2=A0:<br></div><bl=
ockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #=
ccc solid;padding-left:1ex"><div dir=3D"ltr"><br><br><div class=3D"gmail_qu=
ote"><div dir=3D"ltr">On Thu, 19 Jul 2018 at 14:20, Olivier S. &lt;<a href=
=3D"mailto:olivier.sohn@gmail.com" target=3D"_blank">olivier.sohn@gmail.com=
</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:=
0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"auto"><=
div></div><div>That would work too but if we want to sample some strings in=
stead if ints then we have to use a map. (Or a vector of pairs but that com=
plicates the implementation a bit)</div><div><br></div></div></blockquote><=
div><br></div><div>Hi Oliver,</div><div><br></div><div>I think the salient =
point is that regardless of the actual data, the only action we are interes=
ted in is finding the index matching the selected probability. Having done =
that we can use the index to find the real data in whatever data structure =
is using it.</div><div><br></div><div>If you were using a std::multimap&lt;=
float, std::reference_wrapper&lt;std::string&gt;&gt; as the totals containe=
r, that would work too. You might have to change the call to lower_bound, t=
hat&#39;s all.</div><div><br></div><div>In this case you&#39;d want to retu=
rn either the iterator or the dereferenced iterator rather than an index I =
guess.</div><div><br></div><div>It&#39;s worth remembering that since value=
s are stored in the totals container in ascending order, as would be in the=
 indecies (in the given case), this argues strongly against a map or multim=
ap since:</div><div>1. you pay the LogN cost of insertion, which you do not=
 with an array or vector,</div><div>2. in the given examples, we&#39;re sto=
ring ascending indexes as the mapped_type in the map. Since they&#39;re asc=
ending and consecutive we may as well use the indecies of an array and avoi=
d paying the cost of=C2=A0 a lookup.</div><div><br></div><div>I would still=
 go for the array version, even if I had to store a pair (or tuple). All it=
 would require would be a custom predicate on the lower_bound call.</div><d=
iv>=C2=A0<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 =
..8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"auto"><div></=
div><div>Olivier.=C2=A0</div><div><br>On 19 juil. 2018, at 13:32, Richard H=
odges &lt;<a href=3D"mailto:hodges.r@gmail.com" target=3D"_blank">hodges.r@=
gmail.com</a>&gt; wrote:<br><br></div><blockquote type=3D"cite"><div><div d=
ir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Thu, 19 J=
ul 2018 at 10:11, Olivier S. &lt;<a href=3D"mailto:olivier.sohn@gmail.com" =
target=3D"_blank">olivier.sohn@gmail.com</a>&gt; wrote:<br></div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol=
id;padding-left:1ex"><div dir=3D"ltr">Hello,<div><br></div><div>This is the=
 motivating example:</div></div></blockquote><div><br></div><div>I think I&=
#39;d be inclined to transform the probabilities into an array of limits an=
d perform the upper_bound on that:</div><div><br></div><div><pre style=3D"b=
ackground-color:rgb(43,43,43);color:rgb(169,183,198);font-family:&quot;Deja=
Vu Sans Mono&quot;;font-size:11.3pt"><span style=3D"color:rgb(128,128,128)"=
>//<br></span><span style=3D"color:rgb(128,128,128)">// Created by rhodges =
on 19/07/18.<br></span><span style=3D"color:rgb(128,128,128)">//<br></span>=
<span style=3D"color:rgb(128,128,128)"><br></span><span style=3D"color:rgb(=
187,181,41)">#include </span><span style=3D"color:rgb(106,135,89)">&lt;cass=
ert&gt;<br></span><span style=3D"color:rgb(187,181,41)">#include </span><sp=
an style=3D"color:rgb(106,135,89)">&lt;iostream&gt;<br></span><span style=
=3D"color:rgb(187,181,41)">#include </span><span style=3D"color:rgb(106,135=
,89)">&lt;vector&gt;<br></span><span style=3D"color:rgb(187,181,41)">#inclu=
de </span><span style=3D"color:rgb(106,135,89)">&lt;algorithm&gt;<br></span=
><span style=3D"color:rgb(187,181,41)">#include </span><span style=3D"color=
:rgb(106,135,89)">&lt;iterator&gt;<br></span><span style=3D"color:rgb(187,1=
81,41)">#include </span><span style=3D"color:rgb(106,135,89)">&lt;type_trai=
ts&gt;<br></span><span style=3D"color:rgb(187,181,41)">#include </span><spa=
n style=3D"color:rgb(106,135,89)">&lt;array&gt;<br></span><span style=3D"co=
lor:rgb(106,135,89)"><br></span><span style=3D"color:rgb(106,135,89)"><br><=
/span><span style=3D"color:rgb(128,128,128)">/// Convert a container of pro=
babilities into a container of ascending interval limits<br></span><span st=
yle=3D"color:rgb(204,120,50);font-weight:bold">template</span>&lt;<span sty=
le=3D"color:rgb(204,120,50);font-weight:bold">class </span><span style=3D"c=
olor:rgb(185,188,209)">Container</span>&gt;<br><span style=3D"color:rgb(204=
,120,50);font-weight:bold">auto </span><span style=3D"color:rgb(255,198,109=
)">to_limits</span>(<span style=3D"color:rgb(185,188,209)">Container </span=
>container) {<br>    <span style=3D"color:rgb(204,120,50);font-weight:bold"=
>using </span><span style=3D"color:rgb(185,188,209)">container_type </span>=
=3D <span style=3D"color:rgb(181,182,227)">std</span>::<span style=3D"color=
:rgb(185,188,209)">decay_t</span>&lt;<span style=3D"color:rgb(185,188,209)"=
>Container</span>&gt;<span style=3D"color:rgb(204,120,50)">;<br></span><spa=
n style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(204,1=
20,50);font-weight:bold">using </span><span style=3D"color:rgb(185,188,209)=
">value_type </span>=3D <span style=3D"color:rgb(204,120,50);font-weight:bo=
ld">typename </span><span style=3D"color:rgb(185,188,209)">container_type</=
span>::<span style=3D"color:rgb(185,188,209)">value_type</span><span style=
=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)=
">    </span><span style=3D"color:rgb(204,120,50);font-weight:bold">auto </=
span>to_total =3D [total =3D <span style=3D"color:rgb(185,188,209)">value_t=
ype</span>(<span style=3D"color:rgb(104,151,187)">0</span>)](<span style=3D=
"color:rgb(204,120,50);font-weight:bold">auto </span>&amp;&amp;x) <span sty=
le=3D"color:rgb(204,120,50);font-weight:bold">mutable </span>{<br>        t=
otal +=3D x<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D=
"color:rgb(204,120,50)">        </span><span style=3D"color:rgb(204,120,50)=
;font-weight:bold">return </span>total<span style=3D"color:rgb(204,120,50)"=
>;<br></span><span style=3D"color:rgb(204,120,50)">    </span>}<span style=
=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)=
"><br></span><span style=3D"color:rgb(204,120,50)">    </span><span style=
=3D"color:rgb(181,182,227)">std</span>::transform(<span style=3D"color:rgb(=
181,182,227)">std</span>::begin(container)<span style=3D"color:rgb(204,120,=
50)">, </span><span style=3D"color:rgb(181,182,227)">std</span>::end(contai=
ner)<span style=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:=
rgb(204,120,50)">                   </span><span style=3D"color:rgb(181,182=
,227)">std</span>::begin(container)<span style=3D"color:rgb(204,120,50)">,<=
br></span><span style=3D"color:rgb(204,120,50)">                   </span>t=
o_total)<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"co=
lor:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50)">    <=
/span><span style=3D"color:rgb(204,120,50);font-weight:bold">return </span>=
container<span style=3D"color:rgb(204,120,50)">;<br></span>}<span style=3D"=
color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><b=
r></span><span style=3D"color:rgb(204,120,50);font-weight:bold">template</s=
pan>&lt;<span style=3D"color:rgb(204,120,50);font-weight:bold">class </span=
><span style=3D"color:rgb(185,188,209)">Container</span>&gt;<br><span style=
=3D"color:rgb(204,120,50);font-weight:bold">struct </span><span style=3D"co=
lor:rgb(181,182,227)">assert_not_empty </span>{<br>    <span style=3D"color=
:rgb(255,198,109)">assert_not_empty</span>(<span style=3D"color:rgb(185,188=
,209)">Container </span><span style=3D"color:rgb(204,120,50);font-weight:bo=
ld">const </span>&amp;container) <span style=3D"color:rgb(204,120,50);font-=
weight:bold">noexcept </span>{<br>        <span style=3D"color:rgb(144,139,=
37)">assert</span>(!container.<span style=3D"color:rgb(152,118,170);font-st=
yle:italic">empty</span>())<span style=3D"color:rgb(204,120,50)">;<br></spa=
n><span style=3D"color:rgb(204,120,50)">    </span>}<br>}<span style=3D"col=
or:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br><=
/span><span style=3D"color:rgb(204,120,50);font-weight:bold">template</span=
>&lt;<span style=3D"color:rgb(204,120,50);font-weight:bold">class </span><s=
pan style=3D"color:rgb(185,188,209)">Type</span><span style=3D"color:rgb(20=
4,120,50)">, </span><span style=3D"color:rgb(181,182,227)">std</span>::<spa=
n style=3D"color:rgb(185,188,209)">size_t </span><span style=3D"color:rgb(1=
52,118,170);font-style:italic">N</span>&gt;<br><span style=3D"color:rgb(204=
,120,50);font-weight:bold">struct </span><span style=3D"color:rgb(181,182,2=
27)">assert_not_empty</span>&lt;<span style=3D"color:rgb(181,182,227)">std<=
/span>::<span style=3D"color:rgb(181,182,227)">array</span>&lt;<span style=
=3D"color:rgb(185,188,209)">Type</span><span style=3D"color:rgb(204,120,50)=
">, </span><span style=3D"color:rgb(152,118,170);font-style:italic">N</span=
>&gt;&gt; {<br>    <span style=3D"color:rgb(204,120,50);font-weight:bold">c=
onstexpr </span><span style=3D"color:rgb(255,198,109)">assert_not_empty</sp=
an>(<span style=3D"color:rgb(181,182,227)">std</span>::<span style=3D"color=
:rgb(181,182,227)">array</span>&lt;<span style=3D"color:rgb(185,188,209)">T=
ype</span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"col=
or:rgb(152,118,170);font-style:italic">N</span>&gt; <span style=3D"color:rg=
b(204,120,50);font-weight:bold">const </span>&amp;container) <span style=3D=
"color:rgb(204,120,50);font-weight:bold">noexcept </span>{<br>        <span=
 style=3D"color:rgb(204,120,50);font-weight:bold">static_assert</span>(!con=
tainer.empty())<span style=3D"color:rgb(204,120,50)">;<br></span><span styl=
e=3D"color:rgb(204,120,50)">    </span>}<br>}<span style=3D"color:rgb(204,1=
20,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><br></span><span =
style=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(128,128=
,128)">/// given an ascending container of limits, choose one with probabil=
ity equal to 1.0 / max(limits)<br></span><span style=3D"color:rgb(128,128,1=
28)">/// @returns an integer containing the index of the chosen limit<br></=
span><span style=3D"color:rgb(204,120,50);font-weight:bold">template</span>=
&lt;<span style=3D"color:rgb(204,120,50);font-weight:bold">class </span><sp=
an style=3D"color:rgb(185,188,209)">Container</span><span style=3D"color:rg=
b(204,120,50)">, </span><span style=3D"color:rgb(204,120,50);font-weight:bo=
ld">class </span><span style=3D"color:rgb(185,188,209)">Value</span>&gt;<br=
><span style=3D"color:rgb(204,120,50);font-weight:bold">int </span><span st=
yle=3D"color:rgb(255,198,109)">sample_limits</span>(<span style=3D"color:rg=
b(185,188,209)">Container </span>&amp;&amp;totals<span style=3D"color:rgb(2=
04,120,50)">, </span><span style=3D"color:rgb(185,188,209)">Value </span>&a=
mp;&amp;prob) {<br><br>    <span style=3D"color:rgb(128,128,128)">// precon=
dition<br></span><span style=3D"color:rgb(128,128,128)">    </span>assert_n=
ot_empty{totals}<span style=3D"color:rgb(204,120,50)">;<br></span><span sty=
le=3D"color:rgb(204,120,50)"><br></span><span style=3D"color:rgb(204,120,50=
)">    </span><span style=3D"color:rgb(128,128,128)">// convert prob (range=
 0-1) into a total.<br></span><span style=3D"color:rgb(128,128,128)"><br></=
span><span style=3D"color:rgb(128,128,128)">    </span><span style=3D"color=
:rgb(204,120,50);font-weight:bold">auto </span>target =3D prob * totals.<sp=
an style=3D"color:rgb(152,118,170);font-style:italic">back</span>()<span st=
yle=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,=
50)"><br></span><span style=3D"color:rgb(204,120,50)">    </span><span styl=
e=3D"color:rgb(204,120,50);font-weight:bold">auto </span>cap =3D [&amp;tota=
ls](<span style=3D"color:rgb(204,120,50);font-weight:bold">auto </span>iter=
) {<br>        <span style=3D"color:rgb(204,120,50);font-weight:bold">if </=
span>(iter =3D=3D <span style=3D"color:rgb(181,182,227)">std</span>::end(to=
tals))<br>            <span style=3D"color:rgb(204,120,50);font-weight:bold=
">return </span><span style=3D"color:rgb(181,182,227)">std</span>::prev(ite=
r)<span style=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rg=
b(204,120,50)">        </span><span style=3D"color:rgb(204,120,50);font-wei=
ght:bold">else<br></span><span style=3D"color:rgb(204,120,50);font-weight:b=
old">            return </span>iter<span style=3D"color:rgb(204,120,50)">;<=
br></span><span style=3D"color:rgb(204,120,50)">    </span>}<span style=3D"=
color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)"><b=
r></span><span style=3D"color:rgb(204,120,50)">    </span><span style=3D"co=
lor:rgb(204,120,50);font-weight:bold">auto </span>first =3D <span style=3D"=
color:rgb(181,182,227)">std</span>::cbegin(totals)<span style=3D"color:rgb(=
204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)">    </span><=
span style=3D"color:rgb(204,120,50);font-weight:bold">auto </span>last =3D =
<span style=3D"color:rgb(181,182,227)">std</span>::cend(totals)<span style=
=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120,50)=
"><br></span><span style=3D"color:rgb(204,120,50)">    </span><span style=
=3D"color:rgb(204,120,50);font-weight:bold">auto </span>index =3D <span sty=
le=3D"color:rgb(181,182,227)">std</span>::distance(first<span style=3D"colo=
r:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">      =
                         </span>cap(<span style=3D"color:rgb(181,182,227)">=
std</span>::lower_bound(first<span style=3D"color:rgb(204,120,50)">,<br></s=
pan><span style=3D"color:rgb(204,120,50)">                                 =
                   </span>last<span style=3D"color:rgb(204,120,50)">,<br></=
span><span style=3D"color:rgb(204,120,50)">                                =
                    </span>target)))<span style=3D"color:rgb(204,120,50)">;=
<br></span><span style=3D"color:rgb(204,120,50)">    </span><span style=3D"=
color:rgb(204,120,50);font-weight:bold">return int</span>(index)<span style=
=3D"color:rgb(204,120,50)">;<br></span>}<br><br><span style=3D"color:rgb(20=
4,120,50);font-weight:bold">int </span><span style=3D"color:rgb(255,198,109=
)">main</span>() {<br><br>    <span style=3D"color:rgb(128,128,128)">// The=
 goal is to sample among ints from 0 to 9, with different probability for e=
ach int.<br></span><span style=3D"color:rgb(128,128,128)">    // The probab=
ility of i is proba[i] / sum(proba).<br></span><span style=3D"color:rgb(128=
,128,128)">    </span><span style=3D"color:rgb(181,182,227)">std</span>::<s=
pan style=3D"color:rgb(181,182,227)">array</span>&lt;<span style=3D"color:r=
gb(204,120,50);font-weight:bold">float</span><span style=3D"color:rgb(204,1=
20,50)">, </span><span style=3D"color:rgb(104,151,187)">10</span>&gt; proba=
{{<span style=3D"color:rgb(104,151,187)">0.01</span><span style=3D"color:rg=
b(204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.05</span><=
span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,=
151,187)">0.01</span><span style=3D"color:rgb(204,120,50)">, </span><span s=
tyle=3D"color:rgb(104,151,187)">0.15</span><span style=3D"color:rgb(204,120=
,50)">, </span><span style=3D"color:rgb(104,151,187)">0.1</span><span style=
=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">=
0.19</span><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"co=
lor:rgb(104,151,187)">0.19</span><span style=3D"color:rgb(204,120,50)">, </=
span><span style=3D"color:rgb(104,151,187)">0.1</span><span style=3D"color:=
rgb(204,120,50)">, </span><span style=3D"color:rgb(104,151,187)">0.19</span=
><span style=3D"color:rgb(204,120,50)">, </span><span style=3D"color:rgb(10=
4,151,187)">0.01</span>}}<span style=3D"color:rgb(204,120,50)">;<br></span>=
<span style=3D"color:rgb(204,120,50)">    </span><span style=3D"color:rgb(2=
04,120,50);font-weight:bold">auto </span>limits =3D to_limits(proba)<span s=
tyle=3D"color:rgb(204,120,50)">;<br></span><span style=3D"color:rgb(204,120=
,50)">    </span><span style=3D"color:rgb(204,120,50);font-weight:bold">for=
 </span>(<span style=3D"color:rgb(204,120,50);font-weight:bold">auto </span=
>v : <span style=3D"color:rgb(181,182,227)">std</span>::vector&lt;<span sty=
le=3D"color:rgb(204,120,50);font-weight:bold">float</span>&gt;{{<br>       =
                                      <span style=3D"color:rgb(104,151,187)=
">0.f</span><span style=3D"color:rgb(204,120,50)">,<br></span><span style=
=3D"color:rgb(204,120,50)">                                             </s=
pan><span style=3D"color:rgb(104,151,187)">0.00001f</span><span style=3D"co=
lor:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)">    =
                                         </span><span style=3D"color:rgb(10=
4,151,187)">0.02f</span><span style=3D"color:rgb(204,120,50)">,<br></span><=
span style=3D"color:rgb(204,120,50)">                                      =
       </span><span style=3D"color:rgb(104,151,187)">0.5f</span><span style=
=3D"color:rgb(204,120,50)">,<br></span><span style=3D"color:rgb(204,120,50)=
">                                             </span><span style=3D"color:=
rgb(104,151,187)">0.98f</span><span style=3D"color:rgb(204,120,50)">,<br></=
span><span style=3D"color:rgb(204,120,50)">                                =
             </span><span style=3D"color:rgb(104,151,187)">1.f</span><span =
style=3D"color:rgb(204,120,50)">,  </span><span style=3D"color:rgb(128,128,=
128)">// on upper bound<br></span><span style=3D"color:rgb(128,128,128)">  =
                                           </span><span style=3D"color:rgb(=
104,151,187)">1.00001f  </span><span style=3D"color:rgb(128,128,128)">// be=
yond upper bound<br></span><span style=3D"color:rgb(128,128,128)">         =
                            </span>}}) {<br>        <span style=3D"color:rg=
b(181,182,227)">std</span>::cout <span style=3D"color:rgb(95,140,138)">&lt;=
&lt; </span>sample_limits(limits<span style=3D"color:rgb(204,120,50)">, </s=
pan>v) <span style=3D"color:rgb(95,140,138)">&lt;&lt; </span><span style=3D=
"color:rgb(181,182,227)">std</span>::endl<span style=3D"color:rgb(204,120,5=
0)">;<br></span><span style=3D"color:rgb(204,120,50)">    </span>}<br>    <=
br>    <span style=3D"color:rgb(204,120,50);font-weight:bold">return </span=
><span style=3D"color:rgb(104,151,187)">0</span><span style=3D"color:rgb(20=
4,120,50)">;<br></span>}<br><br></pre><br></div><div>=C2=A0</div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol=
id;padding-left:1ex"><div dir=3D"ltr"><div><br></div><div>




<span></span>





<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p1" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(81,182,255);ba=
ckground-color:rgb(0,0,0)">#include <span class=3D"m_-670750835528074674m_1=
012224171286952715m_5052467569217688009m_2612803408380067011gmail-s1" style=
=3D"color:rgb(180,38,26)">&lt;cassert&gt;</span></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p2" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(180,38,26);bac=
kground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_10122241712=
86952715m_5052467569217688009m_2612803408380067011gmail-s2" style=3D"color:=
rgb(81,182,255)">#include </span>&lt;iostream&gt;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p1" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(81,182,255);ba=
ckground-color:rgb(0,0,0)">#include <span class=3D"m_-670750835528074674m_1=
012224171286952715m_5052467569217688009m_2612803408380067011gmail-s1" style=
=3D"color:rgb(180,38,26)">&lt;map&gt;</span></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p1" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(81,182,255);ba=
ckground-color:rgb(0,0,0)">#include <span class=3D"m_-670750835528074674m_1=
012224171286952715m_5052467569217688009m_2612803408380067011gmail-s1" style=
=3D"color:rgb(180,38,26)">&lt;vector&gt;</span></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb=
(0,0,0);min-height:14px"><br></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p4" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(255,178,109);b=
ackground-color:rgb(0,0,0)">using<span class=3D"m_-670750835528074674m_1012=
224171286952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D=
"color:rgb(231,254,226)"> </span>namespace<span class=3D"m_-670750835528074=
674m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s3"=
 style=3D"color:rgb(231,254,226)"> std;</span></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb=
(0,0,0);min-height:14px"><br></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"colo=
r:rgb(255,178,109)">using</span> Intervals =3D map&lt;<span class=3D"m_-670=
750835528074674m_1012224171286952715m_5052467569217688009m_2612803408380067=
011gmail-s4" style=3D"color:rgb(255,178,109)">float</span>,<span class=3D"m=
_-670750835528074674m_1012224171286952715m_5052467569217688009m_26128034083=
80067011gmail-s4" style=3D"color:rgb(255,178,109)">int</span>&gt;;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb=
(0,0,0);min-height:14px"><br></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"colo=
r:rgb(255,178,109)">int</span> sample(Intervals <span class=3D"m_-670750835=
528074674m_1012224171286952715m_5052467569217688009m_2612803408380067011gma=
il-s4" style=3D"color:rgb(255,178,109)">const</span> &amp; intervals, <span=
 class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009m_=
2612803408380067011gmail-s4" style=3D"color:rgb(255,178,109)">float</span> =
randomFloat) {</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span>assert(!intervals.empty());</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span><span class=3D"m_-670750835528074674m_1012224171=
286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color=
:rgb(255,178,109)">auto</span> it =3D intervals.lower_bound(randomFloat);</=
p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span><span class=3D"m_-670750835528074674m_1012224171=
286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color=
:rgb(255,178,109)">if</span>(it =3D=3D intervals.end()) {</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 </span></span>// we return the last element of the ma=
p:</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_-6707508355280746=
74m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s4" =
style=3D"color:rgb(255,178,109)">return</span> crbegin(intervals)-&gt;secon=
d;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 </span></span>// this is what I would prefer writing:=
</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 </span></span>// return intervals.back();</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span>}</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span><span class=3D"m_-670750835528074674m_1012224171=
286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color=
:rgb(255,178,109)">return</span> it-&gt;second;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)">}</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb=
(0,0,0);min-height:14px"><br></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"colo=
r:rgb(255,178,109)">int</span> main() {</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 </span></span>// The goal is to sample among ints from 0 to 9, with=
 different probability for each int.</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb=
(0,0,0);min-height:14px"><br></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 </span></span>// The probability of i is proba[i].</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p7" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(212,212,212);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"colo=
r:rgb(231,254,226)"><span class=3D"m_-670750835528074674m_10122241712869527=
15m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=
=C2=A0 =C2=A0 </span>std::array&lt;</span><span class=3D"m_-670750835528074=
674m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s4"=
 style=3D"color:rgb(255,178,109)">float</span><span class=3D"m_-67075083552=
8074674m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail=
-s3" style=3D"color:rgb(231,254,226)">,</span>10<span class=3D"m_-670750835=
528074674m_1012224171286952715m_5052467569217688009m_2612803408380067011gma=
il-s3" style=3D"color:rgb(231,254,226)">&gt; proba {{</span>0.01<span class=
=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009m_261280=
3408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.05<span c=
lass=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009m_26=
12803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.01<sp=
an class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span>0.1=
5<span class=3D"m_-670750835528074674m_1012224171286952715m_505246756921768=
8009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</span=
>0.1<span class=3D"m_-670750835528074674m_1012224171286952715m_505246756921=
7688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">,</s=
pan>0.19<span class=3D"m_-670750835528074674m_1012224171286952715m_50524675=
69217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,226)">=
,</span>0.19<span class=3D"m_-670750835528074674m_1012224171286952715m_5052=
467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254,22=
6)">,</span>0.1<span class=3D"m_-670750835528074674m_1012224171286952715m_5=
052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231,254=
,226)">,</span>0.19<span class=3D"m_-670750835528074674m_101222417128695271=
5m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb(231=
,254,226)">,</span>0.01<span class=3D"m_-670750835528074674m_10122241712869=
52715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rgb=
(231,254,226)">}};</span></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb=
(0,0,0);min-height:14px"><br></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 </span></span>// The sum of probabilities are 1, so we can conceptu=
ally partition</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 </span></span>// the [0,1] interval in 10 intervals of lengths prob=
a[i].</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span><span class=3D"m_-670750835528074674m_1012224171=
286952715m_5052467569217688009m_2612803408380067011gmail-s5" style=3D"color=
:rgb(0,193,0)">//</span></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 </span></span>// we only need to store the upper bounds of these in=
tervals,</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 </span></span>// because the lower bound of interval n is the upper=
 bound of interval n-1,</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 </span></span>// and because we know that the first lower bound is =
0:</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span>Intervals intervals;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span><span class=3D"m_-670750835528074674m_1012224171=
286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color=
:rgb(255,178,109)">int</span> i =3D <span class=3D"m_-670750835528074674m_1=
012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" style=
=3D"color:rgb(212,212,212)">0</span>;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span><span class=3D"m_-670750835528074674m_1012224171=
286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color=
:rgb(255,178,109)">float</span> upperbound =3D <span class=3D"m_-6707508355=
28074674m_1012224171286952715m_5052467569217688009m_2612803408380067011gmai=
l-s6" style=3D"color:rgb(212,212,212)">0.f</span>;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span><span class=3D"m_-670750835528074674m_1012224171=
286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color=
:rgb(255,178,109)">for</span>(<span class=3D"m_-670750835528074674m_1012224=
171286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"co=
lor:rgb(255,178,109)">auto</span> p : proba) {</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>upperbound +=3D p;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>intervals.emplace(upperbound,i);</=
p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 </span></span>// Note that the last key, due to float=
ing point rounding errors, will be</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 </span></span>// close to 1.f but not necessarilly 1.=
f.</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>++i;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span>}</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p8" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0);min-height:14px"><span class=3D"m_-6707508355280=
74674m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-A=
pple-converted-space">=C2=A0=C2=A0 =C2=A0</span></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 </span></span>// Then, to sample ints, we generate a random numbers=
 between 0 and 1,</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p6" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,193,0);backg=
round-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_1012224171286=
952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"color:rg=
b(231,254,226)"><span class=3D"m_-670750835528074674m_1012224171286952715m_=
5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=C2=A0=
 =C2=A0 </span></span>// and lookup the correponding interval:</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb=
(0,0,0);min-height:14px"><br></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span><span class=3D"m_-670750835528074674m_1012224171=
286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"color=
:rgb(255,178,109)">for</span>(<span class=3D"m_-670750835528074674m_1012224=
171286952715m_5052467569217688009m_2612803408380067011gmail-s4" style=3D"co=
lor:rgb(255,178,109)">auto</span> v : vector&lt;<span class=3D"m_-670750835=
528074674m_1012224171286952715m_5052467569217688009m_2612803408380067011gma=
il-s4" style=3D"color:rgb(255,178,109)">float</span>&gt;{{</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_-6707508355280746=
74m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" =
style=3D"color:rgb(212,212,212)">0.f</span>,</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_-6707508355280746=
74m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" =
style=3D"color:rgb(212,212,212)">0.00001f</span>,</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_-6707508355280746=
74m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" =
style=3D"color:rgb(212,212,212)">0.02f</span>,</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_-6707508355280746=
74m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" =
style=3D"color:rgb(212,212,212)">0.5f</span>,</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_-6707508355280746=
74m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" =
style=3D"color:rgb(212,212,212)">0.98f</span>,</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_-6707508355280746=
74m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" =
style=3D"color:rgb(212,212,212)">1.f</span>,</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span class=3D"m_-6707508355280746=
74m_1012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" =
style=3D"color:rgb(212,212,212)">1.00001f</span>}})</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span>{</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span>cout &lt;&lt; sample(intervals, v)=
 &lt;&lt; endl;</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-Apple-converted-s=
pace">=C2=A0 =C2=A0 </span>}</p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p3" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb=
(0,0,0);min-height:14px"><br></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p4" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(255,178,109);b=
ackground-color:rgb(0,0,0)"><span class=3D"m_-670750835528074674m_101222417=
1286952715m_5052467569217688009m_2612803408380067011gmail-s3" style=3D"colo=
r:rgb(231,254,226)"><span class=3D"m_-670750835528074674m_10122241712869527=
15m_5052467569217688009m_2612803408380067011gmail-Apple-converted-space">=
=C2=A0 =C2=A0 </span></span>return<span class=3D"m_-670750835528074674m_101=
2224171286952715m_5052467569217688009m_2612803408380067011gmail-s3" style=
=3D"color:rgb(231,254,226)"> </span><span class=3D"m_-670750835528074674m_1=
012224171286952715m_5052467569217688009m_2612803408380067011gmail-s6" style=
=3D"color:rgb(212,212,212)">0</span><span class=3D"m_-670750835528074674m_1=
012224171286952715m_5052467569217688009m_2612803408380067011gmail-s3" style=
=3D"color:rgb(231,254,226)">;</span></p>
<p class=3D"m_-670750835528074674m_1012224171286952715m_5052467569217688009=
m_2612803408380067011gmail-p5" style=3D"margin:0px;font-variant-numeric:nor=
mal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;f=
ont-size:12px;line-height:normal;font-family:Menlo;color:rgb(231,254,226);b=
ackground-color:rgb(0,0,0)">}</p><p class=3D"m_-670750835528074674m_1012224=
171286952715m_5052467569217688009m_2612803408380067011gmail-p5" style=3D"ma=
rgin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-we=
ight:normal;font-stretch:normal;font-size:12px;line-height:normal;font-fami=
ly:Menlo;color:rgb(231,254,226);background-color:rgb(0,0,0)"><br></p>


</div><div><br></div><div>Olivier</div><div><br></div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr">Le=C2=A0mer. 18 juil. 2018 =C3=A0=C2=A019=
:53, Richard Hodges &lt;<a href=3D"mailto:hodges.r@gmail.com" target=3D"_bl=
ank">hodges.r@gmail.com</a>&gt; a =C3=A9crit=C2=A0:<br></div><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;p=
adding-left:1ex"><div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr">On Wed, 18 Jul 2018 at 14:10, Olivier Sohn &lt;<a href=3D"mailto=
:olivier.sohn@gmail.com" target=3D"_blank">olivier.sohn@gmail.com</a>&gt; w=
rote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hello,<div><=
br></div><div>I recently encountered an algorithm where I needed to access =
the map&#39;s last element, knowing the map was non-empty. But I found that=
 there is no straightforward way to access the last (or first) element of a=
 map.</div></div></blockquote><div><br></div><div>Without wishing to sound =
too controversial, I am wondering whether this algorithm could have been ex=
pressed in a different way.</div><div><br></div><div>Wanting a reference to=
 the &quot;back&quot; of a container suggests a need to access the least/gr=
eatest/earliest/latest of a group of things.=C2=A0</div><div><br></div><div=
>Are you able to share the motivating use case, as there are other tools fo=
r this in the standard library?<br></div><div>=C2=A0</div><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex"><div dir=3D"ltr"><div><br></div><div>So as suggested by this =
process :=C2=A0<a href=3D"https://isocpp.org/std/submit-a-proposal" target=
=3D"_blank">https://isocpp.org/std/submit-a-proposal</a> I&#39;d like to pr=
opose the following: add the front() and back() methods, with const and non=
 const versions, to std::(multi)map and std::(multi)set, to return a (const=
-) reference to the last / first element(s).</div><div><br></div><div>Like =
with other containers implementing these methods, no guaranty would be give=
n when the container is empty.</div><div><br></div><div>What do you think?<=
/div><div><br></div><div>Cheers,</div><div>Olivier</div><div><br></div></di=
v>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-45df-9536-e0bfab52962b%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e6d18f4e-6337-=
45df-9536-e0bfab52962b%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3=
ePv6jG_DXX7cfQ%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfooter"=
 target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-propo=
sals/CALvx3hbHNVYrbgXgjJfZR2vezehvKPyicsZ3ePv6jG_DXX7cfQ%40mail.gmail.com</=
a>.<br>
</blockquote></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsi=
bvOdUe9S8%3D%2Bzeg%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfoo=
ter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-p=
roposals/CANAj80t5Pdx_xHhK0KgtNbk6refXykAtiBsibvOdUe9S8%3D%2Bzeg%40mail.gma=
il.com</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPU=
Wj%3DvBNSJCx7-pDfYuA%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Df=
ooter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std=
-proposals/CALvx3haNKo0QkFsK6Qyi%2Bm06P4N6g%2BPUWj%3DvBNSJCx7-pDfYuA%40mail=
..gmail.com</a>.<br>
</div></blockquote></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7F5-4C90-AD0E-CFEAC5F95CD6%=
40gmail.com?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/583D12C6-E7F5-4=
C90-AD0E-CFEAC5F95CD6%40gmail.com</a>.<br>
</blockquote></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/sO8Zur6KSkQ=
/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hYvMBhNXTPdBHsLEvbvvU71azApw%2B=
SQiGmZNZd4ps%2BWhw%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfoo=
ter" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-p=
roposals/CALvx3hYvMBhNXTPdBHsLEvbvvU71azApw%2BSQiGmZNZd4ps%2BWhw%40mail.gma=
il.com</a>.<br>
</blockquote></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CANAj80uD9%2BBA6-dC66ixmTvoqcsAknOLvj=
ZsT59d0HnkUyd0cA%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfoote=
r" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-pro=
posals/CANAj80uD9%2BBA6-dC66ixmTvoqcsAknOLvjZsT59d0HnkUyd0cA%40mail.gmail.c=
om</a>.<br>
</blockquote></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hZCDa%2BG-5mUObDsk9A3t4%2B6v0te=
iKe-V9iuOpA-smCwVw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hZCDa%2=
BG-5mUObDsk9A3t4%2B6v0teiKe-V9iuOpA-smCwVw%40mail.gmail.com</a>.<br />

--0000000000006a250c057162d389--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Thu, 19 Jul 2018 21:00:15 -0700 (PDT)
Raw View
------=_Part_4871_131639610.1532059215433
Content-Type: multipart/alternative;
 boundary="----=_Part_4872_878862073.1532059215433"

------=_Part_4872_878862073.1532059215433
Content-Type: text/plain; charset="UTF-8"

On Thursday, July 19, 2018 at 7:49:13 PM UTC-4, Richard Hodges wrote:
>
> On Thu, 19 Jul 2018 at 15:37, Olivier S. <olivie...@gmail.com
> <javascript:>> wrote:
>
>> Sure, there are other ways to implement that, but this example shows that
>> there a legitimate uses for back and front in a map, to return references
>> to elements.
>>
>
> This is unproven since my sample_limits() function does not use front() or
> back(). I remain unconvinced of "legitimate uses". Remember that the STL is
> ~30 years old. Don't you think that someone would have noticed if there was
> a deficiency like this?
>

Need I remind you that it took until C++17 before the ordered containers
got some form of node extraction functionality, as well as
insert-or-assign/try_emplace behavior? It also took until C++20 before we
could ask if a map contains an element *without* fetching the iterator.

So the fact that the type has been around a long time should not be used to
prevent a change from being made. Considering that the fact that the
container has an order is a fundamental part of its definition, it does
make sense to be able to access the first and last elements in it. Just
like we can access the first and last elements of a sequence container.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/7277cda6-7990-4f88-8526-6f731a638397%40isocpp.org.

------=_Part_4872_878862073.1532059215433
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Thursday, July 19, 2018 at 7:49:13 PM UTC-4, Richard Ho=
dges wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left=
: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><d=
iv class=3D"gmail_quote"><div dir=3D"ltr">On Thu, 19 Jul 2018 at 15:37, Oli=
vier S. &lt;<a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=
=3D"jgqSJbQDAwAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;javascri=
pt:&#39;;return true;" onclick=3D"this.href=3D&#39;javascript:&#39;;return =
true;">olivie...@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"gma=
il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef=
t:1ex"><div dir=3D"ltr">Sure, there are other ways to implement that, but t=
his example shows that there a legitimate uses for back and front in a map,=
 to return references to elements.</div></blockquote><div><br></div><div>Th=
is is unproven since my sample_limits() function does not use front() or ba=
ck(). I remain unconvinced of &quot;legitimate uses&quot;. Remember that th=
e STL is ~30 years old. Don&#39;t you think that someone would have noticed=
 if there was a deficiency like this?</div></div></div></blockquote><div><b=
r></div><div>Need I remind you that it took until C++17 before the ordered =
containers got some form of node extraction functionality, as well as inser=
t-or-assign/try_emplace behavior? It also took until C++20 before we could =
ask if a map contains an element <i>without</i> fetching the iterator.</div=
><div><br></div><div>So the fact that the type has been around a long time =
should not be used to prevent a change from being made. Considering that th=
e fact that the container has an order is a fundamental part of its definit=
ion, it does make sense to be able to access the first and last elements in=
 it. Just like we can access the first and last elements of a sequence cont=
ainer.<br></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/7277cda6-7990-4f88-8526-6f731a638397%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/7277cda6-7990-4f88-8526-6f731a638397=
%40isocpp.org</a>.<br />

------=_Part_4872_878862073.1532059215433--

------=_Part_4871_131639610.1532059215433--

.


Author: Richard Hodges <hodges.r@gmail.com>
Date: Fri, 20 Jul 2018 09:21:27 +0200
Raw View
--00000000000087d0dd057169259f
Content-Type: text/plain; charset="UTF-8"

On Fri, 20 Jul 2018 at 06:00, Nicol Bolas <jmckesson@gmail.com> wrote:

> On Thursday, July 19, 2018 at 7:49:13 PM UTC-4, Richard Hodges wrote:
>>
>> On Thu, 19 Jul 2018 at 15:37, Olivier S. <olivie...@gmail.com> wrote:
>>
>>> Sure, there are other ways to implement that, but this example shows
>>> that there a legitimate uses for back and front in a map, to return
>>> references to elements.
>>>
>>
>> This is unproven since my sample_limits() function does not use front()
>> or back(). I remain unconvinced of "legitimate uses". Remember that the STL
>> is ~30 years old. Don't you think that someone would have noticed if there
>> was a deficiency like this?
>>
>
> Need I remind you that it took until C++17 before the ordered containers
> got some form of node extraction functionality, as well as
> insert-or-assign/try_emplace behavior? It also took until C++20 before we
> could ask if a map contains an element *without* fetching the iterator.
>
> So the fact that the type has been around a long time should not be used
> to prevent a change from being made. Considering that the fact that the
> container has an order is a fundamental part of its definition, it does
> make sense to be able to access the first and last elements in it. Just
> like we can access the first and last elements of a sequence container.
>

I agree that it seems to make sense, if only for consistency's sake. I'm
not sure I agree that it has any real motivating use case. I agree that
arguments of antiquity are not necessarily valid.



> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/7277cda6-7990-4f88-8526-6f731a638397%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/7277cda6-7990-4f88-8526-6f731a638397%40isocpp.org?utm_medium=email&utm_source=footer>
> .
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3ha2Yk_jahMtKx_B7--LwhojEGHrGGuJrjF0RDobn6GdzQ%40mail.gmail.com.

--00000000000087d0dd057169259f
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Fri=
, 20 Jul 2018 at 06:00, Nicol Bolas &lt;<a href=3D"mailto:jmckesson@gmail.c=
om">jmckesson@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_=
quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1=
ex"><div dir=3D"ltr">On Thursday, July 19, 2018 at 7:49:13 PM UTC-4, Richar=
d Hodges wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-l=
eft:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><di=
v class=3D"gmail_quote"><div dir=3D"ltr">On Thu, 19 Jul 2018 at 15:37, Oliv=
ier S. &lt;<a rel=3D"nofollow">olivie...@gmail.com</a>&gt; wrote:<br></div>=
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr">Sure, there are other ways =
to implement that, but this example shows that there a legitimate uses for =
back and front in a map, to return references to elements.</div></blockquot=
e><div><br></div><div>This is unproven since my sample_limits() function do=
es not use front() or back(). I remain unconvinced of &quot;legitimate uses=
&quot;. Remember that the STL is ~30 years old. Don&#39;t you think that so=
meone would have noticed if there was a deficiency like this?</div></div></=
div></blockquote><div><br></div><div>Need I remind you that it took until C=
++17 before the ordered containers got some form of node extraction functio=
nality, as well as insert-or-assign/try_emplace behavior? It also took unti=
l C++20 before we could ask if a map contains an element <i>without</i> fet=
ching the iterator.</div><div><br></div><div>So the fact that the type has =
been around a long time should not be used to prevent a change from being m=
ade. Considering that the fact that the container has an order is a fundame=
ntal part of its definition, it does make sense to be able to access the fi=
rst and last elements in it. Just like we can access the first and last ele=
ments of a sequence container.<br></div></div></blockquote><div><br></div><=
div>I agree that it seems to make sense, if only for consistency&#39;s sake=
.. I&#39;m not sure I agree that it has any real motivating use case. I agre=
e that arguments of antiquity are not necessarily valid.</div><div><br></di=
v><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 =
..8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div></d=
iv></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/7277cda6-7990-4f88-8526-6f731a638397%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/7277cda6-7990-=
4f88-8526-6f731a638397%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3ha2Yk_jahMtKx_B7--LwhojEGHrGGuJ=
rjF0RDobn6GdzQ%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3ha2Yk_jahMt=
Kx_B7--LwhojEGHrGGuJrjF0RDobn6GdzQ%40mail.gmail.com</a>.<br />

--00000000000087d0dd057169259f--

.


Author: "oliviersohn via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Fri, 20 Jul 2018 01:04:42 -0700 (PDT)
Raw View
------=_Part_5229_1465990399.1532073882543
Content-Type: multipart/alternative;
 boundary="----=_Part_5230_2101960016.1532073882544"

------=_Part_5230_2101960016.1532073882544
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable



Le vendredi 20 juillet 2018 06:00:15 UTC+2, Nicol Bolas a =C3=A9crit :
>
> Considering that the fact that the container has an order is a fundamenta=
l=20
> part of its definition, it does make sense to be able to access the first=
=20
> and last elements in it. Just like we can access the first and last=20
> elements of a sequence container.
>

Thinking about it, maybe 'back' and 'front' are names that are too strongly=
=20
associated to 'sequence' containers to be used here.

I propose 'min()' and 'max()'.

And I extend the scope of the proposal to include 'find_min()',=20
'find_max()' which return iterators.

Summarizing it, we could have these additional methods:

- For map / multimap:

      mapped_type& min ();
const mapped_type& min () const;


      iterator find_min ();
const_iterator find_min () const;


      mapped_type& max ();
const mapped_type& max () const;


      iterator find_max ();
const_iterator find_max () const;


- For set / multiset:

      value_type& min ();
const value_type& min () const;


      iterator find_min ();
const_iterator find_min () const;


      value_type& max ();
const value_type& max () const;


      iterator find_max ();
const_iterator find_max () const;


--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/225ceb0e-a523-40b5-b49b-5302c7d69d49%40isocpp.or=
g.

------=_Part_5230_2101960016.1532073882544
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>Le vendredi 20 juillet 2018 06:00:15 UTC+2, Nicol =
Bolas a =C3=A9crit=C2=A0:<blockquote class=3D"gmail_quote" style=3D"margin:=
 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div =
dir=3D"ltr"><div>Considering that the fact that the container has an order =
is a fundamental part of its definition, it does make sense to be able to a=
ccess the first and last elements in it. Just like we can access the first =
and last elements of a sequence container.<br></div></div></blockquote><div=
><br></div><div>Thinking about it, maybe &#39;back&#39; and &#39;front&#39;=
 are names that are too strongly associated to &#39;sequence&#39; container=
s to be used here.</div><div><br></div><div>I propose &#39;min()&#39; and &=
#39;max()&#39;.<br></div><div><br></div><div>And I extend the scope of the =
proposal to include &#39;find_min()&#39;, &#39;find_max()&#39; which return=
 iterators.</div><div><br></div><div>Summarizing it, we could have these ad=
ditional methods:</div><div><br></div><div>- For map / multimap:</div><div>=
<br></div><div><pre style=3D"color: rgb(0, 128, 0); font-size: 12px; backgr=
ound-color: rgb(250, 255, 250);"><pre>      mapped_type&amp; min ();
const mapped_type&amp; min () const;</pre><pre><pre style=3D"background-col=
or: rgb(255, 255, 255);"><br></pre><pre style=3D"background-color: rgb(255,=
 255, 255);">      iterator find_min ();
const_iterator find_min () const;</pre></pre><pre><br></pre><pre>      mapp=
ed_type&amp; max ();
const mapped_type&amp; max () const;</pre><div><br></div></pre></div><div><=
pre style=3D"color: rgb(0, 128, 0); font-size: 12px;">      iterator find_m=
ax ();
const_iterator find_max () const;</pre></div><div><pre style=3D"color: rgb(=
0, 128, 0); font-size: 12px;"><br></pre><pre style=3D"color: rgb(0, 128, 0)=
; font-size: 12px;"><div style=3D"color: rgb(34, 34, 34); font-family: Aria=
l, Helvetica, sans-serif; font-size: 13px; white-space: normal;">- For set =
/ multiset:</div><div><br></div><div><div style=3D"color: rgb(34, 34, 34); =
font-family: Arial, Helvetica, sans-serif; font-size: 13px; white-space: no=
rmal;"><pre style=3D"color: rgb(0, 128, 0); font-size: 12px; background-col=
or: rgb(250, 255, 250);"><pre><div style=3D"color: rgb(34, 34, 34); font-fa=
mily: Arial, Helvetica, sans-serif; font-size: 13px; white-space: normal; b=
ackground-color: rgb(255, 255, 255);"><pre style=3D"color: rgb(0, 128, 0); =
font-size: 12px; background-color: rgb(250, 255, 250);"><pre>      value_ty=
pe&amp; min ();
const value_type&amp; min () const;<br></pre><pre><pre style=3D"background-=
color: rgb(255, 255, 255);"><br></pre><pre style=3D"background-color: rgb(2=
55, 255, 255);">      iterator find_min ();
const_iterator find_min () const;</pre></pre><pre><br></pre><pre>      valu=
e_type&amp; max ();<br>const value_type&amp; max () const;<br></pre><div><b=
r></div></pre></div><div style=3D"color: rgb(34, 34, 34); font-family: Aria=
l, Helvetica, sans-serif; font-size: 13px; white-space: normal; background-=
color: rgb(255, 255, 255);"><pre style=3D"color: rgb(0, 128, 0); font-size:=
 12px;">      iterator find_max ();
const_iterator find_max () const;</pre><div><br></div></div><div style=3D"c=
olor: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size=
: 13px; white-space: normal; background-color: rgb(255, 255, 255);"></div><=
/pre></pre></div><div style=3D"color: rgb(34, 34, 34); font-family: Arial, =
Helvetica, sans-serif; font-size: 13px; white-space: normal;"></div></div><=
/pre></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/225ceb0e-a523-40b5-b49b-5302c7d69d49%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/225ceb0e-a523-40b5-b49b-5302c7d69d49=
%40isocpp.org</a>.<br />

------=_Part_5230_2101960016.1532073882544--

------=_Part_5229_1465990399.1532073882543--

.


Author: Nicolas Lesser <blitzrakete@gmail.com>
Date: Fri, 20 Jul 2018 22:22:32 +0200
Raw View
--000000000000ae72200571740ef1
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

I'm strongly against the renaming. Although min() and max() might make more
sense for maps (I agree), but front() and back() are still appropriate
names as they really get the front and back elements respectively. It's
just that they have a more strict guarantee of their values in relation to
the other elements.

Another reason already employed by the standard library is reducing the
need of special casing templates. Maps have a count() function, even though
each element appears only once (for non-multi maps).

I don't think the iterator versions are necessary. Vector and co also don't
and frankly, I don't really see the motivation of including them. You can
get them easily by calling begin() and getting end()-1.

On Fri, Jul 20, 2018, 10:04 AM oliviersohn via ISO C++ Standard - Future
Proposals <std-proposals@isocpp.org> wrote:

>
>
> Le vendredi 20 juillet 2018 06:00:15 UTC+2, Nicol Bolas a =C3=A9crit :
>>
>> Considering that the fact that the container has an order is a
>> fundamental part of its definition, it does make sense to be able to acc=
ess
>> the first and last elements in it. Just like we can access the first and
>> last elements of a sequence container.
>>
>
> Thinking about it, maybe 'back' and 'front' are names that are too
> strongly associated to 'sequence' containers to be used here.
>
> I propose 'min()' and 'max()'.
>
> And I extend the scope of the proposal to include 'find_min()',
> 'find_max()' which return iterators.
>
> Summarizing it, we could have these additional methods:
>
> - For map / multimap:
>
>       mapped_type& min ();
> const mapped_type& min () const;
>
>
>       iterator find_min ();
> const_iterator find_min () const;
>
>
>       mapped_type& max ();
> const mapped_type& max () const;
>
>
>       iterator find_max ();
> const_iterator find_max () const;
>
>
> - For set / multiset:
>
>       value_type& min ();
> const value_type& min () const;
>
>
>       iterator find_min ();
> const_iterator find_min () const;
>
>
>       value_type& max ();
> const value_type& max () const;
>
>
>       iterator find_max ();
> const_iterator find_max () const;
>
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/225ceb0e-a52=
3-40b5-b49b-5302c7d69d49%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/225ceb0e-a5=
23-40b5-b49b-5302c7d69d49%40isocpp.org?utm_medium=3Demail&utm_source=3Dfoot=
er>
> .
>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CALmDwq3-%3DxOdpyXnAk9CSYzHyc4F82dVeU0pKH9LeaVKZ=
8zG-w%40mail.gmail.com.

--000000000000ae72200571740ef1
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto"><div>I&#39;m strongly against the renaming. Although min(=
) and max() might make more sense for maps (I agree), but front() and back(=
) are still appropriate names as they really get the front and back element=
s respectively. It&#39;s just that they have a more strict guarantee of the=
ir values in relation to the other elements.</div><div dir=3D"auto"><br></d=
iv><div dir=3D"auto">Another reason already employed by the standard librar=
y is reducing the need of special casing templates. Maps have a count() fun=
ction, even though each element appears only once (for non-multi maps).</di=
v><div dir=3D"auto"><br></div><div dir=3D"auto">I don&#39;t think the itera=
tor versions are necessary. Vector and co also don&#39;t and frankly, I don=
&#39;t really see the motivation of including them. You can get them easily=
 by calling begin() and getting end()-1.=C2=A0</div><div dir=3D"auto"><br><=
div class=3D"gmail_quote" dir=3D"auto"><div dir=3D"ltr">On Fri, Jul 20, 201=
8, 10:04 AM oliviersohn via ISO C++ Standard - Future Proposals &lt;<a href=
=3D"mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</a>&gt; wrote=
:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bor=
der-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><br><br>Le vendr=
edi 20 juillet 2018 06:00:15 UTC+2, Nicol Bolas a =C3=A9crit=C2=A0:<blockqu=
ote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1=
px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>Considering that the =
fact that the container has an order is a fundamental part of its definitio=
n, it does make sense to be able to access the first and last elements in i=
t. Just like we can access the first and last elements of a sequence contai=
ner.<br></div></div></blockquote><div><br></div><div>Thinking about it, may=
be &#39;back&#39; and &#39;front&#39; are names that are too strongly assoc=
iated to &#39;sequence&#39; containers to be used here.</div><div><br></div=
><div>I propose &#39;min()&#39; and &#39;max()&#39;.<br></div><div><br></di=
v><div>And I extend the scope of the proposal to include &#39;find_min()&#3=
9;, &#39;find_max()&#39; which return iterators.</div><div><br></div><div>S=
ummarizing it, we could have these additional methods:</div><div><br></div>=
<div>- For map / multimap:</div><div><br></div><div><pre style=3D"color:rgb=
(0,128,0);font-size:12px;background-color:rgb(250,255,250)"><pre>      mapp=
ed_type&amp; min ();
const mapped_type&amp; min () const;</pre><pre><pre style=3D"background-col=
or:rgb(255,255,255)"><br></pre><pre style=3D"background-color:rgb(255,255,2=
55)">      iterator find_min ();
const_iterator find_min () const;</pre></pre><pre><br></pre><pre>      mapp=
ed_type&amp; max ();
const mapped_type&amp; max () const;</pre><div><br></div></pre></div><div><=
pre style=3D"color:rgb(0,128,0);font-size:12px">      iterator find_max ();
const_iterator find_max () const;</pre></div><div><pre style=3D"color:rgb(0=
,128,0);font-size:12px"><br></pre><pre style=3D"color:rgb(0,128,0);font-siz=
e:12px"><div style=3D"color:rgb(34,34,34);font-family:Arial,Helvetica,sans-=
serif;font-size:13px;white-space:normal">- For set / multiset:</div><div><b=
r></div><div><div style=3D"color:rgb(34,34,34);font-family:Arial,Helvetica,=
sans-serif;font-size:13px;white-space:normal"><pre style=3D"color:rgb(0,128=
,0);font-size:12px;background-color:rgb(250,255,250)"><pre><div style=3D"co=
lor:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;whi=
te-space:normal;background-color:rgb(255,255,255)"><pre style=3D"color:rgb(=
0,128,0);font-size:12px;background-color:rgb(250,255,250)"><pre>      value=
_type&amp; min ();
const value_type&amp; min () const;<br></pre><pre><pre style=3D"background-=
color:rgb(255,255,255)"><br></pre><pre style=3D"background-color:rgb(255,25=
5,255)">      iterator find_min ();
const_iterator find_min () const;</pre></pre><pre><br></pre><pre>      valu=
e_type&amp; max ();<br>const value_type&amp; max () const;<br></pre><div><b=
r></div></pre></div><div style=3D"color:rgb(34,34,34);font-family:Arial,Hel=
vetica,sans-serif;font-size:13px;white-space:normal;background-color:rgb(25=
5,255,255)"><pre style=3D"color:rgb(0,128,0);font-size:12px">      iterator=
 find_max ();
const_iterator find_max () const;</pre><div><br></div></div><div style=3D"c=
olor:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;wh=
ite-space:normal;background-color:rgb(255,255,255)"></div></pre></pre></div=
><div style=3D"color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;f=
ont-size:13px;white-space:normal"></div></div></pre></div></div>

<p></p>

-- <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" target=3D"_=
blank" rel=3D"noreferrer">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank" rel=3D"noreferrer">std-proposals@isocpp.org</a>.<br=
>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/225ceb0e-a523-40b5-b49b-5302c7d69d49%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank" =
rel=3D"noreferrer">https://groups.google.com/a/isocpp.org/d/msgid/std-propo=
sals/225ceb0e-a523-40b5-b49b-5302c7d69d49%40isocpp.org</a>.<br>
</blockquote></div></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALmDwq3-%3DxOdpyXnAk9CSYzHyc4F82dVeU=
0pKH9LeaVKZ8zG-w%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALmDwq3-%3DxOd=
pyXnAk9CSYzHyc4F82dVeU0pKH9LeaVKZ8zG-w%40mail.gmail.com</a>.<br />

--000000000000ae72200571740ef1--

.


Author: "oliviersohn via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Fri, 20 Jul 2018 13:50:40 -0700 (PDT)
Raw View
------=_Part_6342_2044756838.1532119840770
Content-Type: multipart/alternative;
 boundary="----=_Part_6343_1569187912.1532119840770"

------=_Part_6343_1569187912.1532119840770
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable



Le vendredi 20 juillet 2018 22:22:41 UTC+2, Nicolas Lesser a =C3=A9crit :
>
> front() and back() are still appropriate names as they really get the=20
> front and back elements respectively.
>

I don't think front() and back() are appropriate because they are too vague=
=20
: after all, why would front() be the min element and not the max element?

min() and max() avoid this ambiguity.
=20

> I don't think the iterator versions are necessary. Vector and co also=20
> don't and frankly, I don't really see the motivation of including them. Y=
ou=20
> can get them easily by calling begin() and getting end()-1.=20
>

In my opinion, find_max() is easier to understand and more explicit than=20
end()-1. (and vector doesn't have it because there is no notion of max=20
element in a vector.)
=20

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/7215d0d3-6cd5-4691-aec5-ad9b6abd2d29%40isocpp.or=
g.

------=_Part_6343_1569187912.1532119840770
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><br></div><div><br>Le vendredi 20 juillet 2018 22:22:=
41 UTC+2, Nicolas Lesser a =C3=A9crit=C2=A0:<blockquote class=3D"gmail_quot=
e" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;paddin=
g-left: 1ex;"><div dir=3D"auto"><div>front() and back() are still appropria=
te names as they really get the front and back elements respectively.</div>=
</div></blockquote><div><br></div><div>I don&#39;t think front() and back()=
 are appropriate because they are too vague : after all, why would front() =
be the min element and not the max element?</div><div><br></div><div>min() =
and max() avoid this ambiguity.</div><div>=C2=A0</div><blockquote class=3D"=
gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc so=
lid;padding-left: 1ex;"><div dir=3D"auto"><div dir=3D"auto">I don&#39;t thi=
nk the iterator versions are necessary. Vector and co also don&#39;t and fr=
ankly, I don&#39;t really see the motivation of including them. You can get=
 them easily by calling begin() and getting end()-1.=C2=A0</div></div></blo=
ckquote><div><br></div><div>In my opinion, find_max() is easier to understa=
nd and more explicit than end()-1. (and vector doesn&#39;t have it because =
there is no notion of max element in a vector.)</div><div>=C2=A0<br></div><=
/div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/7215d0d3-6cd5-4691-aec5-ad9b6abd2d29%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/7215d0d3-6cd5-4691-aec5-ad9b6abd2d29=
%40isocpp.org</a>.<br />

------=_Part_6343_1569187912.1532119840770--

------=_Part_6342_2044756838.1532119840770--

.


Author: Richard Hodges <hodges.r@gmail.com>
Date: Fri, 20 Jul 2018 23:20:11 +0200
Raw View
--000000000000135e15057174dd11
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Fri, 20 Jul 2018 at 22:50, oliviersohn via ISO C++ Standard - Future
Proposals <std-proposals@isocpp.org> wrote:

>
>
> Le vendredi 20 juillet 2018 22:22:41 UTC+2, Nicolas Lesser a =C3=A9crit :
>>
>> front() and back() are still appropriate names as they really get the
>> front and back elements respectively.
>>
>
> I don't think front() and back() are appropriate because they are too
> vague : after all, why would front() be the min element and not the max
> element?
>
> min() and max() avoid this ambiguity.
>

The problem with min() and max() is that they start to lie the moment you
change the comparison function:

std::map< int, Foo, std::greater<> > m =3D make_map();
auto&& biggest_foo =3D m.min();  // what??

 note that the above confusion cannot happen with a vector or a deque -
maps are fundamentally different.

>
>
>
>> I don't think the iterator versions are necessary. Vector and co also
>> don't and frankly, I don't really see the motivation of including them. =
You
>> can get them easily by calling begin() and getting end()-1.
>>
>
> In my opinion, find_max() is easier to understand and more explicit than
> end()-1. (and vector doesn't have it because there is no notion of max
> element in a vector.)
>

 find_max() already has a synonym:

auto&& last_value =3D *std::rbegin(my_map);  // equivalent to find_max(),
which may well be lying
auto&& first_value =3D *std::begin(my_map);  // equivalent to find_min(),
which again may be lying


However, I would argue that the above 2 cases are merely special cases of
the general case:

auto&& nth_value =3D *std::next(std::begin(my_map), n);

Which is horribly inefficient and an indicator that we should not be using
a map!



>

>
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/7215d0d3-6cd=
5-4691-aec5-ad9b6abd2d29%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/7215d0d3-6c=
d5-4691-aec5-ad9b6abd2d29%40isocpp.org?utm_medium=3Demail&utm_source=3Dfoot=
er>
> .
>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CALvx3ha2qY8oy5eYQoUNZ0CVTqggMDWhfP5jMnQK_KGYKDQ=
cww%40mail.gmail.com.

--000000000000135e15057174dd11
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Fri=
, 20 Jul 2018 at 22:50, oliviersohn via ISO C++ Standard - Future Proposals=
 &lt;<a href=3D"mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</=
a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 =
0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div=
><br></div><div><br>Le vendredi 20 juillet 2018 22:22:41 UTC+2, Nicolas Les=
ser a =C3=A9crit=C2=A0:<blockquote class=3D"gmail_quote" style=3D"margin:0;=
margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"=
auto"><div>front() and back() are still appropriate names as they really ge=
t the front and back elements respectively.</div></div></blockquote><div><b=
r></div><div>I don&#39;t think front() and back() are appropriate because t=
hey are too vague : after all, why would front() be the min element and not=
 the max element?</div><div><br></div><div>min() and max() avoid this ambig=
uity.</div></div></div></blockquote><div><br></div><div>The problem with mi=
n() and max() is that they start to lie the moment you change the compariso=
n function:</div><div><br></div><div><font face=3D"monospace, monospace">st=
d::map&lt; int, Foo, std::greater&lt;&gt; &gt; m =3D make_map();</font></di=
v><div><font face=3D"monospace, monospace">auto&amp;&amp; biggest_foo =3D m=
..min();=C2=A0 // what??</font></div><div><font face=3D"monospace, monospace=
"><br></font></div><div><font face=3D"monospace, monospace"><div style=3D"f=
ont-family:Arial,Helvetica,sans-serif;font-size:small;background-color:rgb(=
255,255,255);text-decoration-style:initial;text-decoration-color:initial">=
=C2=A0note that the above confusion cannot happen with a vector or a deque =
- maps are fundamentally different.</div><blockquote class=3D"gmail_quote" =
style=3D"font-family:Arial,Helvetica,sans-serif;font-size:small;background-=
color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:=
initial;margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pad=
ding-left:1ex"><div dir=3D"ltr"><div><br class=3D"gmail-Apple-interchange-n=
ewline"></div></div></blockquote></font></div><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex=
"><div dir=3D"ltr"><div><div>=C2=A0</div><blockquote class=3D"gmail_quote" =
style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left=
:1ex"><div dir=3D"auto"><div dir=3D"auto">I don&#39;t think the iterator ve=
rsions are necessary. Vector and co also don&#39;t and frankly, I don&#39;t=
 really see the motivation of including them. You can get them easily by ca=
lling begin() and getting end()-1.=C2=A0</div></div></blockquote><div><br><=
/div><div>In my opinion, find_max() is easier to understand and more explic=
it than end()-1. (and vector doesn&#39;t have it because there is no notion=
 of max element in a vector.)</div><div></div></div></div></blockquote><div=
><div style=3D"font-size:small;background-color:rgb(255,255,255);text-decor=
ation-style:initial;text-decoration-color:initial"><br></div><div style=3D"=
font-size:small;background-color:rgb(255,255,255);text-decoration-style:ini=
tial;text-decoration-color:initial">=C2=A0find_max() already has a synonym:=
<br></div><div style=3D"font-size:small;background-color:rgb(255,255,255);t=
ext-decoration-style:initial;text-decoration-color:initial"><br></div><div =
style=3D"font-size:small;background-color:rgb(255,255,255);text-decoration-=
style:initial;text-decoration-color:initial"><div style=3D"text-decoration-=
style:initial;text-decoration-color:initial"><font face=3D"monospace, monos=
pace">auto&amp;&amp; last_value =3D *std::rbegin(my_map);=C2=A0 // equivale=
nt to find_max(), which may well be lying</font></div><div style=3D"text-de=
coration-style:initial;text-decoration-color:initial"><font face=3D"monospa=
ce, monospace">auto&amp;&amp; first_value =3D *std::begin(my_map);</font><s=
pan style=3D"font-family:monospace,monospace">=C2=A0 // equivalent to find_=
min(), which again may be lying</span><font face=3D"monospace, monospace"><=
br><br class=3D"gmail-Apple-interchange-newline"></font></div><div style=3D=
"text-decoration-style:initial;text-decoration-color:initial"><br></div><di=
v style=3D"text-decoration-style:initial;text-decoration-color:initial">How=
ever, I would argue that the above 2 cases are merely special cases of the =
general case:</div><div style=3D"text-decoration-style:initial;text-decorat=
ion-color:initial"><br></div><div style=3D"text-decoration-style:initial;te=
xt-decoration-color:initial"><font face=3D"monospace, monospace">auto&amp;&=
amp; nth_value =3D *std::next(std::begin(my_map), n);</font></div><br class=
=3D"gmail-Apple-interchange-newline">Which is horribly inefficient and an i=
ndicator that we should not be using a map!</div><div style=3D"font-size:sm=
all;background-color:rgb(255,255,255);text-decoration-style:initial;text-de=
coration-color:initial"><br></div><div style=3D"font-size:small;background-=
color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:=
initial"><br></div><blockquote class=3D"gmail_quote" style=3D"font-size:sma=
ll;background-color:rgb(255,255,255);text-decoration-style:initial;text-dec=
oration-color:initial;margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(20=
4,204,204);padding-left:1ex"><div dir=3D"ltr"><div><br class=3D"gmail-Apple=
-interchange-newline"></div></div></blockquote>=C2=A0</div><blockquote clas=
s=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pad=
ding-left:1ex"><div dir=3D"ltr"><div><div>=C2=A0<br></div></div></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/7215d0d3-6cd5-4691-aec5-ad9b6abd2d29%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/7215d0d3-6cd5-=
4691-aec5-ad9b6abd2d29%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3ha2qY8oy5eYQoUNZ0CVTqggMDWhfP5j=
MnQK_KGYKDQcww%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3ha2qY8oy5eY=
QoUNZ0CVTqggMDWhfP5jMnQK_KGYKDQcww%40mail.gmail.com</a>.<br />

--000000000000135e15057174dd11--

.


Author: "oliviersohn via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Fri, 20 Jul 2018 14:36:15 -0700 (PDT)
Raw View
------=_Part_6382_1099176497.1532122575301
Content-Type: multipart/alternative;
 boundary="----=_Part_6383_661438312.1532122575301"

------=_Part_6383_661438312.1532122575301
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Le vendredi 20 juillet 2018 23:20:24 UTC+2, Richard Hodges a =C3=A9crit :
>
>
> The problem with min() and max() is that they start to lie the moment you=
=20
> change the comparison function:
>
> std::map< int, Foo, std::greater<> > m =3D make_map();
> auto&& biggest_foo =3D m.min();  // what??
>
=20

Allow me to disagree : min() and max() respectively return the min and max=
=20
elements with respect to the order induced on values by map::key_compare.

So they don't lie.

=20

>  find_max() already has a synonym:
>
> auto&& last_value =3D *std::rbegin(my_map);  // equivalent to find_max(),=
=20
> which may well be lying
> auto&& first_value =3D *std::begin(my_map);  // equivalent to find_min(),=
=20
> which again may be lying
>
>
I think my_map.find_max() is more explicit and easy to understand than=20
*std::rbegin(my_map)


> However, I would argue that the above 2 cases are merely special cases of=
=20
> the general case:
>
> auto&& nth_value =3D *std::next(std::begin(my_map), n);
>
> Which is horribly inefficient and an indicator that we should not be usin=
g=20
> a map!
>

This is another debate, it's not the subject of this thread.

>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/1155a697-2ce6-4a02-b071-118d101a3c0a%40isocpp.or=
g.

------=_Part_6383_661438312.1532122575301
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Le vendredi 20 juillet 2018 23:20:24 UTC+2, Richard Hodges=
 a =C3=A9crit=C2=A0:<blockquote class=3D"gmail_quote" style=3D"margin: 0;ma=
rgin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=
=3D"ltr"><div class=3D"gmail_quote"><div><br></div><div>The problem with mi=
n() and max() is that they start to lie the moment you change the compariso=
n function:</div><div><br></div><div><font face=3D"monospace, monospace">st=
d::map&lt; int, Foo, std::greater&lt;&gt; &gt; m =3D make_map();</font></di=
v><div><font face=3D"monospace, monospace">auto&amp;&amp; biggest_foo =3D m=
..min();=C2=A0 // what??</font></div></div></div></blockquote><div>=C2=A0</d=
iv><div><br></div><div><font color=3D"#000000">Allow me to disagree : min()=
 and max() respectively return the min and max elements with respect to the=
 order induced on values by=C2=A0<span style=3D"font-size: 12px;">map::key_=
compare.</span></font></div><div><font color=3D"#000000"><span style=3D"fon=
t-size: 12px;"><br></span></font></div><div><font color=3D"#000000"><span s=
tyle=3D"font-size: 12px;">So they don&#39;t lie.</span></font><br></div><di=
v><font color=3D"#000000"><span style=3D"font-size: 12px;"><br></span></fon=
t></div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:=
 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div =
dir=3D"ltr"><div class=3D"gmail_quote"><div><div style=3D"font-size:small;b=
ackground-color:rgb(255,255,255)">=C2=A0find_max() already has a synonym:<b=
r></div><div style=3D"font-size:small;background-color:rgb(255,255,255)"><b=
r></div><div style=3D"font-size:small;background-color:rgb(255,255,255)"><d=
iv><font face=3D"monospace, monospace">auto&amp;&amp; last_value =3D *std::=
rbegin(my_map);=C2=A0 // equivalent to find_max(), which may well be lying<=
/font></div><div><font face=3D"monospace, monospace">auto&amp;&amp; first_v=
alue =3D *std::begin(my_map);</font><span style=3D"font-family:monospace,mo=
nospace">=C2=A0 // equivalent to find_min(), which again may be lying</span=
><font face=3D"monospace, monospace"><br><br></font></div></div></div></div=
></div></blockquote><div><br></div><div>I think=C2=A0<span style=3D"font-fa=
mily: monospace, monospace; font-size: small;">my_map.</span>find_max() is =
more explicit and easy to understand than=C2=A0<span style=3D"font-size: sm=
all; font-family: monospace, monospace;">*std::rbegin(my_map)</span></div><=
div><br></div><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"=
><div class=3D"gmail_quote"><div><div style=3D"font-size:small;background-c=
olor:rgb(255,255,255)"><div><font face=3D"monospace, monospace"></font></di=
v><div><br></div><div>However, I would argue that the above 2 cases are mer=
ely special cases of the general case:</div><div><br></div><div><font face=
=3D"monospace, monospace">auto&amp;&amp; nth_value =3D *std::next(std::begi=
n(my_map), n);</font></div><br>Which is horribly inefficient and an indicat=
or that we should not be using a map!</div></div></div></div></blockquote><=
div><br></div><div>This is another debate, it&#39;s not the subject of this=
 thread.</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
</blockquote></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/1155a697-2ce6-4a02-b071-118d101a3c0a%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/1155a697-2ce6-4a02-b071-118d101a3c0a=
%40isocpp.org</a>.<br />

------=_Part_6383_661438312.1532122575301--

------=_Part_6382_1099176497.1532122575301--

.


Author: Richard Hodges <hodges.r@gmail.com>
Date: Sat, 21 Jul 2018 10:52:20 +0200
Raw View
--0000000000005fb38205717e889f
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Fri, 20 Jul 2018 at 23:36, oliviersohn via ISO C++ Standard - Future
Proposals <std-proposals@isocpp.org> wrote:

> Le vendredi 20 juillet 2018 23:20:24 UTC+2, Richard Hodges a =C3=A9crit :
>>
>>
>> The problem with min() and max() is that they start to lie the moment yo=
u
>> change the comparison function:
>>
>> std::map< int, Foo, std::greater<> > m =3D make_map();
>> auto&& biggest_foo =3D m.min();  // what??
>>
>
>
> Allow me to disagree : min() and max() respectively return the min and ma=
x
> elements with respect to the order induced on values by map::key_compare.
>
> So they don't lie.
>
>
>
>>  find_max() already has a synonym:
>>
>> auto&& last_value =3D *std::rbegin(my_map);  // equivalent to find_max()=
,
>> which may well be lying
>> auto&& first_value =3D *std::begin(my_map);  // equivalent to find_min()=
,
>> which again may be lying
>>
>>
> I think my_map.find_max() is more explicit and easy to understand than
> *std::rbegin(my_map)
>
>
>> However, I would argue that the above 2 cases are merely special cases o=
f
>> the general case:
>>
>> auto&& nth_value =3D *std::next(std::begin(my_map), n);
>>
>> Which is horribly inefficient and an indicator that we should not be
>> using a map!
>>
>
> This is another debate, it's not the subject of this thread.
>

I disagree on this point. My position is quite firmly that if you need
random access to a map's element, then a map id probably the wrong
container for the use case.

the proposed map.min() is the same operation as map[0] where
map::operator[] is defined as:

decltype(auto) map::operator()(std::size_t n)
{
  return *std::next(begin(), n);
}

map.max() is similarly a special case of map[map.size()-1-n], where n is 0.
Which can of course be optimised if written in terms of the reverse
iterator.

My first question would be "why are we coding for special cases?".
My second question would be "what is the use case for the general case?"
My third (repeated) question would be "what is the use case for the special
case where n is zero?"

We have seen one use case which was expressed more efficiently with arrays.

I argue further that the purpose of a map of any kind is to act as a
dictionary or index of keys to values. If we care at which ordinal an
object lives in the map, we probably shouldn't use a map.




> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1155a697-2ce=
6-4a02-b071-118d101a3c0a%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1155a697-2c=
e6-4a02-b071-118d101a3c0a%40isocpp.org?utm_medium=3Demail&utm_source=3Dfoot=
er>
> .
>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CALvx3hZ2ry-smbwoUSJByPkTCBadq5JzpHNukNj2XTKdGVw=
s7w%40mail.gmail.com.

--0000000000005fb38205717e889f
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Fri=
, 20 Jul 2018 at 23:36, oliviersohn via ISO C++ Standard - Future Proposals=
 &lt;<a href=3D"mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</=
a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 =
0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Le v=
endredi 20 juillet 2018 23:20:24 UTC+2, Richard Hodges a =C3=A9crit=C2=A0:<=
blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border=
-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div class=3D"gmail=
_quote"><div><br></div><div>The problem with min() and max() is that they s=
tart to lie the moment you change the comparison function:</div><div><br></=
div><div><font face=3D"monospace, monospace">std::map&lt; int, Foo, std::gr=
eater&lt;&gt; &gt; m =3D make_map();</font></div><div><font face=3D"monospa=
ce, monospace">auto&amp;&amp; biggest_foo =3D m.min();=C2=A0 // what??</fon=
t></div></div></div></blockquote><div>=C2=A0</div><div><br></div><div><font=
 color=3D"#000000">Allow me to disagree : min() and max() respectively retu=
rn the min and max elements with respect to the order induced on values by=
=C2=A0<span style=3D"font-size:12px">map::key_compare.</span></font></div><=
div><font color=3D"#000000"><span style=3D"font-size:12px"><br></span></fon=
t></div><div><font color=3D"#000000"><span style=3D"font-size:12px">So they=
 don&#39;t lie.</span></font><br></div><div><font color=3D"#000000"><span s=
tyle=3D"font-size:12px"><br></span></font></div><div>=C2=A0</div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px=
 #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_quote"><=
div><div style=3D"font-size:small;background-color:rgb(255,255,255)">=C2=A0=
find_max() already has a synonym:<br></div><div style=3D"font-size:small;ba=
ckground-color:rgb(255,255,255)"><br></div><div style=3D"font-size:small;ba=
ckground-color:rgb(255,255,255)"><div><font face=3D"monospace, monospace">a=
uto&amp;&amp; last_value =3D *std::rbegin(my_map);=C2=A0 // equivalent to f=
ind_max(), which may well be lying</font></div><div><font face=3D"monospace=
, monospace">auto&amp;&amp; first_value =3D *std::begin(my_map);</font><spa=
n style=3D"font-family:monospace,monospace">=C2=A0 // equivalent to find_mi=
n(), which again may be lying</span><font face=3D"monospace, monospace"><br=
><br></font></div></div></div></div></div></blockquote><div><br></div><div>=
I think=C2=A0<span style=3D"font-family:monospace,monospace;font-size:small=
">my_map.</span>find_max() is more explicit and easy to understand than=C2=
=A0<span style=3D"font-size:small;font-family:monospace,monospace">*std::rb=
egin(my_map)</span></div><div><br></div><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:=
1ex"><div dir=3D"ltr"><div class=3D"gmail_quote"><div><div style=3D"font-si=
ze:small;background-color:rgb(255,255,255)"><div><font face=3D"monospace, m=
onospace"></font></div><div><br></div><div>However, I would argue that the =
above 2 cases are merely special cases of the general case:</div><div><br><=
/div><div><font face=3D"monospace, monospace">auto&amp;&amp; nth_value =3D =
*std::next(std::begin(my_map), n);</font></div><br>Which is horribly ineffi=
cient and an indicator that we should not be using a map!</div></div></div>=
</div></blockquote><div><br></div><div>This is another debate, it&#39;s not=
 the subject of this thread.</div></div></blockquote><div><br></div><div>I =
disagree on this point. My position is quite firmly that if you need random=
 access to a map&#39;s element, then a map id probably the wrong container =
for the use case.</div><div><br></div><div>the proposed map.min() is the sa=
me operation as map[0] where map::operator[] is defined as:</div><div><br><=
/div><div><font face=3D"monospace, monospace">decltype(auto) map::operator(=
)(std::size_t n)</font></div><div><font face=3D"monospace, monospace">{</fo=
nt></div><div><font face=3D"monospace, monospace">=C2=A0 return *std::next(=
begin(), n);</font></div><div><font face=3D"monospace, monospace">}</font><=
/div><div><br></div><div>map.max() is similarly a special case of map[map.s=
ize()-1-n], where n is 0. Which can of course be optimised if written in te=
rms of the reverse iterator.</div><div><br></div><div>My first question wou=
ld be &quot;why are we coding for special cases?&quot;.</div><div><div styl=
e=3D"font-size:small;background-color:rgb(255,255,255);text-decoration-styl=
e:initial;text-decoration-color:initial">My second question would be &quot;=
what is the use case for the general case?&quot;</div><div style=3D"font-si=
ze:small;background-color:rgb(255,255,255);text-decoration-style:initial;te=
xt-decoration-color:initial">My third (repeated) question would be &quot;wh=
at is the use case for the special case where n is zero?&quot;</div><div><b=
r></div>We have seen one use case which was expressed more efficiently with=
 arrays.</div><div><br></div><div>I argue further that the purpose of a map=
 of any kind is to act as a dictionary or index of keys to values. If we ca=
re at which ordinal an object lives in the map, we probably shouldn&#39;t u=
se a map.</div><div><br class=3D"gmail-Apple-interchange-newline">=C2=A0<br=
></div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0=
 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><bl=
ockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-l=
eft:1px #ccc solid;padding-left:1ex">
</blockquote></div>

<p></p>

-- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/1155a697-2ce6-4a02-b071-118d101a3c0a%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1155a697-2ce6-=
4a02-b071-118d101a3c0a%40isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hZ2ry-smbwoUSJByPkTCBadq5JzpHNu=
kNj2XTKdGVws7w%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hZ2ry-smbwo=
USJByPkTCBadq5JzpHNukNj2XTKdGVws7w%40mail.gmail.com</a>.<br />

--0000000000005fb38205717e889f--

.


Author: Magnus Fromreide <magfr@lysator.liu.se>
Date: Sat, 21 Jul 2018 11:15:04 +0200
Raw View
On Fri, Jul 20, 2018 at 02:36:15PM -0700, oliviersohn via ISO C++ Standard =
- Future Proposals wrote:
> Le vendredi 20 juillet 2018 23:20:24 UTC+2, Richard Hodges a =C3=A9crit :
> >
> >
> > The problem with min() and max() is that they start to lie the moment y=
ou=20
> > change the comparison function:
> >
> > std::map< int, Foo, std::greater<> > m =3D make_map();
> > auto&& biggest_foo =3D m.min();  // what??
> >
> =20
>=20
> Allow me to disagree : min() and max() respectively return the min and ma=
x=20
> elements with respect to the order induced on values by map::key_compare.
>=20
> So they don't lie.
>=20
> =20
>=20
> >  find_max() already has a synonym:
> >
> > auto&& last_value =3D *std::rbegin(my_map);  // equivalent to find_max(=
),=20
> > which may well be lying
> > auto&& first_value =3D *std::begin(my_map);  // equivalent to find_min(=
),=20
> > which again may be lying
> >
> >
> I think my_map.find_max() is more explicit and easy to understand than=20
> *std::rbegin(my_map)

While that might be true I still think

my_map.back()

is just as understandable as my_map().find_max() and have the added benefit=
 of
following the same conventions as the sequence containers, thus allowing
the use of maps in more places where they might not be a great fit but stil=
l
works ok if this is a secondary use.

Remember that one of the great advantages of the STL is that functions that
do the same thing have the same name so unless you think vector should
have a find_max() that does the same thing as last() this sounds odd indeed=
..

/MF

>=20
> > However, I would argue that the above 2 cases are merely special cases =
of=20
> > the general case:
> >
> > auto&& nth_value =3D *std::next(std::begin(my_map), n);
> >
> > Which is horribly inefficient and an indicator that we should not be us=
ing=20
> > a map!
> >
>=20
> This is another debate, it's not the subject of this thread.
>=20
> >
>=20
> --=20
> 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.
> To view this discussion on the web visit https://groups.google.com/a/isoc=
pp.org/d/msgid/std-proposals/1155a697-2ce6-4a02-b071-118d101a3c0a%40isocpp.=
org.

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/20180721091504.GA22899%40noemi.

.


Author: Magnus Fromreide <magfr@lysator.liu.se>
Date: Sat, 21 Jul 2018 11:18:11 +0200
Raw View
On Sat, Jul 21, 2018 at 10:52:20AM +0200, Richard Hodges wrote:
> On Fri, 20 Jul 2018 at 23:36, oliviersohn via ISO C++ Standard - Future
> Proposals <std-proposals@isocpp.org> wrote:
>=20
> > Le vendredi 20 juillet 2018 23:20:24 UTC+2, Richard Hodges a =C3=A9crit=
 :
> >>
> >>
> >> The problem with min() and max() is that they start to lie the moment =
you
> >> change the comparison function:
> >>
> >> std::map< int, Foo, std::greater<> > m =3D make_map();
> >> auto&& biggest_foo =3D m.min();  // what??
> >>
> >
> >
> > Allow me to disagree : min() and max() respectively return the min and =
max
> > elements with respect to the order induced on values by map::key_compar=
e.
> >
> > So they don't lie.
> >
> >
> >
> >>  find_max() already has a synonym:
> >>
> >> auto&& last_value =3D *std::rbegin(my_map);  // equivalent to find_max=
(),
> >> which may well be lying
> >> auto&& first_value =3D *std::begin(my_map);  // equivalent to find_min=
(),
> >> which again may be lying
> >>
> >>
> > I think my_map.find_max() is more explicit and easy to understand than
> > *std::rbegin(my_map)
> >
> >
> >> However, I would argue that the above 2 cases are merely special cases=
 of
> >> the general case:
> >>
> >> auto&& nth_value =3D *std::next(std::begin(my_map), n);
> >>
> >> Which is horribly inefficient and an indicator that we should not be
> >> using a map!
> >>
> >
> > This is another debate, it's not the subject of this thread.
> >
>=20
> I disagree on this point. My position is quite firmly that if you need
> random access to a map's element, then a map id probably the wrong
> container for the use case.
>=20
> the proposed map.min() is the same operation as map[0] where
> map::operator[] is defined as:
>=20
> decltype(auto) map::operator()(std::size_t n)
> {
>   return *std::next(begin(), n);
> }
>=20
> map.max() is similarly a special case of map[map.size()-1-n], where n is =
0.
> Which can of course be optimised if written in terms of the reverse
> iterator.
>=20
> My first question would be "why are we coding for special cases?".
> My second question would be "what is the use case for the general case?"
> My third (repeated) question would be "what is the use case for the speci=
al
> case where n is zero?"
>=20
> We have seen one use case which was expressed more efficiently with array=
s.
>=20
> I argue further that the purpose of a map of any kind is to act as a
> dictionary or index of keys to values. If we care at which ordinal an
> object lives in the map, we probably shouldn't use a map.

I would say that if we truly don't care of the ordinal then an unordered_ma=
p
is the right type. I have used maps when my primary use is lookup and I hav=
e a
secondary use that need iteration.

/MF

> > --
> > You received this message because you are subscribed to the Google Grou=
ps
> > "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.
> > To view this discussion on the web visit
> > https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1155a697-2=
ce6-4a02-b071-118d101a3c0a%40isocpp.org
> > <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1155a697-=
2ce6-4a02-b071-118d101a3c0a%40isocpp.org?utm_medium=3Demail&utm_source=3Dfo=
oter>
> > .
> >
>=20
> --=20
> 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.
> To view this discussion on the web visit https://groups.google.com/a/isoc=
pp.org/d/msgid/std-proposals/CALvx3hZ2ry-smbwoUSJByPkTCBadq5JzpHNukNj2XTKdG=
Vws7w%40mail.gmail.com.

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/20180721091811.GB22899%40noemi.

.


Author: Richard Hodges <hodges.r@gmail.com>
Date: Sat, 21 Jul 2018 12:04:23 +0200
Raw View
--0000000000000a9d8905717f8ac0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Sat, 21 Jul 2018 at 11:18, Magnus Fromreide <magfr@lysator.liu.se> wrote=
:

> On Sat, Jul 21, 2018 at 10:52:20AM +0200, Richard Hodges wrote:
> > On Fri, 20 Jul 2018 at 23:36, oliviersohn via ISO C++ Standard - Future
> > Proposals <std-proposals@isocpp.org> wrote:
> >
> > > Le vendredi 20 juillet 2018 23:20:24 UTC+2, Richard Hodges a =C3=A9cr=
it :
> > >>
> > >>
> > >> The problem with min() and max() is that they start to lie the momen=
t
> you
> > >> change the comparison function:
> > >>
> > >> std::map< int, Foo, std::greater<> > m =3D make_map();
> > >> auto&& biggest_foo =3D m.min();  // what??
> > >>
> > >
> > >
> > > Allow me to disagree : min() and max() respectively return the min an=
d
> max
> > > elements with respect to the order induced on values by
> map::key_compare.
> > >
> > > So they don't lie.
> > >
> > >
> > >
> > >>  find_max() already has a synonym:
> > >>
> > >> auto&& last_value =3D *std::rbegin(my_map);  // equivalent to
> find_max(),
> > >> which may well be lying
> > >> auto&& first_value =3D *std::begin(my_map);  // equivalent to
> find_min(),
> > >> which again may be lying
> > >>
> > >>
> > > I think my_map.find_max() is more explicit and easy to understand tha=
n
> > > *std::rbegin(my_map)
> > >
> > >
> > >> However, I would argue that the above 2 cases are merely special
> cases of
> > >> the general case:
> > >>
> > >> auto&& nth_value =3D *std::next(std::begin(my_map), n);
> > >>
> > >> Which is horribly inefficient and an indicator that we should not be
> > >> using a map!
> > >>
> > >
> > > This is another debate, it's not the subject of this thread.
> > >
> >
> > I disagree on this point. My position is quite firmly that if you need
> > random access to a map's element, then a map id probably the wrong
> > container for the use case.
> >
> > the proposed map.min() is the same operation as map[0] where
> > map::operator[] is defined as:
> >
> > decltype(auto) map::operator()(std::size_t n)
> > {
> >   return *std::next(begin(), n);
> > }
> >
> > map.max() is similarly a special case of map[map.size()-1-n], where n i=
s
> 0.
> > Which can of course be optimised if written in terms of the reverse
> > iterator.
> >
> > My first question would be "why are we coding for special cases?".
> > My second question would be "what is the use case for the general case?=
"
> > My third (repeated) question would be "what is the use case for the
> special
> > case where n is zero?"
> >
> > We have seen one use case which was expressed more efficiently with
> arrays.
> >
> > I argue further that the purpose of a map of any kind is to act as a
> > dictionary or index of keys to values. If we care at which ordinal an
> > object lives in the map, we probably shouldn't use a map.
>
> I would say that if we truly don't care of the ordinal then an
> unordered_map
> is the right type. I have used maps when my primary use is lookup and I
> have a
> secondary use that need iteration.
>
>
I completely agree with you on the two use cases but my analysis is subtly
different:

If the secondary use is ordered iteration, then all that concerns us is the
ordered nature of the iteration. We do not actually care *which* element is
the first and which is the last, just as we don't care which is the Nth.
All we need to know is that *it will yield a value with a weakly lower key
than *(it+1).




> /MF
>
> > > --
> > > 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, sen=
d
> an
> > > email to std-proposals+unsubscribe@isocpp.org.
> > > To post to this group, send email to std-proposals@isocpp.org.
> > > To view this discussion on the web visit
> > >
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1155a697-2ce=
6-4a02-b071-118d101a3c0a%40isocpp.org
> > > <
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1155a697-2ce=
6-4a02-b071-118d101a3c0a%40isocpp.org?utm_medium=3Demail&utm_source=3Dfoote=
r
> >
> > > .
> > >
> >
> > --
> > 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.
> > To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hZ2ry-=
smbwoUSJByPkTCBadq5JzpHNukNj2XTKdGVws7w%40mail.gmail.com
> .
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/201807210918=
11.GB22899%40noemi
> .
>

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/CALvx3hbHd4fTG0yj6JwghCKs5Vf6f%2BVJXt0STankf6Wha=
28PrA%40mail.gmail.com.

--0000000000000a9d8905717f8ac0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Sat=
, 21 Jul 2018 at 11:18, Magnus Fromreide &lt;<a href=3D"mailto:magfr@lysato=
r.liu.se">magfr@lysator.liu.se</a>&gt; wrote:<br></div><blockquote class=3D=
"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding=
-left:1ex">On Sat, Jul 21, 2018 at 10:52:20AM +0200, Richard Hodges wrote:<=
br>
&gt; On Fri, 20 Jul 2018 at 23:36, oliviersohn via ISO C++ Standard - Futur=
e<br>
&gt; Proposals &lt;<a href=3D"mailto:std-proposals@isocpp.org" target=3D"_b=
lank">std-proposals@isocpp.org</a>&gt; wrote:<br>
&gt; <br>
&gt; &gt; Le vendredi 20 juillet 2018 23:20:24 UTC+2, Richard Hodges a =C3=
=A9crit :<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; The problem with min() and max() is that they start to lie th=
e moment you<br>
&gt; &gt;&gt; change the comparison function:<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; std::map&lt; int, Foo, std::greater&lt;&gt; &gt; m =3D make_m=
ap();<br>
&gt; &gt;&gt; auto&amp;&amp; biggest_foo =3D m.min();=C2=A0 // what??<br>
&gt; &gt;&gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; Allow me to disagree : min() and max() respectively return the mi=
n and max<br>
&gt; &gt; elements with respect to the order induced on values by map::key_=
compare.<br>
&gt; &gt;<br>
&gt; &gt; So they don&#39;t lie.<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;&gt;=C2=A0 find_max() already has a synonym:<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; auto&amp;&amp; last_value =3D *std::rbegin(my_map);=C2=A0 // =
equivalent to find_max(),<br>
&gt; &gt;&gt; which may well be lying<br>
&gt; &gt;&gt; auto&amp;&amp; first_value =3D *std::begin(my_map);=C2=A0 // =
equivalent to find_min(),<br>
&gt; &gt;&gt; which again may be lying<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt;<br>
&gt; &gt; I think my_map.find_max() is more explicit and easy to understand=
 than<br>
&gt; &gt; *std::rbegin(my_map)<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;&gt; However, I would argue that the above 2 cases are merely spec=
ial cases of<br>
&gt; &gt;&gt; the general case:<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; auto&amp;&amp; nth_value =3D *std::next(std::begin(my_map), n=
);<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; Which is horribly inefficient and an indicator that we should=
 not be<br>
&gt; &gt;&gt; using a map!<br>
&gt; &gt;&gt;<br>
&gt; &gt;<br>
&gt; &gt; This is another debate, it&#39;s not the subject of this thread.<=
br>
&gt; &gt;<br>
&gt; <br>
&gt; I disagree on this point. My position is quite firmly that if you need=
<br>
&gt; random access to a map&#39;s element, then a map id probably the wrong=
<br>
&gt; container for the use case.<br>
&gt; <br>
&gt; the proposed map.min() is the same operation as map[0] where<br>
&gt; map::operator[] is defined as:<br>
&gt; <br>
&gt; decltype(auto) map::operator()(std::size_t n)<br>
&gt; {<br>
&gt;=C2=A0 =C2=A0return *std::next(begin(), n);<br>
&gt; }<br>
&gt; <br>
&gt; map.max() is similarly a special case of map[map.size()-1-n], where n =
is 0.<br>
&gt; Which can of course be optimised if written in terms of the reverse<br=
>
&gt; iterator.<br>
&gt; <br>
&gt; My first question would be &quot;why are we coding for special cases?&=
quot;.<br>
&gt; My second question would be &quot;what is the use case for the general=
 case?&quot;<br>
&gt; My third (repeated) question would be &quot;what is the use case for t=
he special<br>
&gt; case where n is zero?&quot;<br>
&gt; <br>
&gt; We have seen one use case which was expressed more efficiently with ar=
rays.<br>
&gt; <br>
&gt; I argue further that the purpose of a map of any kind is to act as a<b=
r>
&gt; dictionary or index of keys to values. If we care at which ordinal an<=
br>
&gt; object lives in the map, we probably shouldn&#39;t use a map.<br>
<br>
I would say that if we truly don&#39;t care of the ordinal then an unordere=
d_map<br>
is the right type. I have used maps when my primary use is lookup and I hav=
e a<br>
secondary use that need iteration.<br>
<br></blockquote><div><br></div><div>I completely agree with you on the two=
 use cases but my analysis is subtly different:</div><div><br></div><div>If=
 the secondary use is ordered iteration, then all that concerns us is the o=
rdered nature of the iteration. We do not actually care *which* element is =
the first and which is the last, just as we don&#39;t care which is the Nth=
.. All we need to know is that <font face=3D"monospace, monospace">*it</font=
> will yield a value with a weakly lower key than <font face=3D"monospace, =
monospace">*(it+1)</font>.</div><div>=C2=A0</div><div><br></div><div>=C2=A0=
</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l=
eft:1px #ccc solid;padding-left:1ex">
/MF<br>
<br>
&gt; &gt; --<br>
&gt; &gt; You received this message because you are subscribed to the Googl=
e Groups<br>
&gt; &gt; &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
&gt; &gt; To unsubscribe from this group and stop receiving emails from it,=
 send an<br>
&gt; &gt; email to <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org=
" target=3D"_blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
&gt; &gt; To post to this group, send email to <a href=3D"mailto:std-propos=
als@isocpp.org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
&gt; &gt; To view this discussion on the web visit<br>
&gt; &gt; <a href=3D"https://groups.google.com/a/isocpp.org/d/msgid/std-pro=
posals/1155a697-2ce6-4a02-b071-118d101a3c0a%40isocpp.org" rel=3D"noreferrer=
" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-prop=
osals/1155a697-2ce6-4a02-b071-118d101a3c0a%40isocpp.org</a><br>
&gt; &gt; &lt;<a href=3D"https://groups.google.com/a/isocpp.org/d/msgid/std=
-proposals/1155a697-2ce6-4a02-b071-118d101a3c0a%40isocpp.org?utm_medium=3De=
mail&amp;utm_source=3Dfooter" rel=3D"noreferrer" target=3D"_blank">https://=
groups.google.com/a/isocpp.org/d/msgid/std-proposals/1155a697-2ce6-4a02-b07=
1-118d101a3c0a%40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter</a>&=
gt;<br>
&gt; &gt; .<br>
&gt; &gt;<br>
&gt; <br>
&gt; -- <br>
&gt; You received this message because you are subscribed to the Google Gro=
ups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
&gt; To unsubscribe from this group and stop receiving emails from it, send=
 an email to <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org" targ=
et=3D"_blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
&gt; To post to this group, send email to <a href=3D"mailto:std-proposals@i=
socpp.org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
&gt; To view this discussion on the web visit <a href=3D"https://groups.goo=
gle.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hZ2ry-smbwoUSJByPkTCBadq5J=
zpHNukNj2XTKdGVws7w%40mail.gmail.com" rel=3D"noreferrer" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hZ2ry-sm=
bwoUSJByPkTCBadq5JzpHNukNj2XTKdGVws7w%40mail.gmail.com</a>.<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%2Bunsubscribe@isocpp.org" target=3D=
"_blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/20180721091811.GB22899%40noemi" rel=
=3D"noreferrer" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/=
msgid/std-proposals/20180721091811.GB22899%40noemi</a>.<br>
</blockquote></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHd4fTG0yj6JwghCKs5Vf6f%2BVJXt=
0STankf6Wha28PrA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbHd4fTG0=
yj6JwghCKs5Vf6f%2BVJXt0STankf6Wha28PrA%40mail.gmail.com</a>.<br />

--0000000000000a9d8905717f8ac0--

.


Author: Barry Revzin <barry.revzin@gmail.com>
Date: Sun, 22 Jul 2018 04:40:59 -0700 (PDT)
Raw View
------=_Part_7373_323867169.1532259659278
Content-Type: multipart/alternative;
 boundary="----=_Part_7374_1250707433.1532259659279"

------=_Part_7374_1250707433.1532259659279
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable



On Friday, July 20, 2018 at 3:50:40 PM UTC-5, olivi...@yahoo.com wrote:
>
>
>
> Le vendredi 20 juillet 2018 22:22:41 UTC+2, Nicolas Lesser a =C3=A9crit :
>>
>> front() and back() are still appropriate names as they really get the=20
>> front and back elements respectively.
>>
>
> I don't think front() and back() are appropriate because they are too=20
> vague : after all, why would front() be the min element and not the max=
=20
> element?
>
> min() and max() avoid this ambiguity.
>

There is nothing vague about front() and back(), in the same way that there=
=20
is nothing vague about begin() and end(). We already have many containers=
=20
in the standard library that have this functionality named this way. It=20
would be the expected name for this functionality, and it's a good name,=20
that clearly describes what it would be doing.=20

The question is "Given that these functions should exist, what should these=
=20
functions be named?"
The question is "Is writing *c.begin() and *c.rbegin() sufficiently bad as=
=20
to merit new operations that do that?'=20

And consistency with existing containers would be one argument in support=
=20
of that. After all, I never write *v.begin() when I mean v.front().

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/67425ebc-5a67-41a0-be5a-d1821aa208e0%40isocpp.or=
g.

------=_Part_7374_1250707433.1532259659279
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Friday, July 20, 2018 at 3:50:40 PM UTC-5, oliv=
i...@yahoo.com wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;m=
argin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=
=3D"ltr"><div><br></div><div><br>Le vendredi 20 juillet 2018 22:22:41 UTC+2=
, Nicolas Lesser a =C3=A9crit=C2=A0:<blockquote class=3D"gmail_quote" style=
=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"=
><div dir=3D"auto"><div>front() and back() are still appropriate names as t=
hey really get the front and back elements respectively.</div></div></block=
quote><div><br></div><div>I don&#39;t think front() and back() are appropri=
ate because they are too vague : after all, why would front() be the min el=
ement and not the max element?</div><div><br></div><div>min() and max() avo=
id this ambiguity.</div></div></div></blockquote><div><br></div><div>There =
is nothing vague about front() and back(), in the same way that there is no=
thing vague about begin() and end(). We already have many containers in the=
 standard library that have this functionality named this way. It would be =
the expected name for this functionality, and it&#39;s a good name, that cl=
early describes what it would be doing.=C2=A0</div><div><br></div><div>The =
question is &quot;Given that these functions should exist, what should thes=
e functions be named?&quot;</div><div>The question is &quot;Is writing *c.b=
egin() and *c.rbegin() sufficiently bad as to merit new operations that do =
that?&#39;=C2=A0</div><div><br></div><div>And consistency with existing con=
tainers would be one argument in support of that. After all, I never write =
*v.begin() when I mean v.front().</div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/67425ebc-5a67-41a0-be5a-d1821aa208e0%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/67425ebc-5a67-41a0-be5a-d1821aa208e0=
%40isocpp.org</a>.<br />

------=_Part_7374_1250707433.1532259659279--

------=_Part_7373_323867169.1532259659278--

.


Author: Barry Revzin <barry.revzin@gmail.com>
Date: Sun, 22 Jul 2018 04:41:40 -0700 (PDT)
Raw View
------=_Part_7622_1843758984.1532259700977
Content-Type: multipart/alternative;
 boundary="----=_Part_7623_1921077027.1532259700977"

------=_Part_7623_1921077027.1532259700977
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable



On Sunday, July 22, 2018 at 6:40:59 AM UTC-5, Barry Revzin wrote:
>
>
>
> On Friday, July 20, 2018 at 3:50:40 PM UTC-5, olivi...@yahoo.com wrote:
>>
>>
>>
>> Le vendredi 20 juillet 2018 22:22:41 UTC+2, Nicolas Lesser a =C3=A9crit =
:
>>>
>>> front() and back() are still appropriate names as they really get the=
=20
>>> front and back elements respectively.
>>>
>>
>> I don't think front() and back() are appropriate because they are too=20
>> vague : after all, why would front() be the min element and not the max=
=20
>> element?
>>
>> min() and max() avoid this ambiguity.
>>
>
> There is nothing vague about front() and back(), in the same way that=20
> there is nothing vague about begin() and end(). We already have many=20
> containers in the standard library that have this functionality named thi=
s=20
> way. It would be the expected name for this functionality, and it's a goo=
d=20
> name, that clearly describes what it would be doing.=20
>
> The question is "Given that these functions should exist, what should=20
> these functions be named?"
> The question is "Is writing *c.begin() and *c.rbegin() sufficiently bad a=
s=20
> to merit new operations that do that?'=20
>
> And consistency with existing containers would be one argument in support=
=20
> of that. After all, I never write *v.begin() when I mean v.front().
>


Blegh, the question is *not* "Given that ..."

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/a11aa307-789c-4601-a6ef-e3e4582ace97%40isocpp.or=
g.

------=_Part_7623_1921077027.1532259700977
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Sunday, July 22, 2018 at 6:40:59 AM UTC-5, Barr=
y Revzin wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-=
left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr=
"><br><br>On Friday, July 20, 2018 at 3:50:40 PM UTC-5, <a>olivi...@yahoo.c=
om</a> wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-lef=
t:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>=
<br></div><div><br>Le vendredi 20 juillet 2018 22:22:41 UTC+2, Nicolas Less=
er a =C3=A9crit=C2=A0:<blockquote class=3D"gmail_quote" style=3D"margin:0;m=
argin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"a=
uto"><div>front() and back() are still appropriate names as they really get=
 the front and back elements respectively.</div></div></blockquote><div><br=
></div><div>I don&#39;t think front() and back() are appropriate because th=
ey are too vague : after all, why would front() be the min element and not =
the max element?</div><div><br></div><div>min() and max() avoid this ambigu=
ity.</div></div></div></blockquote><div><br></div><div>There is nothing vag=
ue about front() and back(), in the same way that there is nothing vague ab=
out begin() and end(). We already have many containers in the standard libr=
ary that have this functionality named this way. It would be the expected n=
ame for this functionality, and it&#39;s a good name, that clearly describe=
s what it would be doing.=C2=A0</div><div><br></div><div>The question is &q=
uot;Given that these functions should exist, what should these functions be=
 named?&quot;</div><div>The question is &quot;Is writing *c.begin() and *c.=
rbegin() sufficiently bad as to merit new operations that do that?&#39;=C2=
=A0</div><div><br></div><div>And consistency with existing containers would=
 be one argument in support of that. After all, I never write *v.begin() wh=
en I mean v.front().</div></div></blockquote><div><br></div><div><br></div>=
<div>Blegh, the question is <b><i><u>not</u></i></b>=C2=A0&quot;Given that =
....&quot;</div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/a11aa307-789c-4601-a6ef-e3e4582ace97%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/a11aa307-789c-4601-a6ef-e3e4582ace97=
%40isocpp.org</a>.<br />

------=_Part_7623_1921077027.1532259700977--

------=_Part_7622_1843758984.1532259700977--

.


Author: Vishal Oza <vickoza@gmail.com>
Date: Sun, 22 Jul 2018 07:47:26 -0700 (PDT)
Raw View
------=_Part_7660_525777077.1532270846225
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

I would say even min and max are unclear. Remember that both set and map ar=
e ordered but the order can be specified by the user. {1, 2, 3, 4, 5, 6, 7,=
 8, 9, 10}, {10, 9, 8, 7, 6, 5, 4, 3, 2,I  1}, and {1, 4, 7, 10, 2, 5, 8, 3=
, 6, 9} can be sets of ints and in all cases the min element value is 1 and=
 the max value is 10, but the first and last value in all cases are not 1 a=
nd 10. I also think the front and back should return either a copy of first=
 and last values or a const reference to these values.

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/5fb0ec52-9877-444a-902e-daa4f32d397e%40isocpp.or=
g.

------=_Part_7660_525777077.1532270846225--

.


Author: Nicolas Lesser <blitzrakete@gmail.com>
Date: Sun, 22 Jul 2018 17:07:30 +0200
Raw View
--000000000000ac75f6057197ec53
Content-Type: text/plain; charset="UTF-8"

It's not a good idea to change the signature of front() for a single
container. That would only cause confusion. Also why don't you want to get
a reference to the first/last element?
On Sun, Jul 22, 2018 at 4:47 PM Vishal Oza <vickoza@gmail.com> wrote:

> I would say even min and max are unclear. Remember that both set and map
> are ordered but the order can be specified by the user. {1, 2, 3, 4, 5, 6,
> 7, 8, 9, 10}, {10, 9, 8, 7, 6, 5, 4, 3, 2,I  1}, and {1, 4, 7, 10, 2, 5, 8,
> 3, 6, 9} can be sets of ints and in all cases the min element value is 1
> and the max value is 10, but the first and last value in all cases are not
> 1 and 10. I also think the front and back should return either a copy of
> first and last values or a const reference to these values.
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5fb0ec52-9877-444a-902e-daa4f32d397e%40isocpp.org
> .
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALmDwq3mR4Ym%2B_-CM0FCchBgNSNsiJrs3%3D7tJWZkK8vB0%3D4cHA%40mail.gmail.com.

--000000000000ac75f6057197ec53
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">It&#39;s not a good idea to change the signature of front(=
) for a single container. That would only cause confusion. Also why don&#39=
;t you want to get a reference to the first/last element?<br><div class=3D"=
gmail_quote"><div dir=3D"ltr">On Sun, Jul 22, 2018 at 4:47 PM Vishal Oza &l=
t;<a href=3D"mailto:vickoza@gmail.com">vickoza@gmail.com</a>&gt; wrote:<br>=
</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l=
eft:1px #ccc solid;padding-left:1ex">I would say even min and max are uncle=
ar. Remember that both set and map are ordered but the order can be specifi=
ed by the user. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {10, 9, 8, 7, 6, 5, 4, 3, =
2,I=C2=A0 1}, and {1, 4, 7, 10, 2, 5, 8, 3, 6, 9} can be sets of ints and i=
n all cases the min element value is 1 and the max value is 10, but the fir=
st and last value in all cases are not 1 and 10. I also think the front and=
 back should return either a copy of first and last values or a const refer=
ence to these values.<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%2Bunsubscribe@isocpp.org" target=3D=
"_blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/5fb0ec52-9877-444a-902e-daa4f32d397e%=
40isocpp.org" rel=3D"noreferrer" target=3D"_blank">https://groups.google.co=
m/a/isocpp.org/d/msgid/std-proposals/5fb0ec52-9877-444a-902e-daa4f32d397e%4=
0isocpp.org</a>.<br>
</blockquote></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CALmDwq3mR4Ym%2B_-CM0FCchBgNSNsiJrs3%=
3D7tJWZkK8vB0%3D4cHA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALmDwq3mR4=
Ym%2B_-CM0FCchBgNSNsiJrs3%3D7tJWZkK8vB0%3D4cHA%40mail.gmail.com</a>.<br />

--000000000000ac75f6057197ec53--

.


Author: Jake Arkinstall <jake.arkinstall@gmail.com>
Date: Sun, 22 Jul 2018 16:41:08 +0100
Raw View
--0000000000002acb3c0571985c5d
Content-Type: text/plain; charset="UTF-8"

Yeah, min and max on a map referring to the keys reminds me of python's
approach to dicts, where many list-like functions applied to dicts are
applied to the keys. It's an approach that makes sense given the language,
but it would be out of place in C++. The main use of it in python is
writing inefficient code with less effort, because it almost always ends up
with a lookup at the end.

I've nothing against adding back and front considering we also have it for
vectors etc, I just struggle to see a good reason FOR it. I'd actually much
rather we get rid of it for vectors etc - and with it a nice chunk of
undefined behaviour - but I'd also much rather pizza to be as healthy as
salad. We can't always get what we want.

So, instead, I'd propose four new functions added to std - std::front(T&
t), std::back(T& t), std::cfront(const T& t), std::cback(const T& t), which
return the obvious but wrapped in a std::optional - an optional reference
to the first/last element iff the corresponding iterator is not the end
iterator. Maybe also versions which returns references without optional,
allowing UB for those who insist that they know for sure that it isn't a
possible branch in the first place.

On Sun, 22 Jul 2018, 15:47 Vishal Oza, <vickoza@gmail.com> wrote:

> I would say even min and max are unclear. Remember that both set and map
> are ordered but the order can be specified by the user. {1, 2, 3, 4, 5, 6,
> 7, 8, 9, 10}, {10, 9, 8, 7, 6, 5, 4, 3, 2,I  1}, and {1, 4, 7, 10, 2, 5, 8,
> 3, 6, 9} can be sets of ints and in all cases the min element value is 1
> and the max value is 10, but the first and last value in all cases are not
> 1 and 10. I also think the front and back should return either a copy of
> first and last values or a const reference to these values.
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5fb0ec52-9877-444a-902e-daa4f32d397e%40isocpp.org
> .
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAC%2B0CCN6bSsd7Saivx6zu65%3D0qVUkLAGm14%2B%2BKwdxCAtUZK-Tw%40mail.gmail.com.

--0000000000002acb3c0571985c5d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto">Yeah, min and max on a map referring to the keys reminds =
me of python&#39;s approach to dicts, where many list-like functions applie=
d to dicts are applied to the keys. It&#39;s an approach that makes sense g=
iven the language, but it would be out of place in C++. The main use of it =
in python is writing inefficient code with less effort, because it almost a=
lways ends up with a lookup at the end.<div dir=3D"auto"><br></div><div dir=
=3D"auto">I&#39;ve nothing against adding back and front considering we als=
o have it for vectors etc, I just struggle to see a good reason FOR it. I&#=
39;d actually much rather we get rid of it for vectors etc - and with it a =
nice chunk of undefined behaviour - but I&#39;d also much rather pizza to b=
e as healthy as salad. We can&#39;t always get what we want.</div><div dir=
=3D"auto"><br></div><div dir=3D"auto">So, instead, I&#39;d propose four new=
 functions added to std - std::front(T&amp; t), std::back(T&amp; t), std::c=
front(const T&amp; t), std::cback(const T&amp; t), which return the obvious=
 but wrapped in a std::optional - an optional reference to the first/last e=
lement iff the corresponding iterator is not the end iterator. Maybe also v=
ersions which returns references without optional, allowing UB for those wh=
o insist that they know for sure that it isn&#39;t a possible branch in the=
 first place.</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr">On=
 Sun, 22 Jul 2018, 15:47 Vishal Oza, &lt;<a href=3D"mailto:vickoza@gmail.co=
m" target=3D"_blank" rel=3D"noreferrer">vickoza@gmail.com</a>&gt; wrote:<br=
></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-=
left:1px #ccc solid;padding-left:1ex">I would say even min and max are uncl=
ear. Remember that both set and map are ordered but the order can be specif=
ied by the user. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {10, 9, 8, 7, 6, 5, 4, 3,=
 2,I=C2=A0 1}, and {1, 4, 7, 10, 2, 5, 8, 3, 6, 9} can be sets of ints and =
in all cases the min element value is 1 and the max value is 10, but the fi=
rst and last value in all cases are not 1 and 10. I also think the front an=
d back should return either a copy of first and last values or a const refe=
rence to these values.<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%2Bunsubscribe@isocpp.org" rel=3D"no=
referrer noreferrer" target=3D"_blank">std-proposals+unsubscribe@isocpp.org=
</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" rel=3D"noreferrer noreferrer" target=3D"_blank">std-proposals@isocpp.=
org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/5fb0ec52-9877-444a-902e-daa4f32d397e%=
40isocpp.org" rel=3D"noreferrer noreferrer noreferrer" target=3D"_blank">ht=
tps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5fb0ec52-9877-44=
4a-902e-daa4f32d397e%40isocpp.org</a>.<br>
</blockquote></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAC%2B0CCN6bSsd7Saivx6zu65%3D0qVUkLAG=
m14%2B%2BKwdxCAtUZK-Tw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoo=
ter">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAC%2B0CC=
N6bSsd7Saivx6zu65%3D0qVUkLAGm14%2B%2BKwdxCAtUZK-Tw%40mail.gmail.com</a>.<br=
 />

--0000000000002acb3c0571985c5d--

.


Author: Vishal Oza <vickoza@gmail.com>
Date: Sun, 22 Jul 2018 09:14:06 -0700 (PDT)
Raw View
------=_Part_7699_388327729.1532276046285
Content-Type: multipart/alternative;
 boundary="----=_Part_7700_1972257463.1532276046286"

------=_Part_7700_1972257463.1532276046286
Content-Type: text/plain; charset="UTF-8"

I think, for at least for sets, std::front(T& t) and std::end(T& t) should
be invalid. I also think it might be good to remove begin() and end() set
and maybe maps. I have been thinking that the return type for set's front
and back should be a copy of T if T has a copy constructor or a const
reference if it does not have one. I think we should never be allow to
alter a Key's value as it determine the position in the map

On Sunday, July 22, 2018 at 10:41:20 AM UTC-5, Jake Arkinstall wrote:
>
> Yeah, min and max on a map referring to the keys reminds me of python's
> approach to dicts, where many list-like functions applied to dicts are
> applied to the keys. It's an approach that makes sense given the language,
> but it would be out of place in C++. The main use of it in python is
> writing inefficient code with less effort, because it almost always ends up
> with a lookup at the end.
>
> I've nothing against adding back and front considering we also have it for
> vectors etc, I just struggle to see a good reason FOR it. I'd actually much
> rather we get rid of it for vectors etc - and with it a nice chunk of
> undefined behaviour - but I'd also much rather pizza to be as healthy as
> salad. We can't always get what we want.
>
> So, instead, I'd propose four new functions added to std - std::front(T&
> t), std::back(T& t), std::cfront(const T& t), std::cback(const T& t), which
> return the obvious but wrapped in a std::optional - an optional reference
> to the first/last element iff the corresponding iterator is not the end
> iterator. Maybe also versions which returns references without optional,
> allowing UB for those who insist that they know for sure that it isn't a
> possible branch in the first place.
>
> On Sun, 22 Jul 2018, 15:47 Vishal Oza, <vic...@gmail.com <javascript:>>
> wrote:
>
>> I would say even min and max are unclear. Remember that both set and map
>> are ordered but the order can be specified by the user. {1, 2, 3, 4, 5, 6,
>> 7, 8, 9, 10}, {10, 9, 8, 7, 6, 5, 4, 3, 2,I  1}, and {1, 4, 7, 10, 2, 5, 8,
>> 3, 6, 9} can be sets of ints and in all cases the min element value is 1
>> and the max value is 10, but the first and last value in all cases are not
>> 1 and 10. I also think the front and back should return either a copy of
>> first and last values or a const reference to these values.
>>
>> --
>> 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-proposal...@isocpp.org <javascript:>.
>> To post to this group, send email to std-pr...@isocpp.org <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5fb0ec52-9877-444a-902e-daa4f32d397e%40isocpp.org
>> .
>>
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0b403cf7-a7e4-4fe1-a96d-015833e05247%40isocpp.org.

------=_Part_7700_1972257463.1532276046286
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I think, for at least for sets, std::front(T&amp; t) and s=
td::end(T&amp; t) should be invalid. I also think it might be good to remov=
e begin() and end() set and maybe maps. I have been thinking that the retur=
n type for set&#39;s front and back should be a copy of T if T has a copy c=
onstructor or a const reference if it does not have one. I think we should =
never be allow to alter a Key&#39;s value as it determine the position in t=
he map<br><br>On Sunday, July 22, 2018 at 10:41:20 AM UTC-5, Jake Arkinstal=
l wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0=
..8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"auto">Yeah=
, min and max on a map referring to the keys reminds me of python&#39;s app=
roach to dicts, where many list-like functions applied to dicts are applied=
 to the keys. It&#39;s an approach that makes sense given the language, but=
 it would be out of place in C++. The main use of it in python is writing i=
nefficient code with less effort, because it almost always ends up with a l=
ookup at the end.<div dir=3D"auto"><br></div><div dir=3D"auto">I&#39;ve not=
hing against adding back and front considering we also have it for vectors =
etc, I just struggle to see a good reason FOR it. I&#39;d actually much rat=
her we get rid of it for vectors etc - and with it a nice chunk of undefine=
d behaviour - but I&#39;d also much rather pizza to be as healthy as salad.=
 We can&#39;t always get what we want.</div><div dir=3D"auto"><br></div><di=
v dir=3D"auto">So, instead, I&#39;d propose four new functions added to std=
 - std::front(T&amp; t), std::back(T&amp; t), std::cfront(const T&amp; t), =
std::cback(const T&amp; t), which return the obvious but wrapped in a std::=
optional - an optional reference to the first/last element iff the correspo=
nding iterator is not the end iterator. Maybe also versions which returns r=
eferences without optional, allowing UB for those who insist that they know=
 for sure that it isn&#39;t a possible branch in the first place.</div></di=
v><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Sun, 22 Jul 2018, 15:4=
7 Vishal Oza, &lt;<a href=3D"javascript:" rel=3D"nofollow" target=3D"_blank=
" gdf-obfuscated-mailto=3D"nHBRZLgeDAAJ" onmousedown=3D"this.href=3D&#39;ja=
vascript:&#39;;return true;" onclick=3D"this.href=3D&#39;javascript:&#39;;r=
eturn true;">vic...@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"=
gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-=
left:1ex">I would say even min and max are unclear. Remember that both set =
and map are ordered but the order can be specified by the user. {1, 2, 3, 4=
, 5, 6, 7, 8, 9, 10}, {10, 9, 8, 7, 6, 5, 4, 3, 2,I=C2=A0 1}, and {1, 4, 7,=
 10, 2, 5, 8, 3, 6, 9} can be sets of ints and in all cases the min element=
 value is 1 and the max value is 10, but the first and last value in all ca=
ses are not 1 and 10. I also think the front and back should return either =
a copy of first and last values or a const reference to these values.<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"javascript:" rel=3D"nofollow" target=3D"_blank" gdf-obfu=
scated-mailto=3D"nHBRZLgeDAAJ" onmousedown=3D"this.href=3D&#39;javascript:&=
#39;;return true;" onclick=3D"this.href=3D&#39;javascript:&#39;;return true=
;">std-proposal...@<wbr>isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"javascript:" rel=3D"nofollo=
w" target=3D"_blank" gdf-obfuscated-mailto=3D"nHBRZLgeDAAJ" onmousedown=3D"=
this.href=3D&#39;javascript:&#39;;return true;" onclick=3D"this.href=3D&#39=
;javascript:&#39;;return true;">std-pr...@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/5fb0ec52-9877-444a-902e-daa4f32d397e%=
40isocpp.org" rel=3D"nofollow" target=3D"_blank" onmousedown=3D"this.href=
=3D&#39;https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5fb0ec=
52-9877-444a-902e-daa4f32d397e%40isocpp.org&#39;;return true;" onclick=3D"t=
his.href=3D&#39;https://groups.google.com/a/isocpp.org/d/msgid/std-proposal=
s/5fb0ec52-9877-444a-902e-daa4f32d397e%40isocpp.org&#39;;return true;">http=
s://groups.google.com/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/5fb0ec52=
-9877-444a-<wbr>902e-daa4f32d397e%40isocpp.org</a><wbr>.<br>
</blockquote></div>
</blockquote></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/0b403cf7-a7e4-4fe1-a96d-015833e05247%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/0b403cf7-a7e4-4fe1-a96d-015833e05247=
%40isocpp.org</a>.<br />

------=_Part_7700_1972257463.1532276046286--

------=_Part_7699_388327729.1532276046285--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sun, 22 Jul 2018 09:35:51 -0700 (PDT)
Raw View
------=_Part_7666_1207509303.1532277351155
Content-Type: multipart/alternative;
 boundary="----=_Part_7667_1594135501.1532277351155"

------=_Part_7667_1594135501.1532277351155
Content-Type: text/plain; charset="UTF-8"

On Sunday, July 22, 2018 at 12:14:06 PM UTC-4, Vishal Oza wrote:
>
> I think, for at least for sets, std::front(T& t) and std::end(T& t) should
> be invalid.
>

Well, that's not what the signatures of the functions would be, but you're
right in the sense that `front/back` should return `set::const_reference`s,
just like its iterators.

I also think it might be good to remove begin() and end() set and maybe
> maps.
>

That would mean that they're not ranges anymore. Why would we want to
remove them?


> I have been thinking that the return type for set's front and back should
> be a copy of T if T has a copy constructor or a const reference if it does
> not have one. I think we should never be allow to alter a Key's value as it
> determine the position in the map
>

Nobody's asking for the ability to manipulate the value of a Key in an
ordered container. The `value_type` for `map` is a `pair<const Key,
Value>`; `front/back` would not change this.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0cbf070e-02f8-416c-9ecb-6a22803ab80d%40isocpp.org.

------=_Part_7667_1594135501.1532277351155
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Sunday, July 22, 2018 at 12:14:06 PM UTC-4, Vishal Oza =
wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8=
ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">I think=
, for at least for sets, std::front(T&amp; t) and std::end(T&amp; t) should=
 be invalid.</div></blockquote><div><br></div>Well, that&#39;s not what the=
 signatures of the functions would be, but you&#39;re right in the sense th=
at `front/back` should return `set::const_reference`s, just like its iterat=
ors.<br><div></div><div><br></div><blockquote class=3D"gmail_quote" style=
=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: =
1ex;"><div dir=3D"ltr">I also think it might be good to remove begin() and =
end() set and maybe maps.</div></blockquote><div><br></div><div>That would =
mean that they&#39;re not ranges anymore. Why would we want to remove them?=
</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0=
;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div di=
r=3D"ltr">I have been thinking that the return type for set&#39;s front and=
 back should be a copy of T if T has a copy constructor or a const referenc=
e if it does not have one. I think we should never be allow to alter a Key&=
#39;s value as it determine the position in the map<br></div></blockquote><=
div><br></div><div>Nobody&#39;s asking for the ability to manipulate the va=
lue of a Key in an ordered container. The `value_type` for `map` is a `pair=
&lt;const Key, Value&gt;`; `front/back` would not change this.<br></div></d=
iv>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/0cbf070e-02f8-416c-9ecb-6a22803ab80d%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/0cbf070e-02f8-416c-9ecb-6a22803ab80d=
%40isocpp.org</a>.<br />

------=_Part_7667_1594135501.1532277351155--

------=_Part_7666_1207509303.1532277351155--

.


Author: olafvdspek@gmail.com
Date: Mon, 23 Jul 2018 00:48:10 -0700 (PDT)
Raw View
------=_Part_7968_1492979015.1532332090533
Content-Type: multipart/alternative;
 boundary="----=_Part_7969_166698264.1532332090533"

------=_Part_7969_166698264.1532332090533
Content-Type: text/plain; charset="UTF-8"



Op vrijdag 20 juli 2018 06:00:15 UTC+2 schreef Nicol Bolas:
>
> It also took until C++20 before we could ask if a map contains an element
> *without* fetching the iterator.
>

Hasn't count() been available since forever?

What did we get in C++20? contains()?

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/745e7811-401e-406b-9170-d16d8b9cddb1%40isocpp.org.

------=_Part_7969_166698264.1532332090533
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>Op vrijdag 20 juli 2018 06:00:15 UTC+2 schreef Nic=
ol Bolas:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: =
0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div=
>It also took until C++20 before we could ask if a map contains an element =
<i>without</i> fetching the iterator.</div></div></blockquote><div><br></di=
v><div>Hasn&#39;t count() been available since forever?</div><div><br></div=
><div>What did we get in C++20? contains()?=C2=A0</div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/745e7811-401e-406b-9170-d16d8b9cddb1%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/745e7811-401e-406b-9170-d16d8b9cddb1=
%40isocpp.org</a>.<br />

------=_Part_7969_166698264.1532332090533--

------=_Part_7968_1492979015.1532332090533--

.