STL(四):list(双向链表)

STL(四):list(双向链表)

 

回顾一下之前讲过的

在任何位置插入删除都是常数时间,不支持随机存取
在这里插入图片描述C的结构体可以手动实现双向链表,可以看链表实现

支持顺序容器都有的成员函数,而且,还有:

8个成员函数用法
push_front在前面插入
8个成员函数用法
pop_front删除前面的元素
sort排序 ( list 不支持 STL 的算法 sort)
remove删除和指定值相等的所有元素
unique删除所有和前一个元素相同的元素(要做到元素不重复,则unique之前还需要 sort)
merge合并两个链表,并清空被合并的那个
reverse颠倒链表
splice在指定位置前面插入另一链表中的一个或多个元素,并在另一链表中删除被插入的元素

 

代码演示上面☝的函数

#include <list>
#include <iostream>
#include <algorithm>
using namespace std;
class A {
    private:
        int n;
    public:
        A( int n_ ) { n = n_; }
        friend bool operator<( const A & a1, const A & a2);
        friend bool operator==( const A & a1, const A & a2);
        friend ostream & operator <<( ostream & o, const A & a);
};

        bool operator<( const A & a1, const A & a2) {
            return a1.n < a2.n;
}
bool operator==( const A & a1, const A & a2) {
            return a1.n == a2.n;
}
ostream & operator <<( ostream & o, const A & a) {
            o << a.n;
            return o;
}

template <class T>
void PrintList(const list<T> & lst) {
//不推荐的写法,还是用两个迭代器作为参数更好
        int tmp = lst.size();
        if( tmp > 0 ) {
                typename list<T>::const_iterator i; 
            i = lst.begin();
                for( i = lst.begin();i != lst.end(); i ++)
                    cout << * i << ",";
}
}// typename用来说明 list<T>::const_iterator是个类型,在vs中不写也可以

int main() {
    list<A> lst1,lst2;
    lst1.push_back(1);lst1.push_back(3); 
    lst1.push_back(2);lst1.push_back(4); 
    lst1.push_back(2);
    lst2.push_back(10);lst2.push_front(20);
    lst2.push_back(30);lst2.push_back(30);
    lst2.push_back(30);lst2.push_front(40);
    lst2.push_back(40);
    cout << "1) "; PrintList( lst1); cout << endl;
    // 1) 1,3,2,4,2,
    cout << "2) "; PrintList( lst2); cout << endl;
    // 2) 40,20,10,30,30,30,40,
    lst2.sort();
    cout << "3) "; PrintList( lst2); cout << endl;
    //3) 10,20,30,30,30,40,40,
    lst2.pop_front(); 
    cout << "4) "; PrintList( lst2); cout << endl;
    //4) 20,30,30,30,40,40,
    lst1.remove(2); //删除所有和A(2)相等的元素
    cout << "5) "; PrintList( lst1); cout << endl;
    //5) 1,3,4,
    lst2.unique(); //删除所有和前一个元素相等的元素
    cout << "6) "; PrintList( lst2); cout << endl;
    //6) 20,30,40,
    lst1.merge (lst2); //合并 lst2到lst1并清空lst2
    cout << "7) "; PrintList( lst1); cout << endl;
    //7) 1,3,4,20,30,40,
    cout << "8) "; PrintList( lst2); cout << endl;
    //8)
    lst1.reverse();
    cout << "9) "; PrintList( lst1); cout << endl;
    //9) 40,30,20,4,3,1,
    lst2.push_back (100);lst2.push_back (200);
    lst2.push_back (300);lst2.push_back (400);
    list<A>::iterator p1,p2,p3;
    p1 = find(lst1.begin(),lst1.end(),3);
    p2 = find(lst2.begin(),lst2.end(),200);
    p3 = find(lst2.begin(),lst2.end(),400);
    lst1.splice(p1,lst2,p2, p3); 
    //将[p2,p3)插入p1之前,并从lst2中删除[p2,p3)
    cout << "10) "; PrintList( lst1); cout << endl;
    //10) 40,30,20,4,200,300,3,1,
    cout << "11) "; PrintList( lst2); cout << endl;
    //11) 100,400,
    return 0;
}

output:
在这里插入图片描述

 
这一个部分还有一个内容关于函数对象和函数指针函数对象和函数指针的简介

 
学会程序和算法,走遍天下也不怕

在这里插入图片描述香格里拉松赞林寺

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值