STL学习笔记(变动性算法)

本节描述的算法会变动区间内的元素内容。有两种方法可以变动元素内容:

1.运用迭代器遍历序列的过程中,直接加以变动

2.将元素从源区间赋值到目标区间的过程中加以变动

 

复制(copy)元素

OutputIterator

copy(InputIterator sourceBeg,

        InputIterator sourceEnd,

        OutputIterator destBeg)

BiderectionalIterator

copy_backward(BidirectionalIterator sourceBeg,

                          BidirectionalIterator sourceEnd,

                          BidirectionalIterator destEnd)

1.这两个算法都将源区间[sourceBeg,sourceEnd)中的所有元素赋值到以destBeg为起点或以destEnd为终点的目标区间去

2.返回目标区间内最后一个被赋值元素的下一位置,也就是第一个违背覆盖的元素的位置

3.destBeg或destEnd不可处于[sourceBeg,sourceEnd)区间内

 

下面的例子展示copy()的一些简单用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     vector<int> coll1;
 8     list<int> coll2;
 9     INSERT_ELEMENTS(coll1,1,9);
10     copy(coll1.begin(),coll1.end(),back_inserter(coll2));
11     copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
12     cout<<endl;
13     copy(coll1.rbegin(),coll1.rend(),coll2.begin());
14     copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
15     cout<<endl;
16 }
View Code

 

 

转换(Transforming)和结合(Combining)元素

算法transform()提供一下两种能力:

1.转换元素

OutputIterator

transform(InputIterator sourceBeg,InputIterator sourceEnd,

                OutputIterator destBeg,UnaryFunc op)

针对源区间[sourceBeg,sourceEnd)中的每一个元素调用 op(elem),并将结果写到以destBeg起始的目标区间内。

下面这个例子展示了transform()的转换元素功能

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     vector<int> coll1;
 8     list<int> coll2;
 9     INSERT_ELEMENTS(coll1,1,9);
10     PRINT_ELEMENTS(coll1,"coll1: ");
11     transform(coll1.begin(),coll1.end(),coll1.begin(),negate<int>());
12     PRINT_ELEMENTS(coll1,"negated: ");
13     transform(coll1.begin(),coll1.end(),back_inserter(coll2),bind2nd(multiplies<int>(),10));
14     PRINT_ELEMENTS(coll2,"coll2: ");
15     transform(coll2.rbegin(),coll2.rend(),ostream_iterator<int>(cout," "),negate<int>());
16     cout<<endl;
17 }
View Code

2.将两序列的元素加以结合

OutputIterator

transform(InputIterator source1Beg,InputIterator source1End,

                 InputIterator source2Beg,

                 OutputIterator destBeg,

                 BinaryFunc op)

针对第一源区间[source1Beg,source1End)以及“从source2Beg开始的第二个源区间“对应的元素,调用:

op(source1Elem,source2Elem) 并将结果以destBeg起始的目标区间内。

下面这个例子展示以上所说的transform()用法:

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     vector<int> coll1;
 8     list<int> coll2;
 9     INSERT_ELEMENTS(coll1,1,9);
10     PRINT_ELEMENTS(coll1,"coll1: ");
11     transform(coll1.begin(),coll1.end(),coll1.begin(),coll1.begin(),multiplies<int>());
12     PRINT_ELEMENTS(coll1,"squared: ");
13     transform(coll1.begin(),coll1.end(),coll1.rbegin(),back_inserter(coll2),plus<int>());
14     PRINT_ELEMENTS(coll2,"coll2: ");
15     cout<<"diff: ";
16     transform(coll1.begin(),coll1.end(),coll2.begin(),ostream_iterator<int>(cout," "),minus<int>());
17     cout<<endl;
18 }
View Code

 

 

互换(Swapping)元素内容

ForwardIterator

swap_ranges(ForwardIterator beg1,ForwardIterator end1,

                      ForwardIterator beg2)

1.强区间[beg1,end1)内的元素和”从beg2开始的区间“内的对应元素互换

2.返回第二区间中”最后一个被交换元素“的下一位置

 

下面这个例子展示swap_ranges()的用法

 1 #include "algostuff.hpp"
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     vector<int> coll1;
 7     deque<int> coll2;
 8     INSERT_ELEMENTS(coll1,1,9);
 9     INSERT_ELEMENTS(coll2,11,23);
10     PRINT_ELEMENTS(coll1,"coll1: ");
11     PRINT_ELEMENTS(coll2,"coll2: ");
12     deque<int>::iterator pos;
13     pos=swap_ranges(coll1.begin(),coll1.end(),coll2.begin());
14     PRINT_ELEMENTS(coll1,"\ncoll1: ");
15     PRINT_ELEMENTS(coll2,"coll2: ");
16     if(pos!=coll2.end())
17         cout<<"first element not modified: "<<*pos<<endl;
18     swap_ranges(coll2.begin(),coll2.begin()+3,coll2.rbegin());
19     PRINT_ELEMENTS(coll2,"\ncoll2:");
20 }
View Code

 

 

赋予(Assigning)新值

1.赋予完全相同的数值

void

fill(ForwardIterator beg,ForwardIterator end,

     const T& newValue)

void

fill(ForwardIterator beg,Size num,

     const T& newValue)

1.fill()将区间[beg,end)内的每一个元素都赋予新值newValue

2.fill_n()将”从beg开始的前num个元素“赋予新值newValue

3.调用者必须确保目标区间有足够空间,要不就得用插入行迭代器

 

以下程序展示fill()和fill_n()的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     fill_n(ostream_iterator<float>(cout," "),10,7.7);
 8     cout<<endl;
 9     list<string> coll;
10     fill_n(back_inserter(coll),9,"hello");
11     PRINT_ELEMENTS(coll,"coll: ");
12     fill(coll.begin(),coll.end(),"again");
13     PRINT_ELEMENTS(coll,"coll: ");
14     fill_n(coll.begin(),coll.size()-2,"hi");
15     PRINT_ELEMENTS(coll,"coll: ");
16     list<string>::iterator pos1,pos2;
17     pos1=coll.begin();
18     pos2=coll.end();
19     fill(++pos1,--pos2,"hmmm");
20     PRINT_ELEMENTS(coll,"coll: ");
21 }
View Code

 

2.赋予新产生的数值

void 

generate(ForwardIterator beg,ForwardIterator end,

               Func op)

void

generate_n(OutputIterator beg,Size num,

                   Func op)

1.generate()会调用以下动作:op() ,并赋值给区间[beg,end)内的每个元素

2.generate_n()会调用以下动作: op(),并赋值给”以beg起始的区间“内的前num个元素

 

以下程序展示如何利用generate()和generatr_n()安插和赋值一些随机数

 1 #include <cstdlib>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     list<int> coll;
 8     generate_n(back_inserter(coll),5,rand);
 9     PRINT_ELEMENTS(coll);
10     generate(coll.begin(),coll.end(),rand);
11     PRINT_ELEMENTS(coll);
12 }
View Code

 

 

替换(Replacing)元素

1.替换序列内的元素

void

replace(ForwardIterator beg,ForwardIterator end,

             const T& oldValue,const T& newValue)

void

replace_if(ForwardIterator beg,ForwardIterator end,

                 UnaryPredicate op,const T& newValue)

1.replace()将区间[beg,end)之内每一个”与oldValue相等“的元素替换成newValue

2.replace_if()将区间[beg,end)之内每一个以下一元判断式:op(elem)返回true的元素替换成newValue

 

以下程序示范replace()和replace_if()的用法

 1 #include "algostuff.hpp"
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     list<int> coll;
 7     INSERT_ELEMENTS(coll,2,7);
 8     INSERT_ELEMENTS(coll,4,9);
 9     PRINT_ELEMENTS(coll,"coll: ");
10     replace(coll.begin(),coll.end(),6,42);
11     PRINT_ELEMENTS(coll,"coll: ");
12     replace_if(coll.begin(),coll.end(),bind2nd(less<int>(),5),0);
13     PRINT_ELEMENTS(coll,"coll: ");
14 }
View Code

 

2.复制并替换元素

OutputIterator

replace_copy(InputIterator sourceBeg,InputIterator sourceEnd,

                     OutputIterator destBeg,

                     const T& oldValud,const T& newValue)

OutputIterator

replace_copy_if(InputIterator sourceBeg,InputIterator sourceEnd,

                          OutputIterator destBeg,

                          UnaryPredicate op,const T& newValue)

1.replace_copy()是copy()和replace()的组合。他将源区间[beg,end)中的元素赋值到”以destBeg为起点“的目标区间

   同时将其中”与oldValue相等“的所有元素替换为newValue

2.replace_copy()是copy()和replace_if()的组合。

 

以下程序示范如何使用replace_copy()和replace_copy_if()

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     list<int> coll;
 8     INSERT_ELEMENTS(coll,2,6);
 9     INSERT_ELEMENTS(coll,4,9);
10     PRINT_ELEMENTS(coll);
11     replace_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),5,55);
12     cout<<endl;
13     replace_copy_if(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),bind2nd(less<int>(),5),0);
14     cout<<endl;
15 }
View Code

 

posted @ 2015-09-28 21:25  Runnyu  阅读(202)  评论(0编辑  收藏  举报