c++ sort keeping track of indices

后端 未结 4 703
忘了有多久
忘了有多久 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:36

    Adding to @Konrad answer:

    If for some reason you are not able to use C++11, then you can use boost::phoenix to simulate it like

        #include 
        #include 
    
        #include 
        #include 
    
        template 
        std::vector ordered(std::vector const& values)
        {
            using namespace boost::phoenix;
            using namespace boost::phoenix::arg_names;
    
            std::vector indices(values.size());
            int i = 0;
            std::transform(values.begin(), values.end(), indices.begin(), ref(i)++);
            std::sort(indices.begin(), indices.end(), ref(values)[arg1] < ref(values)[arg2]);
            return indices;
        }
    

提交回复
热议问题