在本教程中,我们将讨论一个程序,以了解C ++中不同容器的子范围的交换。
为此,我们将获得向量和列表,并且需要交换它们的某些元素。
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main(){
vector<int> v = { -10, -15, -30, 20, 500 };
list<int> lt = { 10, 50, 30, 100, 50 };
swap_ranges(v.begin(), v.begin() + 3, lt.begin());
for (int n : v)
cout << n << ' ';
cout << '\n';
for (int n : lt)
cout << n << ' ';
cout << endl;
return 0;
}输出结果
10 50 30 20 500 -10 -15 -30 100 50