C++编程思想 第2卷 第7章 通用容器 基本序列容器:vector list deque 交换序列

所有的基本序列容器都有一个成员函数swap()
该函数被设计用来将一个序列转换为另一个序列
成员函数swap()利用 特定容器内部结构的知识
提高操作的效率

//: C07:Swapping.cpp {-bor}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// All basic sequence containers can be swapped.
//{L} Noisy
#include <algorithm>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
#include "Noisy.h"
#include "PrintContainer.h"
using namespace std;
ostream_iterator<Noisy> out(cout, " ");

template<class Cont> void testSwap(char* cname) {
  Cont c1, c2;
  generate_n(back_inserter(c1), 10, NoisyGen());
  generate_n(back_inserter(c2), 5, NoisyGen());
  cout << endl << cname << ":" << endl;
  print(c1, "c1"); print(c2, "c2");
  cout << "\n Swapping the " << cname << ":" << endl;
  c1.swap(c2);
  print(c1, "c1"); print(c2, "c2");
}

int main() {
  testSwap<vector<Noisy> >("vector");
  testSwap<deque<Noisy> >("deque");
  testSwap<list<Noisy> >("list");
  getchar();
} ///:~

输出
d[0]
c[0]
~[0]
d[1]
c[0]
~[0]
c[1]
~[1]
d[2]
c[0]
c[1]
~[0]
~[1]
c[2]
~[2]
d[3]
c[0]
c[1]
c[2]
~[0]
~[1]
~[2]
c[3]
~[3]
d[4]
c[0]
c[1]
c[2]
c[3]
~[0]
~[1]
~[2]
~[3]
c[4]
~[4]
d[5]
c[5]
~[5]
d[6]
c[0]
c[1]
c[2]
c[3]
c[4]
c[5]
~[0]
~[1]
~[2]
~[3]
~[4]
~[5]
c[6]
~[6]
d[7]
c[7]
~[7]
d[8]
c[8]
~[8]
d[9]
c[0]
c[1]
c[2]
c[3]
c[4]
c[5]
c[6]
c[7]
c[8]
~[0]
~[1]
~[2]
~[3]
~[4]
~[5]
~[6]
~[7]
~[8]
c[9]
~[9]
d[10]
c[10]
~[10]
d[11]
c[10]
~[10]
c[11]
~[11]
d[12]
c[10]
c[11]
~[10]
~[11]
c[12]
~[12]
d[13]
c[10]
c[11]
c[12]
~[10]
~[11]
~[12]
c[13]
~[13]
d[14]
c[10]
c[11]
c[12]
c[13]
~[10]
~[11]
~[12]
~[13]
c[14]
~[14]

vector:
c1:
0
1
2
3
4
5
6
7
8
9

c2:
10
11
12
13
14


 Swapping the vector:
c1:
10
11
12
13
14

c2:
0
1
2
3
4
5
6
7
8
9

~[0]
~[1]
~[2]
~[3]
~[4]
~[5]
~[6]
~[7]
~[8]
~[9]
~[10]
~[11]
~[12]
~[13]
~[14]
d[15]
c[15]
~[15]
d[16]
c[16]
~[16]
d[17]
c[17]
~[17]
d[18]
c[18]
~[18]
d[19]
c[19]
~[19]
d[20]
c[20]
~[20]
d[21]
c[21]
~[21]
d[22]
c[22]
~[22]
d[23]
c[23]
~[23]
d[24]
c[24]
~[24]
d[25]
c[25]
~[25]
d[26]
c[26]
~[26]
d[27]
c[27]
~[27]
d[28]
c[28]
~[28]
d[29]
c[29]
~[29]

deque:
c1:
15
16
17
18
19
20
21
22
23
24

c2:
25
26
27
28
29


 Swapping the deque:
c1:
25
26
27
28
29

c2:
15
16
17
18
19
20
21
22
23
24

~[24]
~[23]
~[22]
~[21]
~[20]
~[19]
~[18]
~[17]
~[16]
~[15]
~[29]
~[28]
~[27]
~[26]
~[25]
d[30]
c[30]
~[30]
d[31]
c[31]
~[31]
d[32]
c[32]
~[32]
d[33]
c[33]
~[33]
d[34]
c[34]
~[34]
d[35]
c[35]
~[35]
d[36]
c[36]
~[36]
d[37]
c[37]
~[37]
d[38]
c[38]
~[38]
d[39]
c[39]
~[39]
d[40]
c[40]
~[40]
d[41]
c[41]
~[41]
d[42]
c[42]
~[42]
d[43]
c[43]
~[43]
d[44]
c[44]
~[44]

list:
c1:
30
31
32
33
34
35
36
37
38
39

c2:
40
41
42
43
44


 Swapping the list:
c1:
40
41
42
43
44

c2:
30
31
32
33
34
35
36
37
38
39

~[30]
~[31]
~[32]
~[33]
~[34]
~[35]
~[36]
~[37]
~[38]
~[39]
~[40]
~[41]
~[42]
~[43]
~[44]

每一种类型的序列容器都能在不需要复制或者赋值的情况下将一种序列
变换为另一种序列
即使序列的尺寸不同
实际上 其所作的是完全地将一个对象的资源交换成另一个对象的资源

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/82432427