首页 > 如何在vector<string>中查找string类型的字符串

如何在vector<string>中查找string类型的字符串

如题目所示,如何在vector<string>中查找一个string类型的字符串,标准库中的find()对于vector<string>类型不知道如何使用。 谢谢!


这是题主想要的么

#include <bits/stdc++.h>
using namespace std;
int main() {
    int N = 5;
    vector <string> v;
    for(int i = 0;i < N; i++) {
        string tmp;
        char a[5];
        for(int j = 0;j < 3; j++)
            a[j] = 'a'+i;
        a[3] = '\0';
        v.push_back(a);
    }
    auto idx = find(v.begin(), v.end(), string("bbb")) - v.begin();
    cout<<idx<<endl;

    return 0;
}
    

vector<string> vstring;
//传入迭代器
auto index=std::find(vstring.begin(),vstring.end(),"string2find");
if(index!=vstring.end()){
    cout<<"找到了,是第"<<index<<"个"<<endl;
}else{
    cout<<"找不到"<<endl;
}

template< class InputIt, class T >
InputIt find( InputIt first, InputIt last, const T& value );

InputIt的first和last是传入的迭代器

【热门文章】
【热门文章】