How to remove a class object from a vector of objects

Hello, I am having a issue here. Somehow I can't remove or erase an class object inside the vector of objects. To make it clear ill add some parts of my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  void edit(string empId,int companyId){
       string depId;
       fflush(stdin);
       cout<<"Enter the department id to change"<<endl;
       getline(cin,depId);
       fflush(stdin);
       string name;
       string id;
       for(unsigned i=0; i<comp[companyId].depVec.size(); i++){
           for(unsigned j=0; j<comp[companyId].depVec[i].emp.size(); j++){
               if(comp[companyId].depVec[i].emp[j].getEmpId() == empId){
                  name = comp[companyId].depVec[i].emp[j].getEmpName();
                  id = comp[companyId].depVec[i].emp[j].getEmpId();
                  comp[companyId].depVec[i].emp[j].erase();
                }
             }
          }
        for(unsigned i=0; i<comp[companyId].depVec.size(); i++){
           if(comp[companyId].depVec[i].getDepId()==depId){
              comp[companyId].depVec[i].createEmp(name,id);
           }
        }
   }

So as you can see i tried to erase the employee data from vector of employees, I know that I am using erase function not in right way but somehow i cant manage to use it. You may wonder about comp, depVec, its a vector of class objects. I organized my class such as, each company class object there is department class object, so each company can have many departments inside so i declared vector of departments, and also on each department there is a employee which is also vector of employees. So i was trying to implement a function where I need to change the employee to another department within a 1 company, this is i tried so far. I tried to get the employee name and the id then store it on a name, id variables then i push_back those into new department where it is equal to user's input department id.
Last edited on
o as you can see i tried to erase the employee data from vector of employees,


I would guess that comp[companyId].depVec[i].emp[j].erase();
should be: comp[companyId].depVec[i].emp.erase(comp[companyId].depVec[i].emp.begin()+j);

Consider using some variables to make that more readable.

1
2
3
4
5
6
7
8
9
10
11
12
    for (unsigned i = 0; i < comp[companyId].depVec.size(); i++) {
        auto & emp = comp[companyId].depVec[i].emp;
        for (unsigned j = 0; j < emp.size(); j++) {
            auto& employee = emp[j];

            if (employee.getEmpId() == empId) {
                name = employee.getEmpName();
                id = employee.getEmpId();
                emp.erase(emp.begin() + j);
            }
        }
    }

Oh my god thank you very much, that solved my problem. I really appreciate you, i've been trying this whole day and i kinda feel bad :(, i think i wont be a good coder and for that auto i never knew that it is possible to do like that, once again thank you really much.
@cire sorry can I ask 1 more question, how can i make that erasing function to be true only if the user inputed department id exists within the company not for other company? For example when i enter 1-b which is department id of another company. Somehow I cant make that happen i tried like
1
2
3
4
5
         if(comp[!companyId].depVec[i].getDepId() != depId){
            comp[companyId].depVec[i].emp.erase(comp[companyId].depVec[i].emp.begin()+j);
         } else {
            cout<<"Error department Id"<<endl;
          }
if(comp[!companyId].depVec[i].getDepId() != depId){

I don't think you want that exclamation mark there.

how can i make that erasing function to be true only if the user inputed department id exists within the company not for other company?

I'm not sure exactly what you're asking, but if you want to erase when depId is equal to comp[companyId].depVec[i].getDepId(), then you'll want to use equality (==) in the comparison, and not inequality (!=), but I suspect that extraneous exclamation mark was screwing you up.
Last edited on
well I mean i want to make that erase happen only if the user inputed department id exists in the current company, but when i enter another company's depId it removes the employee from deparment. For example employee can not change to another company's department. Without if statement the erase function will erase even when I input random non existing department id, so I dont want to remove the employee from a vector if I enter just random id it should be true only if user input id exists in current company. I tried the 1 you suggested but then the erase is not removing the employee from department
Does this help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void remove(string empId, int companyId, string deptId)
{
    auto& departments = comp[companyId].depVec;

    // find the deptartment with matching deptId
    std::size_t index = 0;
    while (index < departments.size() && departments[index].getDepId() != deptId)
        ++index;

    if (index == departments.size())
    {
        // No matching department was found for the specified company.
        // so do nothing
        return;
    }

    auto & employees = departments[index].emp;

    index = 0;
    while (index < employees.size() && employees[index].getEmpId() != empId)
        ++index;

    if (index == employees.size())
    {
        // No matching employee was found in the specified company/department.
        // so do nothing
        return;
    }

    employees.erase(employees.begin() + index);
}
@cire thank you for your help I will try it as soon as possible when i get back to my computer, I hope it will do.
@cire it seems not working, or maybe i am doing it wrong i just copy pasted your function and called that function on my edit function passing those parameters, am I doing it wrong?
Topic archived. No new replies allowed.