我有一个自定义容器UniqueArray
。这是头文件:
using namespace std;
template <class Element, class Compare = std::equal_to<Element>>
class UniqueArray {
public:
Element** data;
unsigned int curr_size;
unsigned int max_size;
int* availability_array;
explicit UniqueArray(unsigned int size);
UniqueArray(const UniqueArray& other);
~UniqueArray();
UniqueArray& operator=(const UniqueArray&) = delete;
unsigned int insert(const Element& element);
bool getIndex(const Element& element, unsigned int& index) const;
const Element* operator[] (const Element& element) const;
bool remove(const Element& element);
unsigned int getCount() const;
unsigned int getSize() const;
class Filter {
public:
virtual bool operator() (const Element&) const = 0;
};
UniqueArray filter(const Filter& f) const;
class UniqueArrayIsFullException{};
typedef Element** ua_iterator;
typedef const Element** ua_const_iterator;
ua_iterator begin(){
return data;
}
ua_const_iterator begin() const {
return data;
}
ua_iterator end(){
return data + max_size;
}
ua_const_iterator end() const {
return data + max_size;
}
};
现在,此容器稍后在称为ParkingLot
的较大容器中使用,这是头文件:
using namespace ParkingLotUtils;
using std::ostream;
namespace MtmParkingLot {
class ParkingLot {
public:
UniqueArray<Vehicle, Vehicle::compareVehicles> motorbike_parking;
UniqueArray<Vehicle, Vehicle::compareVehicles> car_parking;
UniqueArray<Vehicle, Vehicle::compareVehicles> handicapped_parking;
ParkingLot(unsigned int parkingBlockSizes[]);
~ParkingLot() = default;
ParkingResult enterParking(VehicleType vehicleType, LicensePlate licensePlate, Time entranceTime);
ParkingResult exitParking(LicensePlate licensePlate, Time exitTime);
ParkingResult getParkingSpot(LicensePlate licensePlate, ParkingSpot& parkingSpot) const;
void inspectParkingLot(Time inspectionTime);
friend ostream& operator<<(ostream& os, const ParkingLot& parkingLot);
int calculateFee(Time entryTime, Time exitTime, VehicleType type);
int calculateFeeRecursive(Time entryTime, Time exitTime, VehicleType type, int iter, int totalPrice);
bool isVehicleInLot(LicensePlate licensePlate, VehicleType& type, unsigned int& index);
};
}
我非常想尝试遍历整个ParkingLot
。
到目前为止,这是我写的内容,不确定如何继续:
void ParkingLot::inspectParkingLot(Time inspectionTime) {
std::vector<UniqueArray<Vehicle, Vehicle::compareVehicles>> ParkingLotVector = {motorbike_parking, car_parking, handicapped_parking};
std::vector<UniqueArray<Vehicle, Vehicle::compareVehicles>>::iterator ParkingLotIterator;
for(ParkingLotIterator = ParkingLotVector.begin(); ParkingLotIterator < ParkingLotVector.end(); ParkingLotIterator++){
for(/*stuck*/)
}
}
在第一个for循环中,通过向量内部的UniqueArrays
进行迭代。如何使用我在第一个头文件中声明的迭代器在每个Element
中的UniqueArrays
中进行迭代?