ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

STL——容器(List)List 的数据元素插入和删除操作

2020-05-04 21:05:59  阅读:242  来源: 互联网

标签:容器 begin listInt cout STL List ++ num end


push_back(elem);

//在容器尾部加入一个元素

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     list<int> listInt(num, num + size(num));
10     cout << "初始遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     listInt.push_back(444);
18     cout << "push_back后遍历 listInt:";
19     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << endl;
24 
25     return 0;
26 }

打印结果:

 

 

 

pop_back();

//删除容器中最后一个元素

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     list<int> listInt(num, num + size(num));
10     cout << "初始遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     listInt.pop_back();
18     cout << "pop_back 后遍历 listInt:";
19     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << endl;
24 
25     return 0;
26 }

打印结果:

 

 

 

 

push_front(elem);

//在容器开头插入一个元素

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     list<int> listInt(num, num + size(num));
10     cout << "初始遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     listInt.push_front(0);
18     cout << "push_front 后遍历 listInt:";
19     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << endl;
24 
25     return 0;
26 }

打印结果:

 

 

 

 

pop_front();

//从容器开头移除第一个元素

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     list<int> listInt(num, num + size(num));
10     cout << "初始遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     listInt.pop_front();
18     cout << "pop_front 后遍历 listInt:";
19     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << endl;
24 
25     return 0;
26 }

打印结果:

 

 

 

 

insert(pos, elem);

//在pos位置插elem元素的拷贝,返回新数据的位置

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     list<int> listInt(num, num + size(num));
10     cout << "初始遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17 
18     cout << "insert 后,用 insert 的返回值遍历 listInt:";
19     for (list<int>::iterator it = listInt.insert(++listInt.begin(), 888); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << endl;
24     cout << "最终遍历 listInt:";
25     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
26     {
27         cout << *it << " ";
28     }
29 
30     return 0;
31 }

打印结果:

 

 

 

 

insert(pos, n, elem);

//在pos位置插入n个elem数据,返回新数据的第一个数据的位置

(这个有没有返回值是编译器版本决定,早起版本的编译器没有返回值)

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     list<int> listInt(num, num + size(num));
10     cout << "初始遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     
18     cout << "insert 后,用 insert 的返回值遍历 listInt:";
19     for (list<int>::iterator it = listInt.insert(++listInt.begin(), 2, 888); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << endl;
24     cout << "最终遍历 listInt:";
25     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
26     {
27         cout << *it << " ";
28     }
29 
30     return 0;
31 }

打印结果:

 

 

 

 

 

insert(pos, beg, end);

//在pos位置插入[beg,end)区间的数据,下面代码我举两种使用方法,一种是使用迭代器插入,另一种是插入数组

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     int num_1[] = { 666,777,888 };
10     list<int> listInt(num, num + size(num));
11     list<int> listInt_A(2, 666);
12     cout << "初始遍历 listInt:";
13     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
14     {
15         cout << *it << " ";
16     }
17     cout << endl;
18 
19     
20     cout << "insert 后,用 insert 的返回值遍历 listInt:";
21     for (list<int>::iterator it = listInt.insert(++listInt.begin(), listInt_A.begin(), listInt_A.end()); it != listInt.end(); it++)
22     {
23         cout << *it << " ";
24     }
25 
26     cout << endl;
27     cout << "最终遍历 listInt:";
28     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
29     {
30         cout << *it << " ";
31     }
32     cout << endl << endl;
33 
34 
35     //当然这里也可以插入数组
36     cout << "使用 insert 插入数组,然后用 insert 的返回值遍历 listInt:" << endl;
37     for (list<int>::iterator it = listInt.insert(++listInt.begin(), num_1, num_1 + size(num_1)); it != listInt.end(); it++)
38     {
39         cout << *it << " ";
40     }
41 
42     cout << endl;
43     cout << "最终遍历 listInt:";
44     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
45     {
46         cout << *it << " ";
47     }
48 
49     return 0;
50 }

打印结果:

 

 

 

 

clear();

//移除容器的所有数据

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333 };
 9     list<int> listInt(num, num + size(num));
10     cout << "clear 前遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16     cout << "clear 前 listInt.size() = " << listInt.size() << endl;
17 
18 
19     listInt.clear();
20     cout << "clear 后遍历 listInt:";
21     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
22     {
23         cout << *it << " ";
24     }
25     cout << endl;
26     cout << "clear 后 listInt.size() = " << listInt.size() << endl;
27 
28     return 0;
29 }

打印结果:

 

 

 

 

erase(beg, end);

//删除[beg,end)区间的数据,返回下一个数据的位置

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333,444,555,666 };
 9     list<int> listInt(num, num + size(num));
10     cout << "erase 前遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     
18     cout << "erase 后,用其返回值遍历 listInt:";
19     for (list<int>::iterator it = listInt.erase(++listInt.begin(), --listInt.end()); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23 
24     cout << endl;
25     cout << "erase 后遍历 listInt:";
26     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
27     {
28         cout << *it << " ";
29     }
30 
31     return 0;
32 }

打印结果:

 

 

 

 

erase(pos);

//删除pos位置的数据,返回下一个数据的位置

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333,444,555,666 };
 9     list<int> listInt(num, num + size(num));
10     cout << "erase 前遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     
18     cout << "erase 后,用其返回值遍历 listInt:";
19     for (list<int>::iterator it = listInt.erase(++listInt.begin()); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23 
24     cout << endl;
25     cout << "erase 后遍历 listInt:";
26     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
27     {
28         cout << *it << " ";
29     }
30 
31     return 0;
32 }

打印结果:

 

 

 

remove(elem);

//删除容器中所有与elem值匹配的元素

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,111,333,111,444 };
 9     list<int> listInt(num, num + size(num));
10     cout << "erase 前遍历 listInt:";
11     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     cout << endl;
16 
17     listInt.remove(111);
18     cout << "remove 后遍历 listInt:";
19     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20     {
21         cout << *it << " ";
22     }
23 
24     return 0;
25 }

打印结果:

 

 

 

 

 

 

 

======================================================================================================================

 

标签:容器,begin,listInt,cout,STL,List,++,num,end
来源: https://www.cnblogs.com/CooCoChoco/p/12828193.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有