Sla*_*zer 5 c++ iterator vector
我有两个具有相同功能的代码片段,但是一个可以编译,另一个不能。为什么?
这个编译。
vector<int>::iterator it;
if ((it=find(v.begin(),v.end(),2))!=v.end()){
}
Run Code Online (Sandbox Code Playgroud)
这个没有。
if ((vector<int>::iterator it=find(v.begin(),v.end(),2))!=v.end()){
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误。
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’
Run Code Online (Sandbox Code Playgroud)
PS 可以随意编辑标题 - 我找不到任何描述性的内容。
如果 的全部内容都以变量声明开头(即,如果它只是变量声明),则只能在if'内定义变量。()()
您要做的就是声明一个变量,然后对其进行测试。这是不允许的。
您可以回退到两行版本,也可以编写一个find使用boost::optionalor的基于容器的版本std::tr2::optional,如下所示:
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};
}
Run Code Online (Sandbox Code Playgroud)
iterator上面的代码没有返回 an ,而是返回一个可选的iterator,当在boolean 上下文中计算时,false如果没有找到该项目。
您现在可以输入:
if( auto op_it = my_find( v, 2 ) ) {
auto it = *op_it; // optional, but saves on `*` elsewhere
// code
}
Run Code Online (Sandbox Code Playgroud)
并大致得到你想要的。
optional在 C++14中可用boost,在 Maybe 中std::tr2,并且可能在std::C++14 中。和略有不同boost。std