用私有变量进行C++排序数组

用私有变量进行C++排序数组,c++,C++,我有这门课 Class VehicleTwoD { private: doubleArea; } 所以我想按双区排序 在我的main.cpp 我得到 当我试着运行这个程序时,我在运行时得到了一个错误——分段核心转储错误 假设我的车辆计数器没有错误,并且当我尝试运行显示时,我的车辆至少有2个对象,那么实际出了什么问题 更新: 我之所以能做到这一点,是因为我从这里的专家那里得到了宝贵的反馈 在main.cpp int main() { VehicleTwoD *vehicletwod[100];

我有这门课

Class VehicleTwoD
{
private:
doubleArea;
}
所以我想按双区排序

在我的main.cpp

我得到

当我试着运行这个程序时,我在运行时得到了一个错误——分段核心转储错误

假设我的车辆计数器没有错误,并且当我尝试运行显示时,我的车辆至少有2个对象,那么实际出了什么问题

更新:

我之所以能做到这一点,是因为我从这里的专家那里得到了宝贵的反馈

在main.cpp

int main()
{
VehicleTwoD *vehicletwod[100];

for(int i=0;i<100;i++)
{
vehicletwod[i] = new VehicleTWoD();
}

//some computation then I go to sort..

sort(&vehicletwod[0],&vehicletwod[vehicleCounter]);

for(int i=0;i<vehicleCounter;i++)
{
cout << vehicletwod->toDisplay() << endl;
}

}
输出保持不变,没有排序。。我不知道为什么

这是我在VehicleTwoD.h上做的布尔运算符

public:
bool operator<(const VehicleTwoD&) const;
汽车世界

public:
bool operator<(const VehicleTwoD&) const;
汽车世界公司

bool VehicleTwoD::operator<(const VehicleTwoD& rhs) const
{
return area > rhs.area;
}
什么都没有分类。。我希望它至少有降序和升序之分

我想知道是否有一种方法可以将我的VehicleTwoD数组分配到向量调用sortVector中,因为在升序排序之后,我也想降序排序

所以向量中的反函数将很好地解决它

有什么建议吗?感谢所有在这里帮助和善良的专家

这只会创建指向无效内存位置的引用数组

您需要一个循环或其他方法来分配100个有效引用。例如:

for(int i=0;i < 100;i++)
{
  vehicletwod[i] = new VehicleTwoD() ;
}
您必须记住释放这些内存。

嗯,vehicletwod[i]->显示是UB,因为vehicletwod[i]无论我是什么都不会初始化

VehicleTwoD*VehicleTwoD[100];只需创建一个由100个悬空指针组成的数组


我建议您改用std::vector,因为您的类似乎是多态的,您需要多态行为。

您的程序中有很多错误

VehicleTwoD *vehicletwod[100];
创建100个指向VehicleTwoD的未初始化指针数组

对由两个指针vehicletwod[0]和vehicletwod[vehicleCounter]定义的范围进行排序。由于数组未初始化,这些指针是垃圾,因此排序会损坏内存

您可能需要以下内容

std::vector<VehicleTwoD> vehicletwod; // vector of instances
// initialize the vector
// ...
sort(vehicletwod.begin(), vehicletwod.end()); 

为了补充其他人所说的内容,这里有一个可编译的示例,更新为反向排序/更新为静态类比较函数:

<>而不是重载<运算符,您可能需要考虑进行排序。 方向是明确的,例如通过如下所示指定静态比较器。 我更喜欢这个选项,因为它允许您记录您的意图

车内。h:

#include <random>
class VehicleTwoD
{

    private:
        double area;
    public:
        VehicleTwoD() 
        {
            area = ((double)rand()/(double)RAND_MAX);
        }

        double toDisplay()
        {
            return area;
        }

        bool VehicleTwoD::operator<(const VehicleTwoD& rhs) const
        {
            return area > rhs.area;
        }
        static bool VehicleTwoD::greater(const VehicleTwoD &lhs, const VehicleTwoD &rhs) 
        {
            return lhs.area > rhs.area;
        }

        static bool VehicleTwoD::lesser(const VehicleTwoD &lhs, const VehicleTwoD &rhs) 
        {
            return lhs.area < rhs.area;
        }

};
在main.cpp中:

#include "Vehicle.h"    
#include <vector>
#include <time.h>
#include <algorithm>
#include <iostream>


int main()
{

    srand((unsigned)time(NULL));
    std::vector<VehicleTwoD> vehicles(100);
    std::vector<VehicleTwoD>::iterator it;


    sort(vehicles.begin(), vehicles.end());
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    std::cin.get();

    // reverse sort - note that rbegin() rend() may be second-class citizens
    // depending on your compiler's implementation and that their use may therefore
    // be limited
    sort(vehicles.rbegin(), vehicles.rend());
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    std::cin.get();

    // or (document your intention)
    sort(vehicles.begin(), vehicles.end(), VehicleTwoD::greater);
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    std::cin.get();

    sort(vehicles.begin(), vehicles.end(), VehicleTwoD::lesser);
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    vehicles.clear();
    std::cin.get();
    return 0;

}

我该如何释放这些记忆,谢谢!我用这里发布的附加解决方案更新了我的问题。但是sort仍然不能对任何内容进行排序。在重载运算符中,它看起来应该是:returnarea,它至少应该按升序或降序排序。但是像这样的排序什么也不做,什么也不改变。这可能有助于发布排序功能。您好,上面发布的代码将准确排序。发布您的实际可编译代码,以便我们了解问题所在。对于反向排序,无需分配另一个向量。您可以使用反转iteror rbegin和rend来反转排序顺序。有关示例,请参见上面更新的代码。
#include <random>
class VehicleTwoD
{

    private:
        double area;
    public:
        VehicleTwoD() 
        {
            area = ((double)rand()/(double)RAND_MAX);
        }

        double toDisplay()
        {
            return area;
        }

        bool VehicleTwoD::operator<(const VehicleTwoD& rhs) const
        {
            return area > rhs.area;
        }
        static bool VehicleTwoD::greater(const VehicleTwoD &lhs, const VehicleTwoD &rhs) 
        {
            return lhs.area > rhs.area;
        }

        static bool VehicleTwoD::lesser(const VehicleTwoD &lhs, const VehicleTwoD &rhs) 
        {
            return lhs.area < rhs.area;
        }

};
#include "Vehicle.h"    
#include <vector>
#include <time.h>
#include <algorithm>
#include <iostream>


int main()
{

    srand((unsigned)time(NULL));
    std::vector<VehicleTwoD> vehicles(100);
    std::vector<VehicleTwoD>::iterator it;


    sort(vehicles.begin(), vehicles.end());
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    std::cin.get();

    // reverse sort - note that rbegin() rend() may be second-class citizens
    // depending on your compiler's implementation and that their use may therefore
    // be limited
    sort(vehicles.rbegin(), vehicles.rend());
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    std::cin.get();

    // or (document your intention)
    sort(vehicles.begin(), vehicles.end(), VehicleTwoD::greater);
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    std::cin.get();

    sort(vehicles.begin(), vehicles.end(), VehicleTwoD::lesser);
    for(it = vehicles.begin();  it != vehicles.end(); it++)
    {
        std::cout << "Vehicle area -> " << it->toDisplay() << std::endl;
    }
    std::cout << "Press any key..." << std::endl;
    vehicles.clear();
    std::cin.get();
    return 0;

}