-1

I have a vector composed of strings, some strings have single words, some have multiple words, some have numbers, etc. I have a code that deletes elements of the vector IF the entire string is one specific word ("event") that works perfectly:

for (int j = 0; j< myvec.size()-1; j++) {

    if(myvec[j] == "<event>") {  //erase all instances of "<event>"
        myvec.erase(myvec.begin()+j);
    }
}

However, now I need to delete a few elements in the vector that only START with a word (these all have differing junk after that first key word "wgt"

I have no idea how to get this working. I'm assuming it will be something similar to the above for/if loop, I just don't know how to make the if statement only look at the first word in the string.

Any ideas?

Thanks in advance!

khfrekek
  • 211
  • 1
  • 5
  • 12

1 Answers1

4

The first line places string beginning with 'abc' at the end, and the second erases them from the vector.

auto end_it = std::remove_if(myvec.begin(), myvec.end(), 
                   [](const string &str){ 
                     return str.find("abc") == 0 ;}) ;
myvec.erase(end_it, myvec.end()) ;
marom
  • 5,064
  • 10
  • 14