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

后端 未结 4 2024
我寻月下人不归
我寻月下人不归 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(){
    vector<string>names{"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 "<<req_name<<" is "<<i<<'\n';
                return 0;
            }
            else if(names[i]!=req_name && i==names.size()-1) {
                cout<<"That name is not an element in this vector"<<'\n';
            } else {
                continue;
            }
        }
    
    0 讨论(0)
  • 2020-12-07 14:20

    Nobody mentioned the general version as a function for any std::vector<T> so here it is (in one ugly line) :

    #include <algorithm>
    
    template <typename T> long long int indexOf(const std::vector<T> &vector , const T &data){
    
       return (std::find(vector.begin(), vector.end(), data) != vector.end()) //Is it in the vector ?
               ?
               std::distance(vector.begin(), std::find(vector.begin(), vector.end(), data)) //Yes, take the distance (C++11 and above, see the previous answers)
               : -1; //No, return -1
    }
    

    And without the ternary operator :

    #include <algorithm>
    
    template <typename T> long long int indexOf(const std::vector<T> &vector , const T &data){
    
         if((std::find(vector.begin(), vector.end(), data) != vector.end())){
             return std::distance(vector.begin(), std::find(vector.begin(), vector.end(), data));
         }
         else{return -1;}
    }
    

    It returns either the index or -1 if the element is not in the vector.


    Note that this may be very slow for big vectors, as it computes two times std::find.

    0 讨论(0)
  • 2020-12-07 14:23

    If you want an index, you can use std::find in combination with std::distance.

    auto it = std::find(Names.begin(), Names.end(), old_name_);
    if (it == Names.end())
    {
      // name not in vector
    } else
    {
      auto index = std::distance(Names.begin(), it);
    }
    
    0 讨论(0)
  • 2020-12-07 14:27

    To get a position of an element in a vector knowing an iterator pointing to the element, simply subtract v.begin() from the iterator:

    ptrdiff_t pos = find(Names.begin(), Names.end(), old_name_) - Names.begin();
    

    Now you need to check pos against Names.size() to see if it is out of bounds or not:

    if(pos >= Names.size()) {
        //old_name_ not found
    }
    

    vector iterators behave in ways similar to array pointers; most of what you know about pointer arithmetic can be applied to vector iterators as well.

    Starting with C++11 you can use std::distance in place of subtraction for both iterators and pointers:

    ptrdiff_t pos = distance(Names.begin(), find(Names.begin(), Names.end(), old_name_));
    
    0 讨论(0)
提交回复
热议问题