###### C++ STL 某些用法

distance

1 for ( int i = 0; i < vect.size(); ++i)
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);

--------------------------------------------------------------------------
max, min

max_element(col.begin(), col.end());
min_element(col.begin(), col.end());

*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 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

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 }

------------------------------------------------------------------------

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 }

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 }

-------------------------------------------------------------------------
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 }

Sometimes,我们需要手写循环(相对于for_each)来erase容器内某些元素，新手经常会犯一些错误。这里总结一下比较常用的固定写法。(删除所有偶数项，并打印出删除的项)

1. vector/queue

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 }

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 }

#### c++ STL中栈stack的用法

2013-10-26 16:53:38

#### stl中hash_set的使用

2015-05-20 09:24:39

#### C++中STL用法总结

2017-01-11 22:40:13

#### 【C++ STL模板之queue队列的用法】

2014-04-28 22:51:37

#### C++ STL优先队列常用用法

2016-06-26 13:08:24

#### c++ STL的list用法总结

2017-04-21 22:02:50

#### c++ stl栈容器stack用法介绍

2017-03-27 08:49:44

#### C++ STL equal_range的用法

2014-04-05 14:47:23

#### C++ STL set容器常用用法

2016-06-28 10:39:09

#### C++ STL list介绍与使用方法

2016-12-15 13:34:14

C++ STL 某些用法