swap_ranges(b, e, b2)
注意:下列两种方法也是交换算法
1,容器的swap()成员函数 方法快
2,赋值操作
交换算法对所有容器适用
#include<iostream> #include<algorithm> #include<vector> #include<deque> using namespace std; int main() { vector<int> ivec; deque<int>ideq; for (int i = 1; i <= 9; i++) ivec.push_back(i); for (int i = 11; i <= 23; i++) ideq.push_back(i); for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) cout << *iter << ' '; cout << endl; for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++) cout << *iter << ' '; cout << endl; swap_ranges(ivec.begin(), ivec.end(), ideq.begin()); for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) cout << *iter << ' '; cout << endl; for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++) cout << *iter << ' '; cout << endl; // system("pause"); return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<deque> using namespace std; int main() { vector<int> ivec; deque<int>ideq; for (int i = 1; i <= 9; i++) ivec.push_back(i); for (int i = 11; i <= 23; i++) ideq.push_back(i); for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) cout << *iter << ' '; cout << endl; for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++) cout << *iter << ' '; cout << endl; deque<int>::iterator pos; // 返回值为第二个区间没有交换过来的数 pos = swap_ranges(ivec.begin(), ivec.end(), ideq.begin()); for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) cout << *iter << ' '; cout << endl; for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++) cout << *iter << ' '; cout << endl; if (pos != ideq.end()) { cout << "第一个没有交换的数:" << *pos << endl; } swap_ranges(ideq.begin(), ideq.begin() + 3, ideq.rbegin()); for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++) cout << *iter << ' '; cout << endl; // system("pause"); return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<deque> using namespace std; int main() { vector<int> ivec1; vector<int> ivec2; ivec1.push_back(1); ivec1.push_back(2); ivec1.push_back(3); ivec2.push_back(10); ivec2.push_back(20); ivec2.push_back(30); // swap 比swap_ranges 函数比较快 //ivec1.swap(ivec2);// swap 方法最快 ivec1 = ivec2;// 此方法比较慢 cout << ivec1.at(0) << ' ' << ivec1.at(1) << ' ' << ivec1.at(2) << endl; vector<int> ivec; deque<int>ideq; for (int i = 1; i <= 9; i++) ivec.push_back(i); for (int i = 11; i <= 23; i++) ideq.push_back(i); for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) cout << *iter << ' '; cout << endl; for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++) cout << *iter << ' '; cout << endl; deque<int>::iterator pos; // 返回值为第二个区间没有交换过来的数 pos = swap_ranges(ivec.begin(), ivec.end(), ideq.begin());// 此方法也比较慢,但是可以把两个不同的容器的数据进行交换,相同的也行 for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) cout << *iter << ' '; cout << endl; for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++) cout << *iter << ' '; cout << endl; if (pos != ideq.end()) { cout << "第一个没有交换的数:" << *pos << endl; } swap_ranges(ideq.begin(), ideq.begin() + 3, ideq.rbegin()); for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++) cout << *iter << ' '; cout << endl; // system("pause"); return 0; }
时间: 2024-09-29 09:03:37