#include<iostream>
#include<forward_list>
using namespace std;

int main()
{
    forward_list<int> flst={0,1,2,3,4,5,6,7,8,9};
    auto prev=flst.before_begin();
    auto curr=flst.begin();
    while(curr!=flst.end())
    {
        if(*curr%2)
//当找到奇数元素时,我们将prev传递给erase_after。此调用将prev之后的元素删除,即,删除curr指向的元素。然后我们将curr重置为erase_after的返回值。 curr
=flst.erase_after(prev); else { prev=curr; ++curr; } } for(auto f:flst) cout<<f<<" "; cout<<endl; return 0; }

运行结果如下:

编写程序,查找并删除forward_list<int>中的奇数元素

 

相关文章:

  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2021-09-28
  • 2022-12-23
  • 2021-12-26
  • 2021-09-07
相关资源
相似解决方案