std::forward_list and std::forward_list::push_back

后端 未结 5 1602
猫巷女王i
猫巷女王i 2020-12-01 08:29

I\'d like to use std::forward_list

Because:

Forward list is a container which supports fast insertion and removal of elements from anywhere

5条回答
  •  半阙折子戏
    2020-12-01 09:06

    The point of std::forward_list is to be an ultra-stripped-down version of std::list, so it doesn't store an iterator to the last element. If you need one, you'll have to maintain it yourself, like so:

    forward_list a;
    
    auto it = a.before_begin();
    
    for(int i = 0; i < 10; ++i)
        it = a.insert_after(it, i);
    

提交回复
热议问题