Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vector<int>::iterator inside if statement fails to compile

I have two code snippets with the same functionality, however one compiles and the other does not. Why?

This one compiles.

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

This one does not.

if ((vector<int>::iterator it=find(v.begin(),v.end(),2))!=v.end()){
}

This is the error I get.

main.cpp: In function ‘int main()’:
main.cpp:32:28: error: expected primary-expression before ‘it’
main.cpp:32:28: error: expected ‘)’ before ‘it’
main.cpp:44:5: error: expected ‘)’ before ‘return’

P.S. Be free to edit the title - I could not find anything descriptive.

like image 396
Slazer Avatar asked Nov 02 '22 17:11

Slazer


1 Answers

You can only define a variable inside an if's () if the entire contents of the () starts with the variable declaration (ie, if it is nothing but a variable declaration).

What you are trying to do is both declare a variable, and then do a test on it. That isn't allowed.

You can either fall back to the two line version, or you can write up a container-based find that uses boost::optional or std::tr2::optional as follows:

namespace aux {
  using std::begin; using std::end;
  template<typename C> auto adl_begin( C&& c )->decltype( begin(std::forward<C>(c)) )
  { return begin(std::forward<C>(c)); }
  template<typename C> auto adl_end( C&& c )->decltype( end(std::forward<C>(c)) )
  { return end(std::forward<C>(c)); }
}
using aux::adl_begin; using aux::adl_end;

template<typename C, typename U>
optional< decltype( typename std::decay<*adl_begin( std::declval<C&>() )>::type ) >
my_find( C&& c, U&& u ) {
  auto it = std::find( adl_begin(c), adl_end(c), std::forward<U>(u) );
  if (it == adl_end(c))
    return {none_t};
  else
    return {it};
}

Instead of returning an iterator, the above returns an optional iterator, which when evaluated in a boolean context is false if the item isn't found.

You can now type:

if( auto op_it = my_find( v, 2 ) ) {
  auto it = *op_it; // optional, but saves on `*` elsewhere
  // code
}

and get roughly what you want.

optional is available in boost, in maybe std::tr2, and probably in std:: in C++14. The boost and std ones are slightly different.

like image 149
Yakk - Adam Nevraumont Avatar answered Nov 15 '22 04:11

Yakk - Adam Nevraumont