list.assign(beg, end);
//将[beg, end)区间中的数据拷贝赋值给本身
1 #include
2 #include
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 list
10 list
11
12 cout << "遍历 listInt_A:";
13 for (list
14 {
15 cout << *it << " ";
16 }
17 cout << endl;
18
19 listInt_B.assign(++listInt_A.begin(), --listInt_A.end());
20 cout << "遍历 listInt_B:";
21 for (list
22 {
23 cout << *it << " ";
24 }
25 cout << endl;
26
27 return 0;
28 }
打印结果:
end()是结束符,但没有打印出来555,是因为前开后闭,
list.assign(n, elem);
//将n个elem拷贝赋值给本身
1 #include
2 #include
3
4 using namespace std;
5
6 int main()
7 {
8 list
9
10 cout << "遍历 listInt_A:";
11 for (list
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 return 0;
18 }
打印结果:
list& operator=(const list& lst);
//重载等号操作符
1 #include
2 #include
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 list
10 list
11
12 cout << "遍历 listInt_A:";
13 for (list
14 {
15 cout << *it << " ";
16 }
17 cout << endl;
18
19 listInt_B = listInt_A;
20 cout << "遍历 listInt_B:";
21 for (list
22 {
23 cout << *it << " ";
24 }
25 cout << endl;
26
27 return 0;
28 }
打印结果:
list.swap(lst);
//将lst与本身的元素互换
1 #include
2 #include
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 list
10 list
11
12 cout << "互换前 遍历 listInt_A:";
13 for (list
14 {
15 cout << *it << " ";
16 }
17 cout << endl;
18 cout << "互换前 遍历 listInt_B:";
19 for (list
20 {
21 cout << *it << " ";
22 }
23 cout << endl;
24
25 //互换
26 listInt_A.swap(listInt_B);
27 cout << "互换后 遍历 listInt_A:";
28 for (list
29 {
30 cout << *it << " ";
31 }
32 cout << endl;
33 cout << "互换后 遍历 listInt_B:";
34 for (list
35 {
36 cout << *it << " ";
37 }
38 cout << endl;
39
40 return 0;
41 }
打印结果:
===================================================================================================================