-
November 10th, 2022, 07:56 PM
#1
Do not understand conditional in for loop
Hello, I was hoping someone could help me understand the conditional in this for loop:
Code:
for (auto i = maps.begin(); i != maps.end(); i++)
{
if (&((*i).second) == this)
{
assert(!getManager().hasActiveMap());
ret = ret && getManager().setActiveMap((*i).first);
}
}
what is happening here:
Code:
if (&((*i).second) == this)
is this just asking for the value located at "second" of the index i? If so, could this not just be:
?
-
November 11th, 2022, 03:12 AM
#2
Re: Do not understand conditional in for loop
Originally Posted by Circuits
what is happening here:
Code:
if (&((*i).second) == this)
is this just asking for the value located at "second" of the index i? If so, could this not just be:
?
No.
just because is the same asBesides it is in the most cases easier to use construction like
Code:
for (auto& it : maps)
rather than
Code:
for (auto i = maps.begin(); i != maps.end(); i++)
Victor Nijegorodov
-
November 11th, 2022, 04:19 AM
#3
Re: Do not understand conditional in for loop
You need to consider precedence. . has the highest level and then & (as address). and * (as de-reference). So to have .second you need (*I).
You could have:
Code:
if (&i->second == this)
as -> has a higher precedence than &
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 11th, 2022, 12:17 PM
#4
Re: Do not understand conditional in for loop
Would this be equivalent then?
Code:
for (auto&& i : maps)
{
if (i.second == this)
{
assert(!getManager().hasActiveMap());
ret = ret && getManager().setActiveMap((*i).first);
}
}
-
November 11th, 2022, 12:37 PM
#5
Re: Do not understand conditional in for loop
Originally Posted by Circuits
Would this be equivalent then?
Code:
for (auto&& i : maps)
{
if (i.second == this)
{
assert(!getManager().hasActiveMap());
ret = ret && getManager().setActiveMap((*i).first);
}
}
Does it compile???
Victor Nijegorodov
-
November 11th, 2022, 12:41 PM
#6
Re: Do not understand conditional in for loop
Oh, heh no, no it doesn't
Code:
for (auto& i : maps)
{
if (&i.second == this)
{
assert(!getManager().hasActiveMap());
ret = ret && getManager().setActiveMap((i).first);
}
}
-
November 14th, 2022, 09:33 AM
#7
Re: Do not understand conditional in for loop
Originally Posted by Circuits
Oh, heh no, no it doesn't
It doesn't compile? And what the compiler says?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|