• forward_list详解


    forward_list在头文件<forward_list>中,与list类似,区别就是list时双链表,forward_list是单链表,forward_list只支持前向迭代。在访问第一个元素的时候用的是

    before_begin(),这个方法返回的是第一个元素的前一个元素,也就是虚设的元素,不能解引用,因为这个元素是假设出来的,所以+1就可以访问第一个元素了。

    #include <iostream>
    #include <forward_list>
    
    int main() {
        std::forward_list<int> forList1({1,2,3,4});
        std::forward_list<int> forList2({5,6,7});
        std::forward_list<int> forList3({8,9,10});
    
        forList2.splice_after(forList2.before_begin(),forList1);
        forList2.push_front(0);
    
        auto iter = forList2.before_begin();
        auto iterTemp = iter;
    
        while(++iterTemp != std::end(forList2))
        {
            ++iter;
        }
    //    auto iter = std::end(forList2);
        forList2.insert_after(iter,std::begin(forList3),std::end(forList3));
    
        for(auto temp : forList2)
        {
            std::cout << temp << " ";
        }
    
        return 0;
    }

    结果是:

    0 1 2 3 4 5 6 7 8 9 10

    因为这是一个forward_list没有后向迭代,所以不能用std::end(forList2);只能由前向迭代++,到end.遍历整个forward_list;

  • 相关阅读:
    day3 程序流程控制
    day2 程序流程控制
    String.prototype.formatWith
    未能找到文件“in oslyncsc.exe”
    Azure DocumentDB
    查询表中所有字段的最大长度(大数据情况)
    查询表中所有字段的最大长度
    linux开发
    sql server cvs 导入
    清除“远程桌面连接”的历史记录
  • 原文地址:https://www.cnblogs.com/boost/p/10396855.html
Copyright © 2020-2023  润新知