菜鸟笔记
提升您的技术认知

c++中vector find使用

c++中vector find使用

不同于map(map有find方法),vector本身是没有find这一方法,其find是依靠algorithm来实现的。
话不多说,上代码:

#include <vector>
#include <algorithm>
#include <iostream>

int main( )
{
    using namespace std;

    vector<int> L;
    L.push_back( 1 );
    L.push_back( 2 );
    L.push_back( 3 );
    L.push_back( 4 );
    L.push_back( 5 );
    vector<int>::iterator result = find( L.begin( ), L.end( ), 3 ); //查找3
    if ( result == L.end( ) ) //没找到
        cout << "No" << endl;
    else //找到
        cout << "Yes" << endl;
}

记着要包含algorithm这一头文件,其定义了find这一函数。
资料参考:https://www.coonote.com/cplusplus-note/vector-find.html
建议大家还是自己手动敲一下,看过仅仅是看过,敲一次能映像深刻不少呢。