Fastest way to sort a list of number and their index

前端 未结 8 1188
萌比男神i
萌比男神i 2021-02-14 21:15

I have a question that could seem very basic, but it is in a context where \"every CPU tick counts\" (this is a part of a larger algorithm that will be used on supercomputers).<

8条回答
  •  旧时难觅i
    2021-02-14 21:39

    Use std::vector and std::sort. That should provided the fastest sort method. To Find the original index create a struct.

    struct A {
        int num;
        int index;
    }
    

    Then make your own compare Predicate for sort that compares the num in the struct.

    struct Predicate {
        bool operator()(const A first, const A second) {
            return first.num < second.num;
        }
    }
    

    std::sort(vec.begin(), vec.end(), Predicate())

提交回复
热议问题