I am new to C++ and could not find a solution in any post for this.
I have a vector
of string
s and I wish to erase a string
from this vector
if it contains any of these symbols: {'.', '%', '&','(',')', '!', '-', '{', '}'}
.
I am aware of find()
, which only takes one character to search for; however, I want to go through each word in the string vector and erase them if they contain any of these characters. E.g. find('.')
does not suffice.
I have tried multiple routes such as creating a char
vector of all these characters and looping through each one as a find()
parameter. However, this logic is very flawed, as it will cause an abort trap if the vector only has one line with a '.' in it, or leaves some strings with the unwanted character inside.
vector<std::string> lines = {"hello..","Hello...", "hi%", "world","World!"}
vector<char> c = {'.', '%', '&','(',')', '!', '-', '{', '}'};
for (int i=0; i < lines.size(); i++){
for(int j=0; j < c.size(); j++){
if (lines.at(i).find(c.at(j)) != string::npos ){
lines.erase(lines.begin() + i);
}
}
}
I have also tried find_first_of()
inside a loop of vector 'lines', which yields the same result as there above code.
if (lines.at(i).find_first_of(".%&()!-{}") != string::npos ){
lines.erase(lines.begin() + i);
Can someone please help me with this logic?
EDIT:
when I put in --i
after erasing the line, instead nothing is displayed and I have an abort trap because it loops outside vector range.