distance很多时候我们希望在一个 vector ,或者 list ,或者什么其他东西里面,找到一个值在哪个位置,这个时候 find 帮不上忙,而有人就转而求助手写循环了,而且是原始的手写循环:1 for ( int i = 0; i < vect.size(); ++i)2 if ( vect[i] == value ) break; 如果编译器把 i 看作 for scope 的一部分,你还要把 i 的声明拿出去。真的需要这样么?看看这个:1 int dist = distance(col.begin(), find(col.begin(), col.end(), 5)); 其中 col 可以是很多容器,list, vector, deque... 当然这是你确定 5 就在 col 里面的情形,如果你不确定,那就加点判断:1 int dist;2 list<int>::iterator pos = find(col.begin(), col.end(), 5);3 if ( pos != col.end() )4 dist = distance(col.begin(), pos); 我想这还是比手写循环来的好些吧。--------------------------------------------------------------------------max, min这是有直接的算法支持的,当然复杂度是 O(n),用于未排序容器,如果是排序容器...老兄,那还需要什么算法么?max_element(col.begin(), col.end());min_element(col.begin(), col.end()); 注意返回的是 iterator ,如果你关心的只是值,那么好:*max_element(col.begin(), col.end());*min_element(col.begin(), col.end()); max_element 和 min_element 都默认用 less 来排序,它们也都接受一个 binary predicate ,如果你足够无聊,甚至可以把 max_element 当成 min_element 来用,或者反之:*max_element(col.begin(), col.end(), greater<int>()); // 返回最小值!*min_element(col.begin(), col.end(), greater<int>()); // 返回最大值 当然它们的本意不是这个,而是让你能在比较特殊的情况下使用它们,例如,你要比较的是每个元素的某个成员,或者成员函数的返回值。例如: 1 #include <iostream> 2 #include <list> 3 #include <algorithm> 4 #include <string> 5 #include <boost/bind.hpp> 6 7 using namespace boost; 8 using namespace std; 9 10 struct Person11 {12 Person(const string& _name, int _age): name(_name), age(_age) {}13 int age;14 string name;15 };16 17 int main()18 {19 list<Person> col;20 list<Person>::iterator pos;21 22 col.push_back(Person("Tom", 10));23 col.push_back(Person("Jerry", 12));24 col.push_back(Person("Mickey", 9));25 26 Person eldest = *max_element(col.begin(), col.end(),bind(&Person::age, _1) < bind(&Person::age, _2));27 cout << eldest.name;28 } 输出是 Jerry ,这里用了 boost.bind ,原谅我不知道用 bind2nd, mem_fun 怎么写,我也不想知道...-------------------------------------------------------------------------copy_if没错,STL 里面压根没有 copy_if ,这就是为什么我们需要这个: 1 template<typename InputIterator, typename OutputIterator, typename Predicate> 2 OutputIterator copy_if( InputIterator begin, InputIterator end, OutputIterator destBegin, Predicate p) 3 { 4 while (begin != end) 5 { 6 if (p(*begin))*destBegin++ = *begin; 7 ++begin; 8 } 9 return destBegin;10 } 把它放在自己的工具箱里,是一个明智的选择。------------------------------------------------------------------------惯用手法:erase(iter++)如果你要去除一个 list 中的某些元素,那可千万小心:(下面的代码是错的!!!) 1 #include <iostream> 2 #include <algorithm> 3 #include <iterator> 4 #include <list> 5 6 int main() 7 { 8 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 9 std::list<int> lst(arr, arr + 10);10 11 for ( std::list<int>::iterator iter = lst.begin();iter != lst.end(); ++iter)12 if ( *iter % 2 == 0 )13 lst.erase(iter);14 15 std::copy(lst.begin(), lst.end(), std::ostream_iterator<int>(std::cout, " "));16 } 当 iter 被 erase 掉的时候,它已经失效,而后面却还会做 ++iter ,其行为无可预期!如果你不想动用 remove_if ,那么唯一的选择就是: 1 #include <iostream> 2 #include <algorithm> 3 #include <iterator> 4 #include <list> 5 6 int main() 7 { 8 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 9 std::list<int> lst(arr, arr + 10);10 11 for ( std::list<int>::iterator iter = lst.begin(); iter != lst.end(); )12 if ( *iter % 2 == 0 )13 lst.erase(iter++);14 else15 ++iter;16 17 std::copy(lst.begin(), lst.end(),std::ostream_iterator<int>(std::cout, " "));18 } 但是上面的代码不能用于 vector, string 和 deque ,因为对于这些容器, erase 不光令 iter 失效,还令 iter 之后的所有 iterator 失效!-------------------------------------------------------------------------erase(remove...) 惯用手法上面的循环如此难写,如此不通用,如此不容易理解,还是用 STL 算法来的好,但是注意,光 remove_if 是没用的,必须使用 erase(remove...) 惯用手法: 1 #include <iostream> 2 #include <algorithm> 3 #include <iterator> 4 #include <list> 5 #include <functional> 6 #include <boost/bind.hpp> 7 8 int main() 9 {10 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};11 std::list<int> lst(arr, arr + 10);12 13 lst.erase(remove_if(lst.begin(), lst.end(),boost::bind(std::modulus<int>(), _1, 2) == 0),lst.end()); 14 std::copy(lst.begin(), lst.end(),std::ostream_iterator<int>(std::cout, " "));15 } 当然,这里借助了 boost.bind ,让我们不用多写一个没用的 functor 。 Sometimes,我们需要手写循环(相对于for_each)来erase容器内某些元素,新手经常会犯一些错误。这里总结一下比较常用的固定写法。(删除所有偶数项,并打印出删除的项)1. vector/queue正确方法1: 1 void erase(vector<int> &v) 2 { 3 for(vector<int>::iterator vi=v.begin();vi!=v.end();) 4 { 5 if(*vi % 2 == 0) 6 { 7 cout << "Erasing " << *vi << endl; 8 vi = v.erase(vi); 9 }10 else ++vi;11 }12 } 正确方法2: 1 void erase2(vector<int> &v) 2 { 3 for(vector<int>::reverse_iterator ri=v.rbegin();ri!=v.rend();) 4 { 5 if(*ri % 2 == 0) 6 { 7 cout << "Erasing " << *ri << endl; 8 v.erase((++ri).base()); 9 }10 else ++ri;11 }12 } 2.map/list正确方法 1 void erase(map<int,int> &m) 2 { 3 for(map<int,int>::iterator mi=m.begin();mi!=m.end();) 4 { 5 if(mi->second % 2 == 0) 6 { 7 cout << "Erasing " << mi->second << endl; 8 m.erase(mi++); 9 }10 else ++mi;11 }12 }