Abbreviated list specific vessel and forward_list algorithm

List type list and forward_list have a unique sort, merge, remove, reverse, and unique, while the generic version can not be used for both types, because of the different iterations as requested, a generic version of the iterator needs more support operating.

In addition revrese no parameters, the other can pass a predicate.

splice list is unique to the type of algorithm, and there is no generic version.

#include<bits/stdc++.h>
using namespace std;
int main(void) {
    list<int>n1{ 1,2,3,4,5,6,7 }, n2{ 11,22,33 };
    forward_list<int>n3{ 1,2,3,4,5,6,7 },n4{ 11,22,33 };
    auto pi = n1.begin(),pi2=n2.begin(),pi3=n2.end();
    auto pj = n3.begin(),pj2=n4.begin(),pj3=n4.end();
    n1.splice(pi,n2);        //将n2中的元素放到pi之前的位置
    n3.splice_after(pj, n4); //将n4中的元素放到pj之后的位置
    for (auto i : n1)cout << i << " "; cout << endl;
    for (auto i : n3)cout << i << " "; cout << endl;
    return 0; 
}

Output

11 22 33 1 2 3 4 5 6 7
1 11 22 33 2 3 4 5 6 7

list and has three versions of the splice forward_list

n1.splice(pi,n2);           //将n2中的元素放到pi之前的位置,此时两个链表可以不同
n1.splice(pi,n2,pi2);       //将n2中pi2指向的元素放到pi之前的位置,pi2必须是一个指向n2的有效迭代器,此时两个链表可以相同
n1.splice(pi,n2,pi2,pi3);   //将n2中[pi2,pi3)范围中的元素放到pi之前的位置,此时两个链表可以相同,但是pi不能指向范围中的元素

n3.splice_after(pj, n4); //将n4中的元素放到pj之后的位置
n3.splice_after(pj, n4,pj2); //将n4中的pj2指向的元素放到pj之后的位置,pj2必须是一个指向n4的有效迭代器,此时两个链表可以相同
n3.splice_after(pj, n4,pj2,pj3); //将n4中(pj2,pj3)范围中的元素放到pj之前的位置,此时两个链表可以相同,但是pj不能指向范围中的元素

For the second version of the above splice output follows

11 1 2 3 4 5 6 7
1 22 2 3 4 5 6 7

For the third version of the above splice output follows

11 22 33 1 2 3 4 5 6 7  //list是[pi2,pi3)
1 22 33 2 3 4 5 6 7     //forward_list是(pj2,pj3)

Guess you like

Origin www.cnblogs.com/FlyerBird/p/11779471.html