小编not*_*iry的帖子

如何使用 std::sort 根据某个函数返回的值对数组进行排序,同时传递要排序的数组元素?

如何使用 std::sort 根据某个函数返回的值对数组进行排序,同时传递要排序的数组元素?

class Board{
    //members
};
int score(int num,Board b){
   return b.evaluate(num);
   //lets say score() function returns an evaluation based 
   //on num and Board type object b. 
}

void main(){
    Board b;
    int num_array[10]{0,1,2,3,4,5,6,7,8,9};
    std::sort(num_array.begin(),num_array.end());  
        //how to use std::sort to sort num_array based on values returned
        //by score() while passed elements of num_array
}
Run Code Online (Sandbox Code Playgroud)

有没有办法将函数作为 std::sort 中的第三个参数传递,还是需要以其他方式解决?

c++ arrays sorting

1
推荐指数
1
解决办法
84
查看次数

标签 统计

arrays ×1

c++ ×1

sorting ×1