distance
很多时候我们希望在一个 vector ,或者 list ,或者什么其他东西里面,找到一个值在哪个位置,这个时候 find 帮不上忙,而有人就转而求助手写循环了,而且是原始的手写循环:
1 for ( int i = 0; i < vect.size(); ++i)
2 if ( vect[i] == value ) break;
2 if ( vect[i] == value ) break;
1 int dist = distance(col.begin(), find(col.begin(), col.end(), 5));
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);
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());
min_element(col.begin(), col.end());
*max_element(col.begin(), col.end());
*min_element(col.begin(), col.end());
*min_element(col.begin(), col.end());
*max_element(col.begin(), col.end(), greater<int>()); // 返回最小值!
*min_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 Person
11 {
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 }
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 Person
11 {
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 }
-------------------------------------------------------------------------
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 }
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 }
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 }
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 else
15 ++iter;
16
17 std::copy(lst.begin(), lst.end(),std::ostream_iterator<int>(std::cout, " "));
18 }
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 else
15 ++iter;
16
17 std::copy(lst.begin(), lst.end(),std::ostream_iterator<int>(std::cout, " "));
18 }
-------------------------------------------------------------------------
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 }
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 }
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 {
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 {
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 }
正确方法
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 }
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 }