Removing SPECIFIED and not duplicates in Vectors

1
2
3
4
5
6
7
8
9
10
for (unsigned int i = 0; i < CUSTOMER_NAME.size(); i++) {
				if (CUSTOMER_REMOVE == CUSTOMER_NAME.at(i)) {	CUSTOMER_NAME.erase(remove(CUSTOMER_NAME.begin(), CUSTOMER_NAME.end(), CUSTOMER_NAME[i]), CUSTOMER_NAME.end());
					CUSTOMER_ADDRESS.erase(remove(CUSTOMER_ADDRESS.begin(), CUSTOMER_ADDRESS.end(), CUSTOMER_ADDRESS[i]) , CUSTOMER_ADDRESS.end());
					BRAND_CUSTOMER.erase(remove(BRAND_CUSTOMER.begin(), BRAND_CUSTOMER.end(), BRAND_CUSTOMER[i]), BRAND_CUSTOMER.end());
					MODEL_CUSTOMER.erase(remove(MODEL_CUSTOMER.begin(), MODEL_CUSTOMER.end(), MODEL_CUSTOMER[i]), MODEL_CUSTOMER.end());
					TYPE_CUSTOMER_CAR.erase(remove(TYPE_CUSTOMER_CAR.begin(), TYPE_CUSTOMER_CAR.end(), TYPE_CUSTOMER_CAR[i]), TYPE_CUSTOMER_CAR.end());
					PRICE_CUSTOMER.erase(remove(PRICE_CUSTOMER.begin(), PRICE_CUSTOMER.end(), PRICE_CUSTOMER[i]), PRICE_CUSTOMER.end());
					break;
				}
			}


It's working as it should be, but whenever there's a duplicate and try to view it, I'll get the error: "_DEBUG_ERROR("vector subscript out of range");"
I just want it to remove the specified name I inputted.
PS:
I'm using Visual Studio 2017 just for additional information
Last edited on
> but whenever there's a duplicate and try to view it, I'll get the error

std::remove(), and hence the erase-remove idiom, erases all elements which compare equal to the value passed as the third argument to std::remove(). If there are duplicates, those duplicates would also be erased.
Oh, seems like I am dumb. Lol, thanks for pointing that out. should just have used .erase(.begin() + i) . I made it complicated lol
Consider aggregating all the information for one customer in a struct and then having just a single vector of that customer struct.
The title of your post states you want to remove specified but not duplicates though it does not mention how the removal criterion is determined.
The following program removes only the first occurrence of an element, if found, and not any further duplicates. However it swaps any found element with the back element of the vector as it's easier to erase from the back of the vector and so the order of the original vector is not retained:
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
32
33
34
# include <iostream>
# include <vector>
# include <algorithm>
# include <string>

struct Person
{
    std::string m_name;
    Person(const std::string& name)
    : m_name(name){}
};

int main()
{
    std::vector<Person> folks{};
    folks.emplace_back("John");
    folks.emplace_back("Jane");
    folks.emplace_back("Jack");
    folks.emplace_back("John");
    folks.emplace_back("Jill");

    for (const auto& elem : folks)std::cout << elem.m_name << " "; std::cout << "\n";
    //John Jane Jack John Jill

    auto itr = std::find_if(folks.begin(), folks.end(), [](const Person& p){return p.m_name == "John";});

    if (itr != folks.end())
    {
        std::iter_swap(itr, --folks.end());
        folks.erase(--folks.end());
    }
    for (const auto& elem : folks)std::cout << elem.m_name << " ";
    //Jill Jane Jack John
}

you could use reverse iterators to find the last occurrence if required as well
Topic archived. No new replies allowed.