Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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,(我没有发布公开课) bool sortByName(Car&CarVector) { 返回CarVector.getBrand()

(我没有发布公开课)

bool sortByName(Car&CarVector)
{ 
返回CarVector.getBrand()
主要内容:

intmain(){
矢量切割器;
读取文件(CarVector);
ListCar(CarVector);
返回0;
}
当我调用函数“sort”时,列出Car函数,以按名称对对象向量进行排序:

void ListCar(vector<Car>&CarVector){

    int i, op;
    system("CLS");

    sort(CarVector.begin(), CarVector.end(), sortByName);

    cout << "MENU::CAR LIST BY NAME" << endl;
    cout << ":Brand: \t:Mileage: \t:Year: \t\t:Price:" << endl;

    for(i=0; i<CarVector.size();i++)
    {
        cout << CarVector[i].getBrand() << " \t\t";
        cout << CarVector[i].getMileage() << " \t\t";
        cout << CarVector[i].getYear() << " \t\t";
        cout << CarVector[i].getPrice() << " \t\t";
        cout << endl;
    }

    do
    {
    cout << endl << "1.Back: ";
    cin >> op;
    }while(op!=1);
}
void ListCar(向量和向量){
int i,op;
系统(“CLS”);
排序(CarVector.begin()、CarVector.end()、sortByName);

cout在比较函数中,您需要使用两个参数:它应该相互比较这些对象。而且,这与任何向量无关(它对向量的元素进行操作),因此您不应该这样命名参数以避免混淆

因此,函数可以如下所示:

bool sortByName(Car &a, Car &b) 
{ 
    return a.getBrand() < b.getBrand(); 
}
但是有必要将
const
放在函数
Car::getBrand()
签名的末尾,以表明该函数不会修改其操作的对象。这称为const correction。如前所述,并不总是需要该过程(如使用
std::sort
时)但是,具有常量正确性是一种很好的风格

或者,如果编译器支持lambda,则可以使用它(需要启用C++11支持):

std::sort(CarVector.begin()、CarVector.end()、[](const Car&a、const Car&b){
返回a.getBrand()

请注意,如果要始终按名称比较汽车,则实现
操作符
bool sortByName(汽车和CarVector1、汽车和CarVector2)是有意义的
{ 
返回CarVector1.getBrand()

使用此sortByName

您必须在
sortByName
方法中传递两个汽车对象,以比较两个对象,如下所示:

bool sortByName(Car &C1,Car &C2) 
{ 
    return C1.getBrand() < C2.getBrand(); 
}
bool sortByName(汽车和C1、汽车和C2)
{ 
返回C1.getBrand()

检查此项以了解更多详细信息

请在问题1中描述您的错误:错误C2197:“bool(uu cdecl*)(Car&)”:调用第2个参数太多:错误C1903:无法从以前的错误中恢复;s@MarcodeBarbosa如前所述:两条建议:1)删除“新建”来自Car成员名称的前缀,2)
sortByName
应被称为
compareByBrand
,3)排序应发生在
列表CARS
(注意名称)之外,例如在
main
,4)不要做
系统(“CLS”);
@和YT,感谢您的建议。一个问题,关于
系统(“CLS”)
,你为什么这么说?这是一种不好的代码实现方式?是的,运算符必须在类外重载
void ListCar(vector<Car>&CarVector){

    int i, op;
    system("CLS");

    sort(CarVector.begin(), CarVector.end(), sortByName);

    cout << "MENU::CAR LIST BY NAME" << endl;
    cout << ":Brand: \t:Mileage: \t:Year: \t\t:Price:" << endl;

    for(i=0; i<CarVector.size();i++)
    {
        cout << CarVector[i].getBrand() << " \t\t";
        cout << CarVector[i].getMileage() << " \t\t";
        cout << CarVector[i].getYear() << " \t\t";
        cout << CarVector[i].getPrice() << " \t\t";
        cout << endl;
    }

    do
    {
    cout << endl << "1.Back: ";
    cin >> op;
    }while(op!=1);
}
bool sortByName(Car &a, Car &b) 
{ 
    return a.getBrand() < b.getBrand(); 
}
bool sortByName(const Car &a, const Car &b) 
{ 
    return a.getBrand() < b.getBrand(); 
}
std::sort(CarVector.begin(), CarVector.end(), [](const Car &a, const Car &b){
    return a.getBrand() < b.getBrand();
});
bool sortByName(Car &CarVector1,Car &CarVector2) 
{ 
    return CarVector1.getBrand() < CarVector2.getBrand(); 
}
bool sortByName(Car &C1,Car &C2) 
{ 
    return C1.getBrand() < C2.getBrand(); 
}