因为STL的通用性,对删除的概念有一点限制
通过迭代器 删除 元素
而迭代器可以指向数组、vector、list等
那么试图销毁正在被删除的元素和改变输入范围的大小是不安全或是
不合理的
还有Generators.h PrintSequence.h不贴了
//: C06:Removing.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// The removing algorithms.
//{L} Generators
#include <algorithm>
#include <cctype>
#include <string>
#include "Generators.h"
#include "PrintSequence.h"
using namespace std;
struct IsUpper {
bool operator()(char c) { return isupper(c); }
};
int main() {
string v;
v.resize(25);
generate(v.begin(), v.end(), CharGen());
print(v.begin(), v.end(), "v original", "");
// Create a set of the characters in v:
string us(v.begin(), v.end());
sort(us.begin(), us.end());
string::iterator it = us.begin(), cit = v.end(),
uend = unique(us.begin(), us.end());
// Step through and remove everything:
while(it != uend) {
cit = remove(v.begin(), cit, *it);
print(v.begin(), v.end(), "Complete v", "");
print(v.begin(), cit, "Pseudo v ", " ");
cout << "Removed element:\t" << *it
<< "\nPsuedo Last Element:\t"
<< *cit << endl << endl;
++it;
}
generate(v.begin(), v.end(), CharGen());
print(v.begin(), v.end(), "v", "");
cit = remove_if(v.begin(), v.end(), IsUpper());
print(v.begin(), cit, "v after remove_if IsUpper", " ");
// Copying versions are not shown for remove()
// and remove_if().
sort(v.begin(), cit);
print(v.begin(), cit, "sorted", " ");
string v2;
v2.resize(cit - v.begin());
unique_copy(v.begin(), cit, v2.begin());
print(v2.begin(), v2.end(), "unique_copy", " ");
// Same behavior:
cit = unique(v.begin(), cit, equal_to<char>());
print(v.begin(), cit, "unique equal_to<char>", " ");
getchar();
} ///:~
输出
v original: pHqghUmeaYlNlfdXfircvsCxg
Complete v: pHqghUmeaYlNlfdXfircvsxgg
Pseudo v : p H q g h U m e a Y l N l f d X f i r c v s x g
Removed element: C
Psuedo Last Element: g
Complete v: pqghUmeaYlNlfdXfircvsxggg
Pseudo v : p q g h U m e a Y l N l f d X f i r c v s x g
Removed element: H
Psuedo Last Element: g
Complete v: pqghUmeaYllfdXfircvsxgggg
Pseudo v : p q g h U m e a Y l l f d X f i r c v s x g
Removed element: N
Psuedo Last Element: g
Complete v: pqghmeaYllfdXfircvsxggggg
Pseudo v : p q g h m e a Y l l f d X f i r c v s x g
Removed element: U
Psuedo Last Element: g
Complete v: pqghmeaYllfdfircvsxgggggg
Pseudo v : p q g h m e a Y l l f d f i r c v s x g
Removed element: X
Psuedo Last Element: g
Complete v: pqghmeallfdfircvsxggggggg
Pseudo v : p q g h m e a l l f d f i r c v s x g
Removed element: Y
Psuedo Last Element: g
Complete v: pqghmellfdfircvsxgggggggg
Pseudo v : p q g h m e l l f d f i r c v s x g
Removed element: a
Psuedo Last Element: g
Complete v: pqghmellfdfirvsxggggggggg
Pseudo v : p q g h m e l l f d f i r v s x g
Removed element: c
Psuedo Last Element: g
Complete v: pqghmellffirvsxgggggggggg
Pseudo v : p q g h m e l l f f i r v s x g
Removed element: d
Psuedo Last Element: g
Complete v: pqghmllffirvsxggggggggggg
Pseudo v : p q g h m l l f f i r v s x g
Removed element: e
Psuedo Last Element: g
Complete v: pqghmllirvsxgxggggggggggg
Pseudo v : p q g h m l l i r v s x g
Removed element: f
Psuedo Last Element: x
Complete v: pqhmllirvsxxgxggggggggggg
Pseudo v : p q h m l l i r v s x
Removed element: g
Psuedo Last Element: x
Complete v: pqmllirvsxxxgxggggggggggg
Pseudo v : p q m l l i r v s x
Removed element: h
Psuedo Last Element: x
Complete v: pqmllrvsxxxxgxggggggggggg
Pseudo v : p q m l l r v s x
Removed element: i
Psuedo Last Element: x
Complete v: pqmrvsxsxxxxgxggggggggggg
Pseudo v : p q m r v s x
Removed element: l
Psuedo Last Element: s
Complete v: pqrvsxxsxxxxgxggggggggggg
Pseudo v : p q r v s x
Removed element: m
Psuedo Last Element: x
Complete v: qrvsxxxsxxxxgxggggggggggg
Pseudo v : q r v s x
Removed element: p
Psuedo Last Element: x
Complete v: rvsxxxxsxxxxgxggggggggggg
Pseudo v : r v s x
Removed element: q
Psuedo Last Element: x
Complete v: vsxxxxxsxxxxgxggggggggggg
Pseudo v : v s x
Removed element: r
Psuedo Last Element: x
Complete v: vxxxxxxsxxxxgxggggggggggg
Pseudo v : v x
Removed element: s
Psuedo Last Element: x
Complete v: xxxxxxxsxxxxgxggggggggggg
Pseudo v : x
Removed element: v
Psuedo Last Element: x
Complete v: xxxxxxxsxxxxgxggggggggggg
Pseudo v :
Removed element: x
Psuedo Last Element: x
v: GBwKfnqDuxwfnfOzvSRtkJPRE
v after remove_if IsUpper: w f n q u x w f n f z v t k
sorted: f f f k n n q t u v w w x z
unique_copy: f k n q t u v w x z
unique equal_to<char>: f k n q t u v w x z
字符串v是一个由随机产生的字符填满的字符容器
每个字符在remove语句都被使用
但是每次都显示全部的字符串v
因此在得到结束点以后
就可以看到该范围的剩余部分到底发生了什么变化