Hey guys, how you doing this morning?
Well, I've been trying to expand my horizons and use functions I've never used before, and today I decided to give erase() a shot. Well, after battling with it for awhile (guess who passed it as a const vector? This idiot right here!) I managed to get rid of compiler errors.
The long and short of it is I'm not supposed to print repeat words, BUT ALSO not supposed to change the order in which the words were received. Fair enough. I'll just give relevant code (since input is a standard for with a string control on cin and print is your standard range-for):
1 2 3 4 5 6 7 8 9 10
|
void remove_repeats(std::vector<std::string>& vs) //Helper to exercise 11
{
for (auto i = vs.begin(); i != vs.end(); ++i) {
if (i != vs.end() - 1) {
for (auto j = i + 1; j != vs.end(); ++j) {
if (*i == *j) vs.erase(j);
}
}
}
}
|
Now I realize I have a TON of options, not least of which is to just push_back words that aren't repeated into a new vector and return that, but that doesn't seem ideal, and again, trying new things. If I don't repeat words (and thus erase isn't called) I have no problems, but if I do, I get a neat run-time error about incrememnting iterators, and that is also noteworthy because without the unary * it refuses to remove elements at all and repeats words, but with it I get the error.
Any pointers here would be great. I'm kind of at a loss. I'm also wondering if I couldn't just put the first iteration in a range-for but... baby steps.