C++ Lists(链表)
Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/d3707f4a-10fd-444f-b856-f9ca2077c1cd.htm
assign() 给list赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list的配置器
insert() 插入一个元素到list中
max_size() 返回list能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list的末尾添加一个元素
push_front() 在list的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list删除元素
remove_if() 按指定条件删除元素
rend() 指向list末尾的逆向迭代器
resize() 改变list的大小
reverse() 把list的元素倒转
size() 返回list中的元素个数
sort() 给list排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素
</pre><pre code_snippet_id="633171" snippet_file_name="blog_20150401_1_589455" class="cpp" name="code">#include <list>
#include <iostream>
#include <xfunctional>
//构造函数
void List_Constructor(void);
//从列表中,并复制擦除指定元素的新元素集合到目标名单
void List_assign(void);
//返回到列表中的最后一个元素的引用
void List_back(void);
//返回一个迭代解决列表中的第一个元素
void List_begin(void);
//擦除列表的所有元素
void List_clear(void);
//测试容器是否为空
void List_empty(void);
//返回迭代结束
void List_end(void);
//擦除元素
void List_erase(void);
//返回一个矢量到第一元素的引用
void List_front(void);
//返回用于构造矢量分配器对象的副本
void List_get_allocator(void);
//插入一个元素或多个元素到载体的指定位置的。
void List_insert(void);
//返回最大长度
void List_max_size(void);
//合并两个list
void List_merge(void);
//删除最后一个元素
void List_pop_back(void);
//删除第一个元素
void List_pop_front(void);
//在list的末尾添加一个元素
void List_push_back(void);
//在list的头部添加一个元素
void List_push_front(void);
//返回指向第一个元素的逆向迭代器
void List_rbegin(void);
//从list删除元素
void List_remove(void);
//按指定条件删除元素
void List_remove_if(void);
//指向list末尾的逆向迭代器
void List_rend(void);
//改变list的大小
void List_resize(void);
//把list的元素倒转
void List_reverse(void);
//返回list中的元素个数
void List_size(void);
//给list排序
void List_sort(void);
//合并两个list
void List_splice(void);
//交换两个list
void List_swap(void);
// 删除list中重复的元素
void List_unique(void);
int main()
{
//List_Constructor();
//List_assign();
//List_back();
//List_begin();
//List_clear();
//List_empty();
//List_end();
//List_erase();
//List_front();
//List_get_allocator();
//List_insert();
//List_max_size();
//List_merge();
//List_pop_back();
//List_pop_front();
//List_push_back();
// List_push_front();
//List_rbegin();
//List_remove();
//List_remove_if();
//List_rend();
//List_resize();
//List_reverse();
//List_size();
//List_sort();
//List_splice();
//List_swap();
List_unique();
return 0;
}
//构造函数
void List_Constructor(void)
{
using namespace std;
list <int>::iterator c1_Iter, c2_Iter, c3_Iter, c4_Iter, c5_Iter, c6_Iter;
// Create an empty list c0
list <int> c0;
// Create a list c1 with 3 elements of default value 0
list <int> c1(3);
// Create a list c2 with 5 elements of value 2
list <int> c2(5, 2);
// Create a list c3 with 3 elements of value 1 and with the
// allocator of list c2
list <int> c3(3, 1, c2.get_allocator());
// Create a copy, list c4, of list c2
list <int> c4(c2);
// Create a list c5 by copying the range c4[_First, _Last)
c4_Iter = c4.begin();
c4_Iter++;
c4_Iter++;
list <int> c5(c4.begin(), c4_Iter);
// Create a list c6 by copying the range c4[_First, _Last) and with
// the allocator of list c2
c4_Iter = c4.begin();
c4_Iter++;
c4_Iter++;
c4_Iter++;
list <int> c6(c4.begin(), c4_Iter, c2.get_allocator());
cout << "c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
cout << "c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
cout << "c3 =";
for (c3_Iter = c3.begin(); c3_Iter != c3.end(); c3_Iter++)
cout << " " << *c3_Iter;
cout << endl;
cout << "c4 =";
for (c4_Iter = c4.begin(); c4_Iter != c4.end(); c4_Iter++)
cout << " " << *c4_Iter;
cout << endl;
cout << "c5 =";
for (c5_Iter = c5.begin(); c5_Iter != c5.end(); c5_Iter++)
cout << " " << *c5_Iter;
cout << endl;
cout << "c6 =";
for (c6_Iter = c6.begin(); c6_Iter != c6.end(); c6_Iter++)
cout << " " << *c6_Iter;
cout << endl;
return;
/*
c1 = 0 0 0
c2 = 2 2 2 2 2
c3 = 1 1 1
c4 = 2 2 2 2 2
c5 = 2 2
c6 = 2 2 2
请按任意键继续. . .
*/
}
//从列表中,并复制擦除指定元素的新元素集合到目标名单
void List_assign(void)
{
using namespace std;
list<int> c1, c2;
list<int>::const_iterator cIter;
c1.push_back(10);
c1.push_back(20);
c1.push_back(30);
c2.push_back(40);
c2.push_back(50);
c2.push_back(60);
cout << "c1 =";
for (cIter = c1.begin(); cIter != c1.end(); cIter++)
cout << " " << *cIter;
cout << endl;
c1.assign(++c2.begin(), c2.end());
cout << "c1 =";
for (cIter = c1.begin(); cIter != c1.end(); cIter++)
cout << " " << *cIter;
cout << endl;
c1.assign(7, 4);
cout << "c1 =";
for (cIter = c1.begin(); cIter != c1.end(); cIter++)
cout << " " << *cIter;
cout << endl;
return;
/*
c1 = 10 20 30
c1 = 50 60
c1 = 4 4 4 4 4 4 4
请按任意键继续. . .
*/
}
//返回到列表中的最后一个元素的引用
void List_back(void)
{
using namespace std;
list <int> c1;
c1.push_back(10);
c1.push_back(11);
int& i = c1.back();
const int& ii = c1.front();
cout << "The last integer of c1 is " << i << endl;
i--;
cout << "The next-to-last integer of c1 is " << ii << endl;
return;
/*
The last integer of c1 is 11
The next-to-last integer of c1 is 10
请按任意键继续. . .
*/
}
//返回一个迭代解决列表中的第一个元素
void List_begin(void)
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;
list <int>::const_iterator c1_cIter;
c1.push_back(1);
c1.push_back(2);
c1_Iter = c1.begin();
cout << "The first element of c1 is " << *c1_Iter << endl;
*c1_Iter = 20;
c1_Iter = c1.begin();
cout << "The first element of c1 is now " << *c1_Iter << endl;
// The following line would be an error because iterator is const
// *c1_cIter = 200;
return;
/*
The first element of c1 is 1
The first element of c1 is now 20
请按任意键继续. . .
*/
}
//擦除列表的所有元素
void List_clear(void)
{
using namespace std;
list <int> c1;
c1.push_back(10);
c1.push_back(20);
c1.push_back(30);
cout << "The size of the list is initially " << c1.size() << endl;
c1.clear();
cout << "The size of list after clearing is " << c1.size() << endl;
return;
/*
The size of the list is initially 3
The size of list after clearing is 0
请按任意键继续. . .
*/
}
//测试容器是否为空
void List_empty(void)
{
using namespace std;
list <int> c1;
c1.push_back(10);
if (c1.empty())
cout << "The list is empty." << endl;
else
cout << "The list is not empty." << endl;
return;
/*
The list is not empty.
请按任意键继续. . .
*/
}
//返回迭代结束
void List_end(void)
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;
c1.push_back(10);
c1.push_back(20);
c1.push_back(30);
c1_Iter = c1.end();
c1_Iter--;
cout << "The last integer of c1 is " << *c1_Iter << endl;
c1_Iter--;
*c1_Iter = 400;
cout << "The new next-to-last integer of c1 is "
<< *c1_Iter << endl;
// If a const iterator had been declared instead with the line:
// list <int>::const_iterator c1_Iter;
// an error would have resulted when inserting the 400
cout << "The list is now:";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
return;
/*
The last integer of c1 is 30
The new next-to-last integer of c1 is 400
The list is now: 10 400 30
请按任意键继续. . .
*/
}
//擦除元素
void List_erase(void)
{
using namespace std;
list <int> c1;
list <int>::iterator Iter;
c1.push_back(10);
c1.push_back(20);
c1.push_back(30);
c1.push_back(40);
c1.push_back(50);
cout << "The initial list is:";
for (Iter = c1.begin(); Iter != c1.end(); Iter++)
cout << " " << *Iter;
cout << endl;
c1.erase(c1.begin());
cout << "After erasing the first element, the list becomes:";
for (Iter = c1.begin(); Iter != c1.end(); Iter++)
cout << " " << *Iter;
cout << endl;
Iter = c1.begin();
Iter++;
c1.erase(Iter, c1.end());
cout << "After erasing all elements but the first, the list becomes: ";
for (Iter = c1.begin(); Iter != c1.end(); Iter++)
cout << " " << *Iter;
cout << endl;
return;
/*
程序执行结果如下
The initial list is: 10 20 30 40 50
After erasing the first element, the list becomes: 20 30 40 50
After erasing all elements but the first, the list becomes: 20
请按任意键继续. . .
*/
}
//返回一个矢量到第一元素的引用
void List_front(void)
{
using namespace std;
list <int> c1;
c1.push_back(10);
int& i = c1.front();
const int& ii = c1.front();
cout << "The first integer of c1 is " << i << endl;
i++;
cout << "The first integer of c1 is " << ii << endl;
return;
/*
程序执行结果如下
The first integer of c1 is 10
The first integer of c1 is 11
请按任意键继续. . .
*/
}
//返回用于构造矢量分配器对象的副本
void List_get_allocator(void)
{
using namespace std;
// The following lines declare objects
// that use the default allocator.
list <int> c1;
list <int, allocator<int> > c2 = list <int, allocator<int> >(allocator<int>());
// c3 will use the same allocator class as c1
list <int> c3(c1.get_allocator());
list<int>::allocator_type xlst = c1.get_allocator();
// You can now call functions on the allocator class used by c1
return;;
}
//插入一个元素或多个元素到载体的指定位置的。
void List_insert(void)
{
using namespace std;
list <int> c1, c2;
list <int>::iterator Iter;
c1.push_back(10);
c1.push_back(20);
c1.push_back(30);
c2.push_back(40);
c2.push_back(50);
c2.push_back(60);
cout << "c1 =";
for (Iter = c1.begin(); Iter != c1.end(); Iter++)
cout << " " << *Iter;
cout << endl;
Iter = c1.begin();
Iter++;
c1.insert(Iter, 100);
cout << "c1 =";
for (Iter = c1.begin(); Iter != c1.end(); Iter++)
cout << " " << *Iter;
cout << endl;
Iter = c1.begin();
Iter++;
Iter++;
c1.insert(Iter, 2, 200);
cout << "c1 =";
for (Iter = c1.begin(); Iter != c1.end(); Iter++)
cout << " " << *Iter;
cout << endl;
c1.insert(++c1.begin(), c2.begin(), --c2.end());
cout << "c1 =";
for (Iter = c1.begin(); Iter != c1.end(); Iter++)
cout << " " << *Iter;
cout << endl;
return;
/*
程序执行结果如下
c1 = 10 20 30
c1 = 10 100 20 30
c1 = 10 100 200 200 20 30
c1 = 10 40 50 100 200 200 20 30
请按任意键继续. . .
*/
}
//返回最大长度
void List_max_size(void)
{
using namespace std;
list <int> c1;
list <int>::size_type i;
i = c1.max_size();
cout << "Maximum possible length of the list is " << i << "." << endl;
return;
/*
程序执行结果如下
Maximum possible length of the list is 357913941.
请按任意键继续. . .
*/
}
//合并两个list
void List_merge(void)
{
using namespace std;
list <int> c1, c2, c3;
list <int>::iterator c1_Iter, c2_Iter, c3_Iter;
c1.push_back(3);
c1.push_back(6);
c2.push_back(2);
c2.push_back(4);
c3.push_back(5);
c3.push_back(1);
cout << "c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
cout << "c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
c2.merge(c1); // Merge c1 into c2 in (default) ascending order
c2.sort(greater<int>());
c2.sort();
cout << "After merging c1 with c2 and sorting with >: c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
cout << "c3 =";
for (c3_Iter = c3.begin(); c3_Iter != c3.end(); c3_Iter++)
cout << " " << *c3_Iter;
cout << endl;
c2.merge(c3, greater<int>());
cout << "After merging c3 with c2 according to the '>' comparison relation: c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
return;
/*
程序执行结果如下
c1 = 3 6
c2 = 2 4
After merging c1 with c2 and sorting with >: c2 = 2 3 4 6
c3 = 5 1
After merging c3 with c2 according to the '>' comparison relation: c2 = 5 2 3 4
6 1
请按任意键继续. . .
debug有断言错误
*/
}
//删除最后一个元素
void List_pop_back(void)
{
using namespace std;
list <int> c1;
c1.push_back(1);
c1.push_back(2);
cout << "The first element is: " << c1.front() << endl;
cout << "The last element is: " << c1.back() << endl;
c1.pop_back();
cout << "After deleting the element at the end of the list, "
"the last element is: " << c1.back() << endl;
return;
/*
程序执行结果如下
The first element is: 1
The last element is: 2
After deleting the element at the end of the list, the last element is: 1
请按任意键继续. . .
*/
}
//删除第一个元素
void List_pop_front(void)
{
using namespace std;
list <int> c1;
c1.push_back(1);
c1.push_back(2);
cout << "The first element is: " << c1.front() << endl;
cout << "The second element is: " << c1.back() << endl;
c1.pop_front();
cout << "After deleting the element at the beginning of the list, "
"the first element is: " << c1.front() << endl;
return;
/*
程序执行结果如下
The first element is: 1
The second element is: 2
After deleting the element at the beginning of the list, the first element is: 2
请按任意键继续. . .
*/
}
//在list的末尾添加一个元素
void List_push_back(void)
{
using namespace std;
list <int> c1;
c1.push_back(1);
if (c1.size() != 0)
cout << "Last element: " << c1.back() << endl;
c1.push_back(2);
if (c1.size() != 0)
cout << "New last element: " << c1.back() << endl;
return;
/*
程序执行结果如下
Last element: 1
New last element: 2
请按任意键继续. . .
*/
}
//在list的头部添加一个元素
void List_push_front(void)
{
using namespace std;
list <int> c1;
c1.push_front(1);
if (c1.size() != 0)
cout << "First element: " << c1.front() << endl;
c1.push_front(2);
if (c1.size() != 0)
cout << "New first element: " << c1.front() << endl;
return;
/*
First element: 1
New first element: 2
请按任意键继续. . .
*/
}
//返回指向第一个元素的逆向迭代器
void List_rbegin(void)
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;
list <int>::reverse_iterator c1_rIter;
// If the following line replaced the line above, *c1_rIter = 40;
// (below) would be an error
//list <int>::const_reverse_iterator c1_rIter;
c1.push_back(10);
c1.push_back(20);
c1.push_back(30);
c1_rIter = c1.rbegin();
cout << "The last element in the list is " << *c1_rIter << "." << endl;
cout << "The list is:";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
// rbegin can be used to start an iteration through a list in
// reverse order
cout << "The reversed list is:";
for (c1_rIter = c1.rbegin(); c1_rIter != c1.rend(); c1_rIter++)
cout << " " << *c1_rIter;
cout << endl;
c1_rIter = c1.rbegin();
*c1_rIter = 40;
cout << "The last element in the list is now " << *c1_rIter << "." << endl;
return;
/*
The last element in the list is 30.
The list is: 10 20 30
The reversed list is: 30 20 10
The last element in the list is now 40.
请按任意键继续. . .
*/
}
//从list删除元素
void List_remove(void)
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter, c2_Iter;
c1.push_back(5);
c1.push_back(100);
c1.push_back(5);
c1.push_back(200);
c1.push_back(5);
c1.push_back(300);
cout << "The initial list is c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
list <int> c2 = c1;
c2.remove(5);
cout << "After removing elements with value 5, the list becomes c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
return;
/*
程序执行结果如下
The initial list is c1 = 5 100 5 200 5 300
After removing elements with value 5, the list becomes c2 = 100 200 300
*/
}
bool single_digit(const int& value) { return (value < 10); }
// a predicate implemented as a class:
class is_odd
{
public:
bool operator() (const int& value) { return (value % 2) == 1; }
};
//按指定条件删除元素
void List_remove_if(void)
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter, c2_Iter;
c1.push_back(3);
c1.push_back(4);
c1.push_back(5);
c1.push_back(6);
c1.push_back(7);
c1.push_back(8);
cout << "The initial list is c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
list <int> c2 = c1;
c2.remove_if(is_odd());
cout << "After removing the odd elements, "
<< "the list becomes c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
return;
/*
The initial list is c1 = 3 4 5 6 7 8
After removing the odd elements, the list becomes c2 = 4 6 8
请按任意键继续. . .
*/
}
//指向list末尾的逆向迭代器
void List_rend(void)
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;
list <int>::reverse_iterator c1_rIter;
// If the following line had replaced the line above, an error would
// have resulted in the line modifying an element (commented below)
// because the iterator would have been const
// list <int>::const_reverse_iterator c1_rIter;
c1.push_back(10);
c1.push_back(20);
c1.push_back(30);
c1_rIter = c1.rend();
c1_rIter--; // Decrementing a reverse iterator moves it forward in
// the list (to point to the first element here)
cout << "The first element in the list is: " << *c1_rIter << endl;
cout << "The list is:";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
// rend can be used to test if an iteration is through all of the
// elements of a reversed list
cout << "The reversed list is:";
for (c1_rIter = c1.rbegin(); c1_rIter != c1.rend(); c1_rIter++)
cout << " " << *c1_rIter;
cout << endl;
c1_rIter = c1.rend();
c1_rIter--; // Decrementing the reverse iterator moves it backward
// in the reversed list (to the last element here)
*c1_rIter = 40; // This modification of the last element would have
// caused an error if a const_reverse iterator had
// been declared (as noted above)
cout << "The modified reversed list is:";
for (c1_rIter = c1.rbegin(); c1_rIter != c1.rend(); c1_rIter++)
cout << " " << *c1_rIter;
cout << endl;
return;
/*
The first element in the list is: 10
The list is: 10 20 30
The reversed list is: 30 20 10
The modified reversed list is: 30 20 40
请按任意键继续. . .
*/
}
//改变list的大小
void List_resize(void)
{
using namespace std;
list <int> c1;
c1.push_back(10);
c1.push_back(20);
c1.push_back(30);
c1.resize(4, 40);
cout << "The size of c1 is " << c1.size() << endl;
cout << "The value of the last element is " << c1.back() << endl;
c1.resize(5);
cout << "The size of c1 is now " << c1.size() << endl;
cout << "The value of the last element is now " << c1.back() << endl;
c1.resize(2);
cout << "The reduced size of c1 is: " << c1.size() << endl;
cout << "The value of the last element is now " << c1.back() << endl;
return;
/*
The size of c1 is 4
The value of the last element is 40
The size of c1 is now 5
The value of the last element is now 0
The reduced size of c1 is: 2
The value of the last element is now 20
请按任意键继续. . .
*/
}
//把list的元素倒转
void List_reverse(void)
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;
c1.push_back(10);
c1.push_back(20);
c1.push_back(30);
cout << "c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
c1.reverse();
cout << "Reversed c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
return;
/*
c1 = 10 20 30
Reversed c1 = 30 20 10
请按任意键继续. . .
*/
}
//返回list中的元素个数
void List_size(void)
{
using namespace std;
list <int> c1;
list <int>::size_type i;
c1.push_back(1);
i = c1.size();
cout << "List length is " << i << "." << endl;
c1.push_back(2);
i = c1.size();
cout << "List length is now " << i << "." << endl;
return;
/*
List length is 1.
List length is now 2.
请按任意键继续. . .
*/
}
//给list排序
void List_sort(void)
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;
c1.push_back(20);
c1.push_back(10);
c1.push_back(30);
cout << "Before sorting: c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
c1.sort();
cout << "After sorting c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
c1.sort(greater<int>());
cout << "After sorting with 'greater than' operation, c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
return;
/*
Before sorting: c1 = 20 10 30
After sorting c1 = 10 20 30
After sorting with 'greater than' operation, c1 = 30 20 10
请按任意键继续. . .
*/
}
//合并两个list
void List_splice(void)
{
using namespace std;
list <int> c1, c2, c3, c4;
list <int>::iterator c1_Iter, c2_Iter, w_Iter, f_Iter, l_Iter;
c1.push_back(10);
c1.push_back(11);
c2.push_back(12);
c2.push_back(20);
c2.push_back(21);
c3.push_back(30);
c3.push_back(31);
c4.push_back(40);
c4.push_back(41);
c4.push_back(42);
cout << "c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
cout << "c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
w_Iter = c2.begin();
w_Iter++;
c2.splice(w_Iter, c1);
cout << "After splicing c1 into c2: c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
f_Iter = c3.begin();
c2.splice(w_Iter, c3, f_Iter);
cout << "After splicing the first element of c3 into c2: c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
f_Iter = c4.begin();
l_Iter = c4.end();
l_Iter--;
c2.splice(w_Iter, c4, f_Iter, l_Iter);
cout << "After splicing a range of c4 into c2: c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
return;
/*
c1 = 10 11
c2 = 12 20 21
After splicing c1 into c2: c2 = 12 10 11 20 21
After splicing the first element of c3 into c2: c2 = 12 10 11 30 20 21
After splicing a range of c4 into c2: c2 = 12 10 11 30 40 41 20 21
请按任意键继续. . .
*/
}
//交换两个list
void List_swap(void)
{
using namespace std;
list <int> c1, c2, c3;
list <int>::iterator c1_Iter;
c1.push_back(1);
c1.push_back(2);
c1.push_back(3);
c2.push_back(10);
c2.push_back(20);
c3.push_back(100);
cout << "The original list c1 is:";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
c1.swap(c2);
cout << "After swapping with c2, list c1 is:";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
swap(c1, c3);
cout << "After swapping with c3, list c1 is:";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
return;
/*
The original list c1 is: 1 2 3
After swapping with c2, list c1 is: 10 20
After swapping with c3, list c1 is: 100
请按任意键继续. . .
*/
}
// 删除list中重复的元素
void List_unique(void)
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter, c2_Iter, c3_Iter;
not_equal_to<int> mypred;
c1.push_back(-10);
c1.push_back(10);
c1.push_back(10);
c1.push_back(20);
c1.push_back(20);
c1.push_back(-10);
cout << "The initial list is c1 =";
for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
cout << " " << *c1_Iter;
cout << endl;
list <int> c2 = c1;
c2.unique();
cout << "After removing successive duplicate elements, c2 =";
for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
cout << " " << *c2_Iter;
cout << endl;
list <int> c3 = c2;
c3.unique(mypred);
cout << "After removing successive unequal elements, c3 =";
for (c3_Iter = c3.begin(); c3_Iter != c3.end(); c3_Iter++)
cout << " " << *c3_Iter;
cout << endl;
return;
/*
The initial list is c1 = -10 10 10 20 20 -10
After removing successive duplicate elements, c2 = -10 10 20 -10
After removing successive unequal elements, c3 = -10 -10
请按任意键继续. . .
*/
}