[Solved] N-th occurence of a number in a vector


you can do it like

template<class InputIterator, class T>
size_t find_nth (InputIterator first, InputIterator last, const T& val,size_t count)
{
    size_t ret=0;
    size_t index=0;
    while (first!=last) {
       if (*first==val)
       {
        ret++;
       }
       if(ret == count)
       {
        return index;
       }
    index++;
    ++first;
  }
  return -1;
}

int main()
{
   vector<int> myInt={1,2,3,1,4,1,5,6};
   int ret=find_nth(myInt.begin(),myInt.end(),1,3);//ret is -1 if not
   std::cout<<"index : "<<ret<<std::endl;
   return 0;
}

1

solved N-th occurence of a number in a vector