//编写一个函数模板
//该模板用于将l1链表的[p1,l1.end())区间和l2链表的[p2,l1.end())区间的内容交换。
//在主函数中调用该模板,以测试该模板的正确性
#include
#include
#include
using namespace std;
template
void exchange(list& l1, list::iterator p1, list& l2, list::iterator p2) {
listl3;
l3.splice(l3.begin(), l1, p1, l1.end());
l1.splice(l1.end(), l2, p2, l2.end());
l2.splice(l2.end(), l3, l3.begin(), l3.end());
}
int main()
{
int a[5] = { 1,2,3,4,5 };
int b[6] = { 10,11,12,13,14,15 };
listl1(a, a + 5);
listl2(b, b + 6);
cout << "l1 before exchange: ";
copy(l1.begin(), l1.end(), ostream_iterator(cout, " "));
cout << "l2 before exchange: ";
copy(l2.begin(), l2.end(), ostream_iterator(cout, " "));
list<int>::iterator iter1 = l1.begin();
advance(iter1, 3);
list<int>::iterator iter2 = l2.begin();
advance(iter2, 3);
exchange<int>(l1, iter1, l2, iter2);
cout << "l1 after exchange: ";
copy(l1.begin(), l1.end(), ostream_iterator<int>(cout, " "));
cout << "l2 after exchange: ";
copy(l2.begin(), l2.end(), ostream_iterator<int>(cout, " "));
return 0;
}
1>g:\c++\homework in book\10_6\10_6\10_6.cpp(12): warning C4346: “std::list>::iterator”: 依赖名称不是类型
1> g:\c++\homework in book\10_6\10_6\10_6.cpp(12): note: 用“typename”为前缀来表示类型
1>g:\c++\homework in book\10_6\10_6\10_6.cpp(12): error C2061: 语法错误: 标识符“iterator”
1>g:\c++\homework in book\10_6\10_6\10_6.cpp(35): error C2672: “exchange”: 未找到匹配的重载函数
1>g:\c++\homework in book\10_6\10_6\10_6.cpp(35): error C2780: “void exchange(std::list> &)”: 应输入 1 个参数,却提供了 4 个
1> g:\c++\homework in book\10_6\10_6\10_6.cpp(12): note: 参见“exchange”的声明
1>g:\c++\homework in book\10_6\10_6\10_6.cpp(35): error C2780: “_Ty std::exchange(_Ty &,_Other &&)”: 应输入 2 个参数,却提供了 4 个
1> d:\microsoft visual studio 14.0\vc\include\utility(590): note: 参见“std::exchange”的声明
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========