Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: A small quiz (1)

62 views
Skip to first unread message

Paavo Helde

unread,
Sep 7, 2015, 5:18:35 PM9/7/15
to
r...@zedat.fu-berlin.de (Stefan Ram) wrote in news:quiz-20150907203920
@ram.dialup.fu-berlin.de:

> The following code is legal, but it still has an unnecessary
> run-time inefficiency in it, that can be cured by a (small)
> change in the source code:
>
> /* assume necessary #includes */
>
>::std::unordered_map< ::std::string, int >map;
> /* here some values might be put into the map */
>
> for( const ::std::pair< ::std::string, int > & pair : map )
> /* assume a statement here that might use the pair */

The correct way is to use auto and let it figure out the needed type:

for( const auto& pair : map )

According to the docs the type would be std::pair<const std::string,int>,
but why should I waste my time on it and hope to get it right every time?
This example is just an example of fragile code.

Cheers
Paavo

Luca Risolia

unread,
Sep 7, 2015, 5:54:48 PM9/7/15
to
Il 07/09/2015 21:51, Stefan Ram ha scritto:
> The following code is legal, but it still has an unnecessary
> run-time inefficiency in it, that can be cured by a (small)
> change in the source code:

> for( const ::std::pair< ::std::string, int > & pair : map )
> /* assume a statement here that might use the pair */

I know from what book your quizzes come from...;)


rsteele

unread,
Sep 7, 2015, 9:40:02 PM9/7/15
to
I agree with Paavo. Use const auto& for a range-based for-loop.

rsteele

unread,
Sep 7, 2015, 9:42:45 PM9/7/15
to
I agree with Paavo. Use const auto& p within a range-based for-loop.

auxo

unread,
Sep 8, 2015, 9:01:23 AM9/8/15
to Stefan Ram
Note: talking fully off the top of my head -- untested, not even sure if
it's legal, but I'm taking a shot.

> run-time inefficiency in it, that can be cured by a (small)
> change in the source code:

> for( const ::std::pair< ::std::string, int > & pair : map )
> /* assume a statement here that might use the pair */

Would `const ::std::pair<const ::std::string&, int>&` make sense? I.e.
making the element inside the pair a constant reference too (`int`
doesn't matter in this case because it's a builtin type) because why
make a copy of the string if all you're doing is looking at the pair.

Something like that?

Assuming the above is correct, would `const ::std::pair<::std::string&,
int>&` make sense/be valid (i.e. a normal reference to the string inside
the pair but a constant reference to the whole pair)? I assume it wouldn't?

auxo

auxo

unread,
Sep 8, 2015, 9:01:23 AM9/8/15
to Stefan Ram

thullu

unread,
Sep 22, 2015, 4:01:18 AM9/22/15
to
> Yes, the correct answers were given in this thread,

Was auxo correct?

> and it's correct that I got both ideas from the same book.

Which book?

On 09/22/2015 12:31 AM, Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> The following code is legal, but it still has an unnecessary
>> run-time inefficiency in it, that can be cured by a (small)
>> change in the source code:
>> /* assume necessary #includes */
>> ::std::unordered_map< ::std::string, int >map;
>> /* here some values might be put into the map */
>> for( const ::std::pair< ::std::string, int > & pair : map )
>> /* assume a statement here that might use the pair */
>
> Yes, the correct answers were given in this thread,
> and it's correct that I got both ideas from the same book.
>
0 new messages