STL学习笔记(移除性算法)

本节所列的算法是根据元素值或某一准则,在一个区间内移除某些元素。

这些算法并不能改变元素的数量,它们只是将原本置于后面的“不移除元素”向前移动,覆盖那些被移除的元素。

这些算法都返回逻辑上的新终点

 

移除某些特定元素

1.移除某序列内的元素

ForwardIterator

remove(ForwardIterator beg,ForwardIterator end,

             const T& value)

ForwardIterator

remove_if(ForwardIterator beg,ForwardIterator end,

                 UnaryPredicate op)

1.remove()会移除区间[beg,end)中每一个“与value相等”的元素

2.remove_if()会移除区间[beg,end)中每一个“令以下一元判断式”: op(elem) 为true的元素。

 

下面程序示范remove()和remove_if()的用法:

 1 #include "algostuff.hpp"
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     vector<int> coll;
 7     INSERT_ELEMENTS(coll,2,6);
 8     INSERT_ELEMENTS(coll,4,9);
 9     INSERT_ELEMENTS(coll,1,7);
10     PRINT_ELEMENTS(coll,"coll:     ");
11     vector<int>::iterator pos;
12     pos=remove(coll.begin(),coll.end(),5);
13     PRINT_ELEMENTS(coll,"size not changed: ");
14     coll.erase(pos,coll.end());
15     PRINT_ELEMENTS(coll,"size changed: ");
16     coll.erase(remove_if(coll.begin(),coll.end(),
17                          bind2nd(less<int>(),4)),coll.end());
18     PRINT_ELEMENTS(coll,"< 4 removed: ");
19 }
View Code

 

2.复制时一并移除元素

OutputIterator

remove_copy(InputIterator sourceBeg,InputIterator sourceEnd,

                      OutputIterator destBeg,

                      const T& value)

OutputIterator

remove_copy_if(InputIterator sourceBeg,InputIterator sourceEnd,

                          OutputIterator destBeg,

                          UnaryPredicate op)

1.remove_copy()是copy()和remove()组合。它将源区间[beg,end)内的所有元素赋值到“以destBeg为起点”的目标区间内。

   并在复制过程中移除“与value相等”的所有元素

2.remove_copy_if是copy()和remove_if()的组合

 

以下程序示范remove_copy()和remove_copy_if的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     list<int> coll1;
 8     INSERT_ELEMENTS(coll1,1,6);
 9     INSERT_ELEMENTS(coll1,1,9);
10     PRINT_ELEMENTS(coll1);
11     remove_copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "),3);
12     cout<<endl;
13     remove_copy_if(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "),bind2nd(greater<int>(),4));
14     cout<<endl;
15     multiset<int> coll2;
16     remove_copy_if(coll1.begin(),coll1.end(),inserter(coll2,coll2.end()),bind2nd(less<int>(),4));
17     PRINT_ELEMENTS(coll2);
18 }
View Code

 

 

移除重复元素

1.移除连续重复元素

ForwardIterator

unique(ForwardIterator beg,ForwardIterator end)

ForwardIterator

unique(ForwardIterator beg,ForwardIterator end

            BinaryPredicate op)

1.以上两种形式都会移除连续重复元素中的多余元素

2.第一形式将区间[beg,end)内所有“与前一元素相等的元素“移除。

3.第二形式将每一个”位于元素e之后并且造成以下二元判断式:op(elem,e)结果为true”的所有elem元素移除。

   换言之此判断式并非用来将元素和其原本的前一元素比较,而是将它和未被移除的前一元素比较。

 

下面程序示范unique()的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int source[]={1,4,4,6,1,2,2,3,1,6,6,6,5,7,5,4,4};
 8     int sourceNum=sizeof(source)/sizeof(source[0]);
 9     list<int> coll;
10     copy(source,source+sourceNum,back_inserter(coll));
11     PRINT_ELEMENTS(coll);
12     list<int>::iterator pos;
13     pos=unique(coll.begin(),coll.end());
14     copy(coll.begin(),pos,ostream_iterator<int>(cout," "));
15     cout<<endl<<endl;
16     copy(source,source+sourceNum,coll.begin());
17     PRINT_ELEMENTS(coll);
18     coll.erase(unique(coll.begin(),coll.end(),greater<int>()),coll.end());
19     PRINT_ELEMENTS(coll);
20 }
View Code

 

2.复制过程中移除重复元素

OutputIterator

unique_copy(InputIterator sourceBeg,InputIterator sourceEnd,

                     OutputIterator destBeg)

OutputIterator

unique_copy(InputIterator sourceBeg,InputIterator sourceEnd,

                     OutputIterator destBeg,

                     BinaryPredicate op)

两种形式都是copy()和unique()的组合

 

下面程序示范unique_copy()的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 bool differenceOne(int elem1,int elem2)
 6 {
 7     return elem1+1==elem2||elem1-1==elem2;
 8 }
 9 
10 int main()
11 {
12     int source[]={1,4,4,6,1,2,2,3,1,6,6,6,5,7,5,4,4};
13     int sourceNum=sizeof(source)/sizeof(source[0]);
14     list<int> coll;
15     copy(source,source+sourceNum,back_inserter(coll));
16     PRINT_ELEMENTS(coll);
17     unique_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
18     cout<<endl;
19     unique_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),differenceOne);
20     cout<<endl;
21 }
View Code

 

posted @ 2015-09-29 12:57  Runnyu  阅读(199)  评论(0编辑  收藏  举报