zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    通过存储过程的游标修改某个字段的全部数据
    spring cloud配置注册中心显示服务的ip地址和端口
    git几个必知托管平台
    hdu5790
    hdu5794
    hdu5739
    hdu5829
    线性规划初探
    bzoj4199
    bzoj4197
  • 原文地址:https://www.cnblogs.com/boost/p/10396855.html
Copyright © 2011-2022 走看看