Can someone explain in English what is going on here?
std::vector<Cat*> cats; //I get that cats is a vector of Cat objects
if (std::find(cats.begin(), cats.end(), morris) == cats.end()) {
cats.push_back(morris);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
@mlimber has already given one explanation.
I’d explain it a bit differently. In plain English, it’s a way of taking something really simple:
and making it slower (linear instead of logarithmic) and considerably harder to read or understand.
Edit: In fairness, I suppose I should add that there are a few reasons you might want to do something like this. For example, if you really need to know the order in which
Cats were added to the collection, preserving the original order might make some sense. Likewise, if you’re usually using the collection in a way that benefits from them being contiguous in memory, and only rarely adding a new item, it might make more sense to store the data in avectorthan anset.A
set, however, is designed to do exactly what’s being done here, so asetis the obvious choice (absent compelling reasons to use avectorthat just aren’t visible in what you’ve shown).