C++ stl 学习4-- forward_list

forward_list 是单向链表,只有一端能插入元素。

forward_list常用的函数如下

 

注意,一般容易有的size成员函数forward_list没有,

需要获取的话用distance(begin(list), end(list)) 计算

 

before_begin() // 第一个元素的前一个元素,有点像vector里面的rend(),但是forward_list不能逆向遍历

不要解引用,一般用作emplace_after, insert_after, erase_after or splice_after的参数

The iterator returned shall not be dereferenced: It is meant to be used as an argument for member functions 

emplace_after, insert_after, erase_after or splice_after, to specify the beginning of the sequence as the location where the action is performed

emplace_front()   // 在第一个元素处生成元素, 调用构造函数

inserts a new element at the beginning of the forward_list, right before its current first element. This new element is constructed in place using args as the arguments for its construction.

push_front()    // 在第一个元素处插入元素, 调用构造函数  +  拷贝(移动)构造函数

Inserts a new element at the beginning of the forward_list, right before its current first element. The content of val is copied (or moved) to the inserted element.

emplace_after()  //在指定位置的后面生成元素

The container is extended by inserting a new element after the element at position. This new element is constructed in place using args as the arguments for its construction

insert_after()  // 在指定位置的后面插入元素,调用构造 + 移动(复制)构造函数,返回值是指向插入的最后一个元素的迭代器

An iterator that points to the last of the newly inserted elements, or position if no element was inserted.

erase_after() //删除指定位置后面那个元素,返回值是移除元素的下一个元素

iterator erase_after (const_iterator position);

iterator erase_after (const_iterator position, const_iterator last);

Removes from the forward_list container either a single element (the one after position) or a range of elements ((position,last))

An iterator pointing to the element that follows the last element erased by the function call, which is last for the second version.

If the operation erased the last element in the sequence, the value returned is end.

splice_after()   //将另一个forward_list 里面元素转移到指定位置的后面

Transfers elements from fwdlist into the container inserting them after the element pointed by position

This effectively inserts those elements into the container and removes them from fwdlst, altering the sizes of both containers.

The operation does not involve the construction or destruction of any element.

They are transferred, no matter whether fwdlst is an lvalue or an rvalue, or whether the value_type supports move-construction or not

 

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

int main()
{
    forward_list<int>  a{1,3,5,7,9};
    forward_list<int>  b{2,4,6,8,10};
    forward_list<int>  c{2,3,4,5,6};
    auto it = a.erase_after(a.begin());   // 1 5 7 9  *it = 5
    a.merge(b);       // a 1 2 4 5 6 7 8 9 10  b空
    b.emplace_front(12);   //  b 12
    b.push_front(15);    // 15  12
    b.insert_after(b.before_begin(), 13);   // 13  15  12
    b.splice_after(b.before_begin(), c);   // 2 3 4 5 6 13 15 12
    cout << a << endl;
    cout << b << endl;
    return 0;
}

 

posted @ 2021-02-12 12:50  goodluck14  阅读(262)  评论(0)    收藏  举报