Regarding this topic I have read that the Erase-remove idiom is the way to go. I have a rough understanding of how this works but am unsure whether I can implement a match-counter along with it.
For counting alone, I would use something like this:
Code:
std::vector<std::string> v; // contains duplicate strings in different elements
std::string term = "foo"; // search term, changing at runtime as well
unsigned int matches = 0;
for( auto e : v ) {
if( e == term ) {
++matches;
}
}
The Erase-remove approach is being demonstrated like this:
Code:
bool is_odd( int var ) {
return ( var % 2 ) != 0;
}
std::vector<int> v;
v.erase( std::remove_if( v.begin(), v.end(), is_odd ), v.end() );
Or using a lambda:
Code:
std::vector<int> v;
v.erase( std::remove_if( v.begin(), v.end(), []( int var ) {
return( var % 2 ) != 0;
}), v.end() );
I'm not sure how (or if) I can combine the two things. That is, I don't know how to integrate a function comparing two (changing) strings into the remove_if() method. Nor do I know how to increment a counter during iteration.
The vector is extremely large, so speed is paramount. I think there are many other avenues for optimization, but decreasing the vector's size for each consecutive search could deliver a big speed boost for subsequent searches I imagine, as traversing it holds the biggest cost.