如何使用std::forward_list 查找插入数据

//
//  Forward_list.hpp
//  练习
//
//  Created by hanzhiqiang on 2017/6/11.
//  Copyright © 2017年 hanzhiqiang. All rights reserved.
//

#ifndef Forward_list_hpp
#define Forward_list_hpp

#include <stdio.h>
#include <iostream>
#include <forward_list>

using namespace std;

int main()
{
    forward_list<string> mList;
    mList.emplace_front("aaa");
    mList.emplace_front("bbb");
    mList.emplace_front("ccc");
    
    for (auto it = mList.begin(); it != mList.end(); it++)
    {
        cout<<*it<<endl;
    }
    
//    for (auto it = mList.before_begin(); it != mList.end(); it++)
//    {
//        cout<<*it<<endl;
//    }
    
//    auto itList = find(mList.begin(), mList.end(), "fff");
//    if (itList != mList.end()) \
//    {
//        mList.emplace_after(itList, "111");
//    }
//    else
//    {
//        mList.insert_after(mList.end(),"222");//c++ primer p 313 向末尾插入数据结果未知  error
//    }
    
    auto prev = mList.before_begin();
    auto curr = mList.begin();
    bool isInsert = false;
    while (curr != mList.end())
    {
        if (*curr == "fff")
        {
            curr = mList.insert_after(curr, "111");
            isInsert = true;
        }
        prev = curr;
        curr++;
    }
    
    if(!isInsert)
    {
        curr = mList.insert_after(prev, "222");//向末尾插入数据成功
    }
    
    for (auto it = mList.begin(); it != mList.end(); it++)
    {
        cout<<"插入元素后"<<*it<<endl;
    }
    
    cout<<"fuck"<<endl;
    return 0;
}

#endif /* Forward_list_hpp */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值