2021-08-29

How to define variable and compare value inside if statement?

I have a code snippet like this:

if((std::vector<int>::iterator iter = std::find(v.begin(), v.end(), i)) != v.end()){
    ....
}

But the compiler complaints on this statement. However, changing my code into

std::vector<int>::iterator iter;
if((iter = std::find(v.begin(), v.end(), i)) != v.end()){
    ....
}

fixes the issue.

So, I was wondering why the first version does not work and what is the return value of statement std::vector<int>::iterator iter = std::find(v.begin(), v.end(), i)?



from Recent Questions - Stack Overflow https://ift.tt/3mDp5Tv
https://ift.tt/eA8V8J

No comments:

Post a Comment