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

后端 未结 4 2026
我寻月下人不归
我寻月下人不归 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: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);
    }
    

提交回复
热议问题