PLEASE HELP: VECTOR ERASE FUNCTION

So I have a struct called CompanyInfo.
1
2
3
4
5
6
7
struct companyInfo{
        string companyName; // Name of the Company
        products;   // Company's products 
        vector<string> VecProducts;// number of products in unknown, so putting them in a vector
};
companyInfo myNewComp;
vector<companyInfo>myVec; // All the information goes here 


Then When user calls the Add function, I ask for Name of company and ask for its products. And store everything in a vector

1
2
3
4
5
6
7
8
9
10
11
void Add(){
   cout << " Company Name: ";
   cin >> myNewComp.companyName;   // User can enter "samsung, apple, sony etc."

   do{
   cout << " Product: "
   cin >> myNewComp.products;     // User can enter "cellphones, tvs, camera etc."
   myNewComp.VecProducts.push_back(myNewComp.products);  
   myVec.push_back(myNewComp);
     } while(products != "zzz")  // stop loop after user hits "zzz"
}


So everything is fine until this point. Now lets says that the user enter adds 3 companies and its products.
Samsung (products: TVs, camera, smartphones)
Apple (products: smartphones, tablets, laptops)
Sony (products: camera, TVs, headphones)

Now in the Search() function, I ask the user to enter a product he is looking for, and in the vector, I keep only those companies that sell this product. So if user is looking for camera, I delete Apple and its products and keep only Samsung and Sony.
HOW DO I THEN DO THAT? I DON'T KNOW HOW TO DELETE 'APPLE AND ITS PRODUCTS'
Last edited on
I FORGOT TO MENTION UP THERE, I TRIED THIS CODE BUT IT DOESN'T WORK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string lookingFor; 
cout << " Enter what you're looking for: ";
 cin >> lookingFor;

    for (int i=0; i<myVec.size(); i++)
    {
      
        for (int j=0; j< myVec[i].VecProducts.size(); j++)
        {
       
            if (lookingFor != myVec[i].VecProducts[j])
            {
                myVec[i].VecAttribute.erase(myVec[i].VecAttribute.begin(), myVec[i].VecAttribute.begin()+j);
              // THIS DOESN'T WORK - HOW CAN I FIX THIS ?
            }
        }
        
    }
1. Do not shout. (All caps is shouting.)

2. If you do have a std::vector<int> foo;, how would you delete the third element of foo?
@ Keskiverto,
Sorry for the caps.
Now, I know I how to delete a specific element in vector. But in this case, I am deleting entire struct and a vector of products inside the bigger vector and so I am very confused. Can you please give me some code or pseudocode please?

I tried the nested for loops but they don't seem to work.
Do you want to

1. Delete a specific element from a vector

or

2. Remove members from a struct
@ keskiverto,
Either of them could work. As long as in myVec, I have only the companies that sell the products user is looking for. If user is looking for camera, I don't want Apple and its products in myVec. I want only Samsung and Sony (and their products) in myVec.
Its not either. It is the first.

Therefore again, if you do have a std::vector<companyInfo> foo;, how would you delete the third element of foo?
@ keskiverto,
So I go though myVec, then I look at every single products in VecProducts and none of them match with what the user is looking for, then I delete that.
Below is the code I used but it doesn't seem to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string lookingFor; 
cout << " Enter what you're looking for: ";
 cin >> lookingFor;
bool found = false;
    for (int i=0; i<myVec.size(); i++)
    {      
        for (int j=0; j< myVec[i].VecProducts.size(); j++)
        {       
            if (lookingFor == myVec[i].VecProducts[j])
            {
               found = true;
               break;              
            }
        }
     if (!found)
   {
      myVec[i].VecAttribute.erase(myVec[i].VecAttribute.begin() + i ;
    }        
 }
Last edited on
The erase invalidates iterators. When you erase an element, your for loop will still increment the i.

There is remove-erase idiom for these situations.


http://www.cplusplus.com/reference/algorithm/remove_if/
http://www.cplusplus.com/reference/algorithm/find/

1
2
3
4
5
string key; 
cin >> key;
auto start = remove_if( begin(myVec), end(myVec), [key = key](const auto & com) {
  return end(com.VecProducts) == find( begin(com.VecProducts), end(com.VecProducts), key); } );
myVec.erase( start, end(myVec) );
Topic archived. No new replies allowed.