对STL中list的所有操作整理

#include<list>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

//STL中的链表是一个双向循环链表
//链表的迭代中只支持前移和后移
void printList(const list<int>& l) {
    for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
#if 0
//ONE: list 构造函数
void test1() {
    //创建list容器
    //idea1: 默认构造
    list<int>l1;
    //添加数据
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);
    //遍历容器
    cout << "默认构造:" << endl;
    printList(l1);

    //idea2: 区间方式构造
    list<int>l2(l1.begin(),l1.end());
    cout << "区间构造:" << endl;
    printList(l2);

    //拷贝构造
    list<int>l3(l2);
    cout << "拷贝构造:" << endl;
    printList(l3);

    //n个element
    list<int>l4(10, 1000);
    cout << "n个element:" << endl;
    ; printList(l4);
}
#endif
#if 0
void test2() {
//TWO: 赋值
    list<int>l1;

    //添加数据
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);

    list<int>l2;
    //重载operator=赋值
    l2 = l1;
    cout << "重载operator=赋值:" << endl;
    printList(l2);
    list<int>l3;
    l3.assign(l2.begin(), l2.end());
    cout << "利用assign区间赋值:" << endl;
    printList(l3);
    list<int>l4;
    l4.assign(10, 100);
    cout << "n个element:" << endl;
    printList(l4);
}
#endif
#if 0
//THREE 交换元素
void test3() {
    list<int>l1;

    //添加数据
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);

    //利用swap交换list中的元素
    list<int>l2;
    l2.assign(10, 100);
    cout << "交换前:" << endl;
    cout << "list1:" << endl;
    printList(l1);
    cout << "list2:" << endl;
    printList(l2);
    cout << "交换后:" << endl;
    l1.swap(l2);
    cout << "list1:" << endl;
    printList(l1);
    cout << "list2:" << endl;
    printList(l2);
}
#endif

#if 0
//FOUR: list大小操作
void test4() {
    list<int>l1;

    //添加数据
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);
    printList(l1);

    //判断容器是否为空
    if (l1.empty()) {
        cout << "empty" << endl;
    }
    else
    {
        cout << "not empty" << endl;
        cout << "list1的元素个数:" << l1.size() << endl;
    }

    //重新指定元素个数
    l1.resize(10);//多余空间默认用0来填充
    cout << "多余部分自动用0补充:" << endl;
    printList(l1);
    l1.resize(2);
    cout << "超出size部分会被删除掉: " << endl;
    printList(l1);//超出size部分会被删除掉
}
#endif

#if 0
void test5() {
    //FIVE: list插入和删除
    list<int>l1;
    cout << "尾插:" << endl;
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    cout << "头插:" << endl;
    l1.push_front(10);
    l1.push_front(20);
    l1.push_front(30);
    printList(l1);

    cout << "尾删:" << endl;
    l1.pop_back();
    printList(l1);
    cout << "头删:" << endl;
    l1.pop_front();
    printList(l1);

    //在迭代器位置插入
    cout << "insert 迭代器位置插入:" << endl;
    l1.insert(l1.begin(), 1000);
    l1.insert(l1.end(), 22);
    printList(l1);

    //迭代器位置删除
    list<int>::iterator it;
    it = l1.begin();
    cout << "迭代器位置删除:" << endl;
    l1.erase(it);
    printList(l1);

    //移除: 删除与输入值所有相匹配的元素
    l1.push_back(10000);
    l1.push_back(10000);
    l1.push_back(10000);
    cout << "移除前:" << endl;
    printList(l1);
    cout << "移除后:" << endl;
    l1.remove(10000);
    printList(l1);

    cout << "clear: " << endl;
    l1.clear();
    printList(l1);
}
#endif
#if 0
//SIX: 数据存取
void test6() {
    list<int>l1;

    //添加数据
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);
    printList(l1);

    //list的迭代器不支持随机访问
    //l1[0] 不可以用[]访问list容器中的元素
    //list中没有成员at: list本质是链表

    cout << "第一个元素:" << l1.front() << endl;
    cout << "最后一个元素:" << l1.back() << endl;

    //验证迭代器是不支持随机访问的
    list<int>::iterator it = l1.begin();

    //迭代器支持双向
    it++;
    it--;
    //it = it + 1; //不支持随机访问
}
#endif
#if 0
//SEVEN: 反转和排序
void test7() {
    list<int>l1;

    //添加数据
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(4);
    l1.push_back(3);
    printList(l1);

    cout << "反转后:" << endl;
    l1.reverse();
    printList(l1);
    //所有不支持随机访问迭代器的容器不可以用标准的算法
    //不支持随机访问迭代器的容器,内部会提供一些对应算法
    //sort(l1.begin(), l1.end()); //wrong
    l1.sort();//调用list的成员函数
    cout << "排升序后:" << endl;
    printList(l1);
}
#endif
int main02() {

    system("pause");
    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值