C++primer 9.3.3节练习

练习9.25

如果elem1和elem2相等,则什么都不会发生,因为elem1是要删除的第一个元素,而elem2是删除最后一个元素的后一个位置,所以什么都不会发生;

如果elem2是尾后迭代器,那么删除的是elem1到容器尾所有元素;

如果皆为尾后迭代器,仍然什么都不会发生。

练习9.26

 1 #include <iostream>
 2 #include <vector>
 3 #include <list>
 4 #include <deque>
 5 #include <string>
 6 #include <iterator>
 7 
 8 using namespace std;
 9 
10 
11 int main()
12 {
13     int ia[] = { 0,1,1,2,3,5,8,13,21,55,89 };
14     vector<int> v;
15     list<int> l;
16     for (auto c : ia)
17     {
18         v.push_back(c);
19         l.push_back(c);
20     }

    //int ia[] = {0,1,1,2,3,5,8,13,21,55,89};     
    //vector<int> ivec(ia,end(ia));     
    //list<int> ilist(ivec.begin(),ivec.end());     //这种复制方法更简单,效率更高
21     for (auto it = v.begin(); it != v.end();)
22     {
23         if ((*it) % 2 == 0)
24             it = v.erase(it);
25         else
26             ++it;
27     }
28     for (auto it1 = l.begin(); it1 != l.end();)
29     {
30         if ((*it1) % 2 != 0)
31             it1 = l.erase(it1);
32         else
33             ++it1;
34     }
35     for (auto c : v)
36         cout << c << endl;
37     cout << endl;
38     for (auto c : l)
39         cout << c << endl;
40     system("pause");
41     return 0;
42 }

 

转载于:https://www.cnblogs.com/wuyinfenghappy/p/7340055.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值