How to get position of a certain element in strings vector, to use it as an index in ints vector?

后端 未结 4 2032
我寻月下人不归
我寻月下人不归 2020-12-07 13:47

I am trying to get the index of an element in a vector of strings, to use it as an index in another vector of int type, is this possible ?

4条回答
  •  孤街浪徒
    2020-12-07 14:08

    I am a beginner so here is a beginners answer. The if in the for loop gives i which can then be used however needed such as Numbers[i] in another vector. Most is fluff for examples sake, the for/if really says it all.

    int main(){
    vectornames{"Sara", "Harold", "Frank", "Taylor", "Sasha", "Seymore"};
    string req_name;
    cout<<"Enter search name: "<<'\n';
    cin>>req_name;
        for(int i=0; i<=names.size()-1; ++i) {
            if(names[i]==req_name){
                cout<<"The index number for "<

提交回复
热议问题