0

P S : May be this question has already been asked but I tried a lot and also I'm not using the pointer with vector. If the solution is that, please tell me how to use pointer here.

My Question: I'm creating a vector of Car class instances and used getter and setter methods to retrieving and pushing new records inside it. Even I'm editing that records also but I don't know how to delete particular record! I have put the code in comments which I tried by myself. could someone help me to remove/erase the particular record/ instance of the class, from this vector?

Thanks in advance.

Car.cpp

#include "Car.h"
#include "global.h"
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
int cid =1;

string Name;
float Price;

//In this function I want to delete the records
void deleteCarVector( vector<Car>& newAllCar)
{
    int id;
    cout << "\n\t\t Please Enter the Id of Car to Delete Car Details :  ";
    cin >> id;
    //replace (newAllCar.begin(), newAllCar.end(),"a","b");

    unsigned int size = newAllCar.size();
    for (unsigned int i = 0; i < size; i++)
    {
        if(newAllCar[i].getId() == id)
        {
            cout << "Current Car Name : "<<newAllCar[i].getName() << "\n";

            // Here Exactly the problem!
            // delete newAllCar[i];
            // newAllCar.erase(newAllCar[i].newName);
            // newAllCar.erase(remove(newAllCar.begin(),newAllCar.end(),newAllCar.at(i).getId()));
        }
    }
    printCarVector(newAllCar);
    cout << endl;
}
4
  • Please edit your code to reduce it to a minimal reproducible example of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. Commented May 24, 2018 at 17:22
  • newAllCar.erase(i); should do the trick. Good luck!
    – BonsaiOak
    Commented May 24, 2018 at 18:30
  • Voting to close as a duplicate of stackoverflow.com/questions/875103/… OP didn't specify many details so any answer would have to be for the general case, which is already covered in the other question.
    – BonsaiOak
    Commented May 24, 2018 at 18:35
  • Possible duplicate of How do I erase an element from std::vector<> by index?
    – BonsaiOak
    Commented May 24, 2018 at 18:35

2 Answers 2

3

[...]How can I "remove/erase" the particular record from vector class object?

You have your answer in your question itself: you need erase–remove idiom to remove the Car objects from your std::vector<Car>, according to the key/id you provide.

carVec.erase(std::remove_if(carVec.begin(), carVec.end()
     , [&id_to_delete](const Car& ele) {
            return ele.getnewId() == id_to_delete;
     }), 
     carVec.end()
);

Live Demo


Updates

C++20 ensures a uniform container erasure semantics for all standard containers. Using std::erase_if (std::vector), the OP's code would look like:

std::erase_if(carVec, [&id_to_delete](const auto& ele) { 
    return ele.getnewId() == id_to_delete;
});

Live Demo

0
1

you can do something like below if you don't want to use lambda functions

void deleteCarVector( vector<Car>& newAllCar)
{
    int id;
    cout<<" \n\t\t Please Enter the Id of Car to Delete Car Details :  ";
    cin>>id;

    auto carItr = newAllCar.begin();

    while(carItr != newAllCar.end())
    {
        if((*carItr)->getId==id)
        {
            delete *carItr;
            newAllCar.erase(carItr);
            break;
        }
        carItr++;
    }

    printCarVector(newAllCar);

    cout << endl;
}
4
  • 1
    Can I talk you into replacing the whileall of the iterator work with a range-based for? Commented May 24, 2018 at 17:26
  • There are different ways of coding you can use range based for with little tweeks
    – Maddy
    Commented May 24, 2018 at 17:29
  • 2
    Its giving me the pointer error... Commented May 24, 2018 at 18:20
  • what is the error that you are getting
    – Maddy
    Commented May 24, 2018 at 18:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.