我的目标是尝试解决这个问题:Removing all empty elements in a vector from end。使用擦除删除成语。
std::vector<std::string>
这个想法是从给定的字符串中删除从末尾开始的所有空元素(等于空白) 。当找到非空元素时,应停止删除元素。
例子:
vec = { " ", "B", " ", "D", "E", " ", " ", " " };
拆除后:
vec = { " ", "B", " ", "D", "E"};
这是我尝试的解决方案:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
int main()
{
std::vector<std::string> vec = { " ", "B", " ", "D", "E", " ", " ", " " };
bool notStop = true;
auto removeSpaceFromLast = [&](const std::string& element)-> bool
{
if(element != " " ) notStop = false;
return ( (element == " ") && (notStop) );
};
vec.erase(
std::remove_if(vec.rbegin(), vec.rend(), removeSpaceFromLast),
vec.rend() );
std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout,","));
return 0;
}
这给了我一个错误:
no matching function for call to std::vector<std::__cxx11::basic_string<char> >::erase(std::reverse_iterator<__gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > > >, std::vector<std::__cxx11::basic_string<char> >::reverse_iterator)'|
然后我读到std::vector::erase()
这里的工作:Does vector::erase not work with reverse iterators?
并更改了代码:
vec.erase(
std::remove_if(vec.rbegin().base(), vec.rend().base(), removeSpaceFromLast),
vec.rend().base() );
这次它编译了,但给了我输出=原始向量。
任何人都可以解释:
- 为什么会这样?
- 如果可能,我们该如何解决?