STL_算法_03_拷贝和替换算法

◆ 常用的拷贝和替换算法:

1.1、复制(容器A(全部/部分) 复制到 容器B(全部/部分)),返回的值==>iteratorOutBegin.end()

iterator copy(iterator1Begin, iterator1End, iteratorOutBegin);

1.2、替换(将指定范围内的所有等于oldValue的元素替换成newValue)(应该是值替换)

void replace(iteratorBegin, iteratorEnd, oldValue, newValue);

1.3、替换(若函数对象返回true,则将对应的元素的oldValue替换成newValue)

void replace_if(iteratorBegin, iteratorEnd, functor满足条件, newValue);

1.4、交换(交换两个容器的元素)(就算两个容器中的值的个数不同,也无需手动设置)

void swap(容器A, 容器B);

1、

1.1、第6讲 PPT.32

◆ copy() :  复制序列

ZC: 只有一种参数格式,返回值是 iterator[ 该值==result容器.end() ]

ZC: vs2010 测试代码:

 1 #ifdef WIN32
 2 #pragma warning (disable: 4786)
 3 #endif
 4 
 5 #include <string>
 6 #include <vector>
 7 #include <set>
 8 
 9 #include <algorithm>    // 算法
10 #include <numeric>    // 算法
11 #include <functional>    // 算法
12 
13 using namespace std;
14 
15 void main()
16 {
17     vector<int> vecIntA;
18     vecIntA.push_back(1);
19     vecIntA.push_back(3);
20     vecIntA.push_back(9);
21     vecIntA.push_back(5);
22     vecIntA.push_back(7);
23 
24     vector<int> vecIntB;
25     vecIntB.resize(5);    //扩大空间 // ZC: 没有这个步骤,VC6编译出来的exe会崩溃
26 
27     vector<int>::iterator itRtn = copy(vecIntA.begin(), vecIntA.end(), vecIntB.begin());    //vecIntB: {1,3,5,7,9}
28 
29     int iIdx = 0;
30     vector<int>::iterator it = vecIntB.begin();
31     while(it != vecIntB.end())
32     {
33         printf("[%02d] ==> %d
", iIdx, *it);
34         it ++;
35         iIdx ++;
36     }
37 
38     vector<int>::iterator itB = vecIntB.end();
39     vector<int>::iterator itA = vecIntA.end();
40 
41     if (itRtn == itA)
42         printf("(1) itRtn == itA
");
43     else
44         printf("(1) itRtn != itA
");
45     if (itRtn == itB)
46         printf("(1) itRtn == itB
");
47     else
48         printf("(1) itRtn != itB
");
49 
50     itRtn --;
51     itB --;
52     itA --;
53 
54     if (itRtn == itA)
55         printf("(2) itRtn == itA : %d, %d
", *itRtn, *itA);
56     else
57         printf("(2) itRtn != itA
");
58     if (itRtn == itB)
59         printf("(2) itRtn == itB : %d, %d
", *itRtn, *itA);
60     else
61         printf("(2) itRtn != itB
");
62 
63     system("pause");
64 }

ZC:控制台输出:

[00] ==> 1
[01] ==> 3
[02] ==> 9
[03] ==> 5
[04] ==> 7
(1) itRtn != itA
(1) itRtn == itB
(2) itRtn != itA
(2) itRtn == itB : 7, 7
请按任意键继续. . .

1.2、第6讲 PPT.34

◆ replace(beg, end, oldValue, newValue) :    将指定范围内的所有等于oldValue的元素替换成newValue。

ZC: 只有一种参数格式,返回值是 void

ZC: VC6 测试代码:

#ifdef WIN32
#pragma warning (disable: 4786)
#endif

#include <string>
#include <vector>
#include <set>

#include <algorithm>    // 算法
#include <numeric>    // 算法
#include <functional>    // 算法

using namespace std;

void main()
{
    vector<int> vecIntA;
    vecIntA.push_back(1);
    vecIntA.push_back(3);
    vecIntA.push_back(5);
    vecIntA.push_back(3);
    vecIntA.push_back(9);

    replace(vecIntA.begin(), vecIntA.end(), 3, 8);        //{1,8,5,8,9}

    int iIdx = 0;
    vector<int>::iterator it = vecIntA.begin();
    while(it != vecIntA.end())
    {
        printf("[%02d] ==> %d
", iIdx, *it);
        it ++;
        iIdx ++;
    }
}

ZC:控制台输出:

1 [00] ==> 1
2 [01] ==> 8
3 [02] ==> 5
4 [03] ==> 8
5 [04] ==> 9
6 Press any key to continue

1.3、第6讲 PPT.35

◆ replace_if() : 将指定范围内所有操作结果为true的元素用新值替换。

ZC: 只有一种参数格式,返回值是 void

ZC: VC6 测试代码:

 1 #ifdef WIN32
 2 #pragma warning (disable: 4786)
 3 #endif
 4 
 5 #include <string>
 6 #include <vector>
 7 #include <set>
 8 
 9 #include <algorithm>    // 算法
10 #include <numeric>    // 算法
11 #include <functional>    // 算法
12 
13 using namespace std;
14 
15 bool GreaterThree(int iNum)
16 {
17     if (iNum >= 3)
18     {
19         return true;
20     }
21     else
22     {
23         return false;
24     }
25 }
26 
27 void main()
28 {
29     //把大于等于3的元素替换成8
30     vector<int> vecIntA;
31     vecIntA.push_back(1);
32     vecIntA.push_back(3);
33     vecIntA.push_back(5);
34     vecIntA.push_back(3);
35     vecIntA.push_back(9);
36 
37     replace_if(vecIntA.begin(), vecIntA.end(), GreaterThree, 8);
38 
39     int iIdx = 0;
40     vector<int>::iterator it = vecIntA.begin();
41     while(it != vecIntA.end())
42     {
43         printf("[%02d] ==> %d
", iIdx, *it);
44         it ++;
45         iIdx ++;
46     }
47 }

ZC:控制台输出:

1 [00] ==> 1
2 [01] ==> 8
3 [02] ==> 8
4 [03] ==> 8
5 [04] ==> 8
6 Press any key to continue

1.4、第6讲 PPT.36

◆ swap(容器A, 容器B) :   交换两个容器的元素

ZC: vs2010中有29种参数格式,暂时只用上面这一种形式,返回值是 void

ZC: VC6 测试代码:

 1 #ifdef WIN32
 2 #pragma warning (disable: 4786)
 3 #endif
 4 
 5 #include <string>
 6 #include <vector>
 7 #include <set>
 8 
 9 #include <algorithm>    // 算法
10 #include <numeric>    // 算法
11 #include <functional>    // 算法
12 
13 using namespace std;
14 
15 void main()
16 {
17     vector<int> vecIntA;
18     vecIntA.push_back(1);
19     vecIntA.push_back(5);
20     vecIntA.push_back(3);
21     
22     vector<int> vecIntB;
23     vecIntB.push_back(2);
24     vecIntB.push_back(4);
25 
26     // ZC: 这里不需要手动的对 vecIntA/vecIntB 的大小做修改
27     swap(vecIntA, vecIntB);  //交换
28 
29     int iIdx = 0;
30     vector<int>::iterator itA = vecIntA.begin();
31     while(itA != vecIntA.end())
32     {
33         printf("[%02d] ==> %d
", iIdx, *itA);
34         itA ++;
35         iIdx ++;
36     }
37 
38     printf("
");
39 
40     iIdx = 0;
41     vector<int>::iterator itB = vecIntB.begin();
42     while(itB != vecIntB.end())
43     {
44         printf("[%02d] ==> %d
", iIdx, *itB);
45         itB ++;
46         iIdx ++;
47     }
48 }

ZC:控制台输出:

1 [00] ==> 2
2 [01] ==> 4
3 
4 [00] ==> 1
5 [01] ==> 5
6 [02] ==> 3
7 Press any key to continue

?.?、第6讲 PPT.?

◆ 

ZC: VC6 测试代码:

ZC:控制台输出:

X

原文地址:https://www.cnblogs.com/cppskill/p/5241681.html