作者:手机用户2502937805 | 来源:互联网 | 2022-12-21 17:25
1> Slava suppor..:
使用迭代器而作为奖励你根本不需要写一个循环:
auto it = std::find( vector.begin(), vector.end(), 1 );
if( it == vector.end() ) {
... // not found
}
如果您愿意,可以使用带有显式循环的迭代器.
auto it = vector.begin();
for( ; it != vector.end(); ++it ) {
if( *it == 1 ) break;
}
if( it == vector.end() ) {
... // not found
}
auto index = std::distance( vector.begin(), it );