Given a std::vector
of strings, what is the best way of removing all elements starting from the end that are empty (equal to empty string or whitespace). The removal of elements should stop when a non-empty element is found.
My current method, (work in progress) is something like:
while (Vec.size() > 0 && (Vec.back().size() == 0 || is_whitespace(Vec.back()))
{
Vec.pop_back();
}
where is_whitespace
returns a bool stating if a string is whitespace or not
I suspect that my method will resize the vector at each iteration and that is suboptimal. Maybe with some algorithm it is possible to do in one step.
Input: { "A", "B", " ", "D", "E", " ", "", " " }
Desired Output: { "A", "B", " ", "D", "E" }