c++ sort keeping track of indices

后端 未结 4 692
忘了有多久
忘了有多久 2020-11-27 03:44

do you have some efficient routine for returning array with indices for sorted elements in a array? I think that some convenient way exists using stl vector. Do you have alr

4条回答
  •  眼角桃花
    2020-11-27 04:31

    You could try something like this:

    template
    class index_sorter {
      public:
        compare(C const& c) : c(c) {}
        bool operator()(std::size_t const& lhs, std::size_t const& rhs) const {
          return c[lhs] < c[rhs];
        }
      private:
        C const& c;
    };
    
    std::sort(index_vector.begin(), index_vector.end(), index_sorter(vector));
    

提交回复
热议问题