Moving elements from a deque to a stack?

  c++

I have a deque made up from a class called ‘car’. The construct of car is as follows:

car(int id, const std::string &license) : id(id), license(license) {}

The deque is in another class called ‘garage’ with a size limit of 10. Declared as a private member of garage:

deque<car> parking_lot;

Given a string license, in the function

void garage::departure(const std::string &license)

I need to find if the inputted license is inside deque<car> parking_lot and if it is, then move all elements in front of that found license out into a temporary stack, remove the car object with the inputted license, then move all car obj back into the deque<car>parking_lot from the temporary stack so that they are all in the same order as they was; at the same time incrementing the number of times they were moved out into the temp. stack.

I tried using for loop and pushing each elem. iterator into the stack one by one and then break the loop if it = itr as follows:

std::stack<car> moved;

if (const auto it = std::find(parking_lot.begin(), parking_lot.end(), license) != parking_lot.end())
{
  for (auto itr : parking_lot)
  {
     moved.push(itr);

     if(itr == it)
     {
       break;
     }
  }

But it returned an error:

"no match for ‘operator==’ (operand types are ‘car’ and ‘std::deque::iterator’ {aka ‘std::_Deque_iterator<car, car&, car*>’})"

I am not sure how to approach this problem.

Second, I am using

parking_lot.erase(std::remove(parking_lot.begin(), parking_lot.end(), license), parking_lot.end());

To remove all elem in front of license but how would I increment the number of time each car obj (that are in front of license inside the range of .begin() to license) was moved out?

I am still relatively new to C++.

Source: Windows Questions C++

LEAVE A COMMENT