gpt4 book ai didi

c++ - 对指针 vector 进行排序工作一次然后崩溃 "unable to read memory"

转载 作者:行者123 更新时间:2023-11-28 02:50:52 25 4
gpt4 key购买 nike

我有一个 Flight pointers vector ,我将它传递给一个函数,以便在屏幕上进行排序和显示。我在 Flight 类 中使用仿函数来进行排序。它第一次运行完美,然后在 sortCriteria 递增后,它在第二次尝试单步执行航类 vector 时崩溃。我得到的错误是 Access violation reading location 0x013DFFFC,所以我打赌这与 vector 在第一次排序后失去对内存位置的跟踪有关。提前感谢您提供的任何帮助。这是我的排序功能:

//this function displays the flight schedule and sorts it by any field
void showFlightSchedule(vector<Flight*>& flights)
{
//declare local variables
char choice = ' ';
int sortCriteria = 1;
while (toupper(choice) != 'X')
{
//choosing which field to sort the schedule by
switch (sortCriteria)
{
case 1:
sort (flights.begin(), flights.end(), Flight::SortByDepartCity);
break;
case 2:
sort (flights.begin(), flights.end(), Flight::SortByDestinationCity);
break;
case 3:
sort (flights.begin(), flights.end(), Flight::SortByDepartTime);
break;
case 4:
sort (flights.begin(), flights.end(), Flight::SortByArrivalTime);
break;
case 5:
sort (flights.begin(), flights.end(), Flight::SortByFlightNumber);
break;
case 6:
sort (flights.begin(), flights.end(), Flight::SortByAircraftType);
break;
case 7:
sort (flights.begin(), flights.end(), Flight::SortByFreqFlyPoints);
break;
case 8:
sort (flights.begin(), flights.end(), Flight::SortByFlightFull);
break;
}


//display header
system("cls");
cout << left << endl;
cout << " " << setw(7) << "From" << setw(6) << "To" << setw(8) << "Depart" << setw(8) << "Arrive" <<
setw(8) << "Flight" << setw(12) << "Aircraft" << setw(12) << "Frequent" << setw(6) << "Flight\n";
cout << "\t\t\t\t " << setw(10) << "Number" << setw(8) << "Type" << setw(14) << "Flyer Points" << setw(6) << "Status\n";

//slightly altering the header to indicate how the list is sorted
switch (sortCriteria)
{
case 1:
cout << " --\\_/------------------------------------------------------------------\n\n";
break;
case 2:
cout << " ---------\\_/-----------------------------------------------------------\n\n";
break;
case 3:
cout << " ----------------\\_/----------------------------------------------------\n\n";
break;
case 4:
cout << " ------------------------\\_/--------------------------------------------\n\n";
break;
case 5:
cout << " --------------------------------\\_/------------------------------------\n\n";
break;
case 6:
cout << " -----------------------------------------\\_/---------------------------\n\n";
break;
case 7:
cout << " -----------------------------------------------------\\_/---------------\n\n";
break;
case 8:
cout << " ----------------------------------------------------------------\\_/----\n\n";
break;
}

//step through the flights vector displaying the information
for (int idx = 0; idx < flights.size(); idx++)
{
cout << " " << setw(7) << flights[idx]->getDepartCity() << setw(6) << flights[idx]->getDestinationCity() << setw(8) <<
flights[idx]->getDepartTime() << setw(9) << flights[idx]->getArrivalTime() << setw(10) << flights[idx]->getFlightNumber() << setw(11) <<
flights[idx]->getAircraftType() << setw(11) << flights[idx]->getFreqFlyPoints();
if (flights[idx]->getFlightFull())
cout << setw(6) << "FULL\n\n";
else
cout << endl << endl;
flights[idx]++;
}

//display footer
cout << " -----------------------------------------------------------------------\n\n";
cout << "\t\t\t C -- Change Sorting\n";
cout << "\t\t\t X -- Exit to Main Menu\n";
cout << "\t\t\t Enter C or X: ";

//get choice from user
cin >> choice;

//error-trapping loop
while ((toupper(choice) != 'C') && (toupper(choice) != 'X'))
{
cout << "Please choose \"C\" or \"X\": ";
cin >> choice;
}

//changing the sort flag
if (sortCriteria == 8)
sortCriteria = 1;
else
sortCriteria++;
}

}

在我的 Flight.h 文件中,我有这些排序语句:

static bool SortByDepartCity(const Flight* f1, const Flight* f2)
{
return f1->departCity < f2->departCity;
}

static bool SortByDestinationCity(const Flight* f1, const Flight* f2)
{
return f1->destinationCity < f2->destinationCity;
}

static bool SortByDepartTime(const Flight* f1, const Flight* f2)
{
return f1->departTime < f2->departTime;
}

static bool SortByArrivalTime(const Flight* f1, const Flight* f2)
{
return f1->arrivalTime < f2->arrivalTime;
}

static bool SortByFlightNumber(const Flight* f1, const Flight* f2)
{
return atoi(f1->flightNumber.c_str()) < atoi(f2->flightNumber.c_str());
}

static bool SortByAircraftType(const Flight* f1, const Flight* f2)
{
return f1->aircraftType < f2->aircraftType;
}

static bool SortByFreqFlyPoints(const Flight* f1, const Flight* f2)
{
return f1->freqFlyPoints > f2->freqFlyPoints;
}

static bool SortByFlightFull(const Flight* f1, const Flight* f2)
{
return f1->flightFull < f2->flightFull;
}

最佳答案

删除这个

flights[idx]++;

在打印循环的最后一行。

附言这里有一些代码可以简化你的代码

template< class T, class FieldType, FieldType T::*FieldPtr >
struct LessBy {
bool operator()( const T * left, const T * right ) const {
return left->*FieldPtr < right->*FieldPtr;
}
};

typedef LessBy< Flight, std::string, & Flight::departCity > SortByDepartCity;
typedef LessBy< Flight, std::string, & Flight::destinationCity > SortByDestinationCity;
//and so on

关于c++ - 对指针 vector 进行排序工作一次然后崩溃 "unable to read memory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com