练习89

#include <iostream>
#include <list>
using namespace std;


template<class T>
void exchange(list<T>& l1, typename list<T>::iterator p1, list<T>& l2, typename list<T>::iterator p2) {
list<T> temp;
temp.splice(temp.begin(), l1, p1, l1.end());
l1.splice(l1.end(), l2, p2, l2.end());
l2.splice(l2.end(), temp, temp.begin(), temp.end());
}


typedef list<int> INTLIST;


int main() {
INTLIST l1, l2;
for (int i = 0; i < 4; i++)
l1.push_back(i);
for (int i = 6; i < 12; i++)
l2.push_back(i);
INTLIST::iterator li;
cout << "交换前" << endl;
cout << "l1: ";
for (li = l1.begin(); li != l1.end(); li++)
cout << *li << ' ';
cout << endl;
cout << "l2: ";
for (li = l2.begin(); li != l2.end(); li++)
cout << *li << ' ';
cout << endl;
INTLIST::iterator p1, p2;
p1 = l1.begin();
p2 = l2.begin();
exchange(l1, ++(++p1), l2, ++p2);
cout << "交换后" << endl;
cout << "l1: ";
for (li = l1.begin(); li != l1.end(); li++)
cout << *li << ' ';
cout << endl;
cout << "l2: ";
for (li = l2.begin(); li != l2.end(); li++)
cout << *li << ' ';
cout << endl;


return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值