C++中要从string
中删除全部某个特定字符, 可用以下代码ios
str.erase(std::remove(str.begin(), str.end(), 'a'), str.end());
其中, remove
来自<algorithm>
, 它的签名是函数
template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
做用: 在容器中, 删除[first, last)
之间的全部值等于val
的值.code
删除方法: 将某个等于val
的值用下一个不等于val
的值覆盖.rem
返回值: 一个迭代器 (记做newEnd
), 该迭代器指向最后一个未被删除元素的下一个元素, 即至关于容器新的end
.get
因此, 运行完remove
后, 容器的[first, newEnd)
内的元素即为全部未被删除的元素, [newEnd, end)
之间的元素已经没用了.string
这样, 再运行str.erase(newEnd, str.end())
便可清空[newEnd, end)
之间的元素.it
std::remove_if
与remove
相似, 只是它接受的第三个参数是个函数.io
// remove_if example #include <iostream> // std::cout #include <algorithm> // std::remove_if bool IsOdd (int i) { return ((i%2)==1); } int main () { int myints[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9 // bounds of range: int* pbegin = myints; // ^ int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^ pend = std::remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ? // ^ ^ std::cout << "the range contains:"; for (int* p=pbegin; p!=pend; ++p) std::cout << ' ' << *p; std::cout << '\n'; return 0; }
Output:ast
myvector contains: 10 30 30 10 10 0 0 0
还有个std::remove_copy
, 签名:class
template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& val);
它会把[first, last)
之间不等于val
的元素都向从result
开始的容器拷贝.
// remove_copy example #include <iostream> // std::cout #include <algorithm> // std::remove_copy #include <vector> // std::vector int main () { int myints[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20 std::vector<int> myvector (8); std::remove_copy (myints,myints+8,myvector.begin(),20); // 10 30 30 10 10 0 0 0 std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Output:
myvector contains: 10 30 30 10 10 0 0 0