Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 排序多个数据成员_C++_Sorting_Vector - Fatal编程技术网

C++ 排序多个数据成员

C++ 排序多个数据成员,c++,sorting,vector,C++,Sorting,Vector,在指针向量中进行多个排序的最佳方法是什么(std::vector使用并为每个案例编写一个Compare函子。如果需要组合多个字段,请使用 下面是一个示例。您不需要像我在这里所做的那样使排序函数静态成员,但它可以对私有成员使用std::tie #include <algorithm> #include <iostream> #include <vector> class Vehicle { public: Vehicle(unsigned maxSpe

在指针向量中进行多个排序的最佳方法是什么(
std::vector使用并为每个案例编写一个
Compare
函子。如果需要组合多个字段,请使用

下面是一个示例。您不需要像我在这里所做的那样使排序函数
静态
成员,但它可以对私有成员使用
std::tie

#include <algorithm>
#include <iostream>
#include <vector>

class Vehicle {
public:
    Vehicle(unsigned maxSpeed, unsigned short numberOfHorsePower,
            unsigned short numberOfGears, unsigned short numberOfWheels,
            unsigned weight) :
        maxSpeed_(maxSpeed),
        numberOfHorsePower_(numberOfHorsePower), numberOfGears_(numberOfGears),
        numberOfWheels_(numberOfWheels), weight_(weight) {}

    // you can use the accessor functions with loose sort statements if you don't
    // need to tie many columns together

    inline unsigned get_maxSpeed() const { return maxSpeed_; }
    inline unsigned short get_numberOfHorsePower() const {
        return numberOfHorsePower_;
    }
    inline unsigned short get_numberOfGears() const { return numberOfGears_; }
    inline unsigned short get_numberOfWheels() const { return numberOfWheels_; }
    inline unsigned get_weight() const { return weight_; }

    // sorting functions

    template<typename It>
    static void sort_on_speed(It first, It last) {
        std::sort(first, last,
            // a lambda receiving references to two Vehicle objects to compare
            [](const Vehicle& a, const Vehicle& b) {
                // return true if a is less than b (using the field you want)
                return a.maxSpeed_ < b.maxSpeed_;
            }
       );
    }

    template<typename It>
    static void sort_on_hp_plus_speed(It first, It last) {
        std::sort(first, last,
            // another lambda using std::tie to sort primarily on hp
            // and secondary on speed
            [](const Vehicle& a, const Vehicle& b) {
                return std::tie(a.numberOfHorsePower_, a.maxSpeed_) <
                       std::tie(b.numberOfHorsePower_, b.maxSpeed_);
        });
    }

private:
    unsigned int maxSpeed_;
    unsigned short numberOfHorsePower_;
    unsigned short numberOfGears_;
    unsigned short numberOfWheels_;
    unsigned int weight_;
};

int main() {
    std::vector<Vehicle> cars = {
        {1, 2, 3, 4, 5}, {2, 3, 4, 5, 1}, {3, 4, 5, 1, 2}
    };

    // sort on speed
    Vehicle::sort_on_speed(cars.begin(), cars.end());

    // sort on hp + speed
    Vehicle::sort_on_hp_plus_speed(cars.begin(), cars.end());
}
#包括
#包括
#包括
等级车辆{
公众:
车辆(无符号最大速度,无符号短马力,
无符号短数齿轮,无符号短数车轮,
无符号重量):
最大速度(最大速度),
numberofhorspower(numberofhorspower),numberOfGears(numberOfGears),
numberOfWheels(numberOfWheels),weight(weight){}
//如果不使用,则可以将访问器函数与松散排序语句一起使用
//需要把许多柱子绑在一起
内联无符号get_maxSpeed()常量{return maxSpeed}
内联无符号短get_numberOfHorsePower()常量{
返回马力的数值;
}
内联无符号短get_numberOfGears()常量{return numberOfGears_;}
内联无符号短get_numberOfWheels()常量{return numberOfWheels_;}
内联无符号get_weight()常量{返回权重}
//排序功能
模板
静态无效排序速度(先排序,后排序){
排序(第一,最后,
//接收对两个车辆对象的引用以进行比较的lambda
[](施工车辆和a、施工车辆和b){
//如果a小于b,则返回true(使用所需字段)
返回a.maxSpeed
应该非常有用。

使用带有自定义比较器的
std::sort
。您可以进行预测:ts似乎询问多个排序,即按标准1单独排序的数组。同时,按标准2同时排序的相同数组。即独立。可能的重复表示单个sorting:根据标准1,在标准1的相同值范围内,也根据标准2进行排序。
    void Vehicles::sortVehiclesByWeightAsc()
    {
        Vehicle* tmp;
        size_t n = _VehiclesCollection.size();
        int i, j, minIndex;
    
        for (i = 0; i < n - 1; i++) 
        {
            minIndex = i;
            for (j = i + 1; j < n; j++) 
            {
                if (_VehiclesCollection[j].weight < _VehiclesCollection[minIndex].weight)
                {
                    minIndex = j;
                }
            }
    
            if (minIndex != i) 
            {
                tmp = _VehiclesCollection[i];
                _VehiclesCollection[i] = _VehicleCollection[minIndex];
                _VehiclesCollection[minIndex] = tmp;
            }
    
        }
    }
#include <algorithm>
#include <iostream>
#include <vector>

class Vehicle {
public:
    Vehicle(unsigned maxSpeed, unsigned short numberOfHorsePower,
            unsigned short numberOfGears, unsigned short numberOfWheels,
            unsigned weight) :
        maxSpeed_(maxSpeed),
        numberOfHorsePower_(numberOfHorsePower), numberOfGears_(numberOfGears),
        numberOfWheels_(numberOfWheels), weight_(weight) {}

    // you can use the accessor functions with loose sort statements if you don't
    // need to tie many columns together

    inline unsigned get_maxSpeed() const { return maxSpeed_; }
    inline unsigned short get_numberOfHorsePower() const {
        return numberOfHorsePower_;
    }
    inline unsigned short get_numberOfGears() const { return numberOfGears_; }
    inline unsigned short get_numberOfWheels() const { return numberOfWheels_; }
    inline unsigned get_weight() const { return weight_; }

    // sorting functions

    template<typename It>
    static void sort_on_speed(It first, It last) {
        std::sort(first, last,
            // a lambda receiving references to two Vehicle objects to compare
            [](const Vehicle& a, const Vehicle& b) {
                // return true if a is less than b (using the field you want)
                return a.maxSpeed_ < b.maxSpeed_;
            }
       );
    }

    template<typename It>
    static void sort_on_hp_plus_speed(It first, It last) {
        std::sort(first, last,
            // another lambda using std::tie to sort primarily on hp
            // and secondary on speed
            [](const Vehicle& a, const Vehicle& b) {
                return std::tie(a.numberOfHorsePower_, a.maxSpeed_) <
                       std::tie(b.numberOfHorsePower_, b.maxSpeed_);
        });
    }

private:
    unsigned int maxSpeed_;
    unsigned short numberOfHorsePower_;
    unsigned short numberOfGears_;
    unsigned short numberOfWheels_;
    unsigned int weight_;
};

int main() {
    std::vector<Vehicle> cars = {
        {1, 2, 3, 4, 5}, {2, 3, 4, 5, 1}, {3, 4, 5, 1, 2}
    };

    // sort on speed
    Vehicle::sort_on_speed(cars.begin(), cars.end());

    // sort on hp + speed
    Vehicle::sort_on_hp_plus_speed(cars.begin(), cars.end());
}