cb40a_c++_STL_算法_交换swap_ranges
cb40a_c++_STL_算法_交换swap_ranges
swap_ranges(b,e,b2);
如果两个容器的数据数量不一致时,只交换一部分数据,
a里面3个,b里面5个,则只会交换3个,b里面还有两个不变
注意:下列两种方法也是交换算法
1.容器的swap()成员函数
2.赋值操作
1 /*cb40a_c++_STL_算法_交换swap_ranges 2 swap_ranges(b,e,b2); 3 如果两个容器的数据数量不一致时,只交换一部分数据, 4 a里面3个,b里面5个,则只会交换3个,b里面还有两个不变 5 6 注意:下列两种方法也是交换算法 7 1.容器的swap()成员函数 8 2.赋值操作 9 10 */ 11 12 #include <iostream> 13 #include <algorithm> 14 #include <vector> 15 #include <deque> 16 17 using namespace std; 18 19 int main() 20 { 21 vector<int> ivec; 22 deque<int> ideq; 23 24 for (int i = 1; i <= 9; ++i) 25 ivec.push_back(i); 26 for (int i = 11; i <= 23; ++i) 27 ideq.push_back(i); 28 cout << "vector数据" << endl; 29 for (vector<int> ::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) 30 cout << *iter << ' '; 31 cout << endl; 32 cout << "deque的数据" << endl; 33 for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); ++iter) 34 cout << *iter << ' '; 35 cout << endl; 36 cout << "vector与deque的数据交换" << endl; 37 38 swap_ranges(ivec.begin(), ivec.end(), ideq.begin()); 39 40 41 42 //再次显示: 43 cout << "vector数据" << endl; 44 for (vector<int> ::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) 45 cout << *iter << ' '; 46 cout << endl; 47 cout << "deque的数据" << endl; 48 for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); ++iter) 49 cout << *iter << ' '; 50 cout << endl; 51 52 53 deque<int>::iterator pos; 54 pos = swap_ranges(ivec.begin(), ivec.end(), ideq.begin()); 55 if (pos != ideq.end()) 56 { 57 cout << "还存在没有交换完的数据: 位置是:" << *pos << endl; 58 } 59 60 //再次显示: 61 cout << "vector数据" << endl; 62 for (vector<int> ::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) 63 cout << *iter << ' '; 64 cout << endl; 65 cout << "deque的数据" << endl; 66 for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); ++iter) 67 cout << *iter << ' '; 68 cout << endl; 69 70 cout << "ideq里面:前面的三个数据和后面的三个数据相互交换" << endl; 71 swap_ranges(ideq.begin(), ideq.begin()+ 3, ideq.rbegin()); 72 for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); ++iter) 73 cout << *iter << ' '; 74 cout << endl; 75 76 vector<int>ivec1; 77 vector<int>ivec2; 78 ivec1.push_back(1); 79 ivec1.push_back(2); 80 ivec1.push_back(3); 81 82 ivec2.push_back(10); 83 ivec2.push_back(20); 84 ivec2.push_back(30); 85 86 cout << "交换前vector1的数据:" << endl; 87 for (vector<int> ::iterator iter = ivec1.begin(); iter != ivec1.end(); ++iter) 88 cout << *iter << ' '; 89 cout << endl; 90 cout << "交换前vector2的数据:" << endl; 91 92 for (vector<int> ::iterator iter = ivec2.begin(); iter != ivec2.end(); ++iter) 93 cout << *iter << ' '; 94 cout << endl; 95 96 ivec1.swap(ivec2); 97 cout << "ivec1.swap(ivec2);实际交换的是指针---,速度快。" << endl; 98 99 cout << "交换后vector1的数据:" << endl; 100 for (vector<int> ::iterator iter = ivec1.begin(); iter != ivec1.end(); ++iter) 101 cout << *iter << ' '; 102 cout << endl; 103 cout << "交换后vector2的数据:" << endl; 104 105 for (vector<int> ::iterator iter = ivec2.begin(); iter != ivec2.end(); ++iter) 106 cout << *iter << ' '; 107 cout << endl; 108 109 110 111 return 0; 112 }
欢迎讨论,相互学习。
cdtxw@foxmail.com