C++学习第二十三天(stack、queue、list容器)

  1. stack容器基本概念
    stack是一种先进后出的数据结构,他只有一个出口,栈中只有顶端的元素才能被外界使用,因此栈不允许有遍历行为。(栈可以返回元素个数,可以判断容器是否为空)

1.1. 常用接口

默认构造函数:stack stc;
拷贝构造函数:stack(const stack &stk);
赋值操作:stack &operator=(const stack &stk);

示例:

#include <iostream>
#include <stack>

using namespace std;

void test01()
{
    stack<int> s;
    //入栈
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);
    //只要栈不为空就查看栈,并执行出栈操作
    cout << "出栈前栈的大小为:" << s.size() << endl;
    while (!s.empty())
    {
        //查看栈顶元素
        cout << "栈顶元素为:" << s.top() << endl;
        //出栈
        s.pop();
    }
    cout << "出栈后栈的大小为:" << s.size() << endl;
}

int main(void)
{
    test01();

    return 0;
}

  1. queue容器的概念
    概念:queue是一种先进先出的数据结构,它有两个出口;队列容器运行从一端新增元素,从另一端删除元素;队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为;队列中进数据称为入队,出数据称为出队。

构造函数和stack使用方式相同
接口:示例

#include <iostream>
#include <queue>

using namespace std;

class Person
{
public:
    Person(string name, int age)
    {
        this->m_name = name;
        this->m_age = age;
    }

    string m_name;
    int m_age;
};

void test01()
{
    //创建队列容器
    queue<Person> q;
    //准备数据
    Person p1("唐僧" , 10);
    Person p2("孙悟空" , 20);
    Person p3("猪八戒" , 30);
    Person p4("沙僧" , 40);
    //入队
    q.push(p1);
    q.push(p2);
    q.push(p3);
    q.push(p4);
    //判断队列是否为空,查看队头、队尾、出队
    while (!q.empty())
    {
        cout << "出队前队头数据的姓名为:" << q.front().m_name << "年龄为:" << q.front().m_age << endl;
        cout << "出队前队尾数据的姓名为:" << q.back().m_name << "年龄为:" << q.back().m_age << endl;
        cout << "出队前队列的大小为:" << q.size() << endl;
        //出队
        q.pop();
        cout << "出队后队头数据的姓名为:" << q.front().m_name << "年龄为:" << q.front().m_age << endl;
        cout << "出队后队尾数据的姓名为:" << q.back().m_name << "年龄为:" << q.back().m_age << endl;
        cout << "出队后队列的大小为:" << q.size() << endl;
    }
}

int main(void)
{
    test01();

    return 0;
}

  1. list容器的概念

将数据进行链式存储,链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。链表是由一系列结点组成,结点是由一个存储数据元素的数据域和一个存储下一个结点地址的指针域。

stl中的链表是一个双向循环链表(一个指针域有两个指针一个指向前一个结点的地址另一个指向后一个结点的地址,且第一个结点和最后一个结点是连通的)。

优点:可以对任意位置进行快速的插入删除元素,不需要移动大量的元素,只需要修改链表即可;采用动态存储分配,不会造成内存内存浪费和溢出;
缺点:对于容器的遍历的速度没有数组快,占用的空间比数组大。

由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。且有一个重要的性质,插入和删除操作都不会造成原有list迭代器失效,这在vector容器中是不成立的,因为如果插入数据太多,vector容器会找一个新的地址空间将数据存放进去。

list构造函数:

#include <iostream>
#include <list>

using namespace std;

void printList(const list<int> &l)
{
    for(list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test01()
{
    //创建list容器
    list<int> l1;//默认构造

    //添加数据
    l1.push_back(10);
    l1.push_back(20);
    l1.push_back(30);
    l1.push_back(40);

    //遍历容器
    printList(l1);

    //区间构造
    list<int> l2(l1.begin(), l1.end());
    printList(l2);

    //拷贝构造
    list<int> l3(l2);
    printList(l3);
    
    //n个elem
    list<int> l4(10,1000);
    printList(l4);
}

int main(void)
{
    test01();

    return 0;
}

list赋值和交换:

#include <iostream>
#include <list>

using namespace std;

void printList(const list<int> &l)
{
    for(list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test01()
{
    //创建list容器
    list<int> l1;//默认构造

    //添加数据
    l1.push_back(10);
    l1.push_back(20);
    l1.push_back(30);
    l1.push_back(40);

    printList(l1);

    list<int> l2;
    l2 = l1;//operator等号赋值
    printList(l2);

    //assign区间方式赋值
    list<int> l3;
    l3.assign(l2.begin(), l2.end());
    printList(l3);

    //n个elem
    list<int> l4;
    l4.assign(10,1000);
    printList(l4);
}

//交换
void test02()
{
    list<int> l1;
    l1.push_back(10);
    l1.push_back(20);
    l1.push_back(30);
    l1.push_back(40);

    list<int> l2;
    l2.assign(10,100);

    cout << "交换前:" << endl;
    printList(l1);
    printList(l2);

    //交换
    l1.swap(l2);

    cout << "交换后:" << endl;
    printList(l1);
    printList(l2);
}

int main(void)
{
    test01();
    test02();

    return 0;
}

list容器大小操作:

#include <iostream>
#include <list>

using namespace std;

void printList(const list<int> &l)
{
    for(list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test01()
{
    //创建list容器
    list<int> l1;//默认构造

    //添加数据
    l1.push_back(10);
    l1.push_back(20);
    l1.push_back(30);
    l1.push_back(40);

    printList(l1);

    //判断容器是否为空
    if(l1.empty())
    {
        cout << "l1为空" << endl;
        cout << "l1的大小为:" << l1.size() << endl;
    }
    else
    {
        cout << "l1不为空" << endl;
    }

    //重指定l1的大小
    l1.resize(10,1000);//指定大小为10默认值用1000代表
    printList(l1);
    l1.resize(2);//指定大小为2,去除多余元素
    printList(l1);
}

int main(void)
{
    test01();

    return 0;
}

list容器的插入和删除:

#include <iostream>
#include <list>

using namespace std;

void printList(const list<int> &l)
{
    for(list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test01()
{
    //创建list容器
    list<int> l1;

    //尾插
    l1.push_back(10);
    l1.push_back(20);
    //头插
    l1.push_front(100);
    l1.push_front(200);
    printList(l1);

    //尾删
    l1.pop_back();
    //头删
    l1.pop_front();
    printList(l1);

    //insert插入
    l1.insert(l1.begin(),100);//利用起始迭代器在最前面插入1000
    printList(l1);
    list<int>:: iterator it = l1.begin();
    l1.insert(++it,345);//相当于在第二个位置插入了345
    printList(l1);

    //删除
    it = l1.begin();
    l1.erase(it);
    printList(l1);

    //移除
    l1.push_back(8888);
    l1.push_back(8888);
    l1.push_back(8888);
    l1.remove(8888);//直接指定删除所有的元素
    printList(l1);

    //清空
    l1.clear();
    printList(l1);
}

int main(void)
{
    test01();

    return 0;
}

list容器数据存取:
list容器不支持下标访问([])和跳跃式访问(at),原因是因为list本质是链表,不是用连续线性空间存储数据,迭代器也不支持随即访问的。

#include <iostream>
#include <list>

using namespace std;

void printList(const list<int> &l)
{
    for(list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test01()
{
    //创建list容器
    list<int> l1;

    //添加数据
    l1.push_back(10);
    l1.push_back(20);
    l1.push_back(30);
    l1.push_back(40);

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

    //验证迭代器是不支持随机访问的
    list<int>::iterator it = l1.begin();//不报错
    it++;//允许的
    it--;//允许的,说明支持双向
    //it += 1;//防止+2、+3等,说明该迭代器不支持随机访问。
}

int main(void)
{
    test01();

    return 0;
}

list反转和排序:

#include <iostream>
#include <list>
#include <algorathm>

using namespace std;

void printList(const list<int> &l)
{
    for(list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}


//降序函数
bool myCompare(int v1 , int v2)
{
    //降序,就让第一个数大于第二个数
    return v1 > v2;
}

void test01()
{
    //创建list容器
    list<int> l1;

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

    cout << "升序排序前";
    printList(l1);

    //排序(所有不支持随机访问迭代器的容器,不可以使用标准算法,它内部会提供一些算法)
    //l1.sort(l1.begin(), l1.end());//不支持标准算法
    l1.sort();//默认升序
    cout << "升序排序后,即反转前";
    printList(l1);

    //降序
    l1.sort(myCompare);
    cout << "降序排序后" ;
    printList(l1);

    //反转后
    l1.reverse();
    cout << "反转后";
    printList(l1);
}

int main(void)
{
    test01();

    return 0;
}

list自定义类型排序案例:

#include <iostream>
#include <list>
#include <string>

using namespace std;

class Person
{
public:
    Person(string name, int age, double height)
    {
        this->m_name = name;
        this->m_age = age;
        this->m_height = height;
    }
public:
    string m_name;
    int m_age;
    double m_height;
};

//指定排序规则
bool comparePerson(Person &p1, Person &p2)//参数说明是两个Person类型进行对比
{
    //年龄作升序
    if(p1.m_age == p2.m_age)
    {
        //如果年龄相同,按照升高降序排序
        return p1.m_height > p2.m_height;
    }
    return p1.m_age < p2.m_age;
}
void test01()
{
    list<Person> l1;

    Person p1("刘备", 35, 175.1);
    Person p2("曹操", 45, 180.2);
    Person p3("孙权", 40, 170.3);
    Person p4("赵云", 25, 190.5);
    Person p5("张飞", 35, 160.4);
    Person p6("关羽", 35, 200.8);

    l1.push_back(p1);
    l1.push_back(p2);
    l1.push_back(p3);
    l1.push_back(p4);
    l1.push_back(p5);
    l1.push_back(p6);

    for(list<Person>::iterator it = l1.begin(); it != l1.end(); it++)
    {
        cout << "姓名:" << (*it).m_name << "  年龄:" << (*it).m_age << "  生高:" << it->m_height << endl;
    }

    //排序
    cout << "---------------------" << endl;
    cout << "排序后" << endl;
    l1.sort(comparePerson);//操作自定义类型时,排序需要指定它的规则(必须写回调函数或仿函数)
    for(list<Person>::iterator it = l1.begin(); it != l1.end(); it++)
    {
        cout << "姓名:" << (*it).m_name << "年龄:" << (*it).m_age << "生高:" << it->m_height << endl;
    }
}

int main(void)
{
    test01();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值