标准模板库——STL(二)

理解容器 list:

1.list 内部结构是线性双向链表,其内部所占内存空间不是连续的,其内部是通过指针将各节点连接起来的,故随机访问元素效率并不高,但是对其插入和删除元素非常快。

2.

通用操作用以下参考程序表现:

#include <iostream>
#include<list>
#include<cstring>
using namespace std;
class student          
{
    private:
        char m_name[20];
        int m_age;
    public:
        student(char *name,int age)
        {
            strcpy(m_name,name);
            m_age=age;
        }
        char *Getname()
        {
            return m_name;
        }
        int Getage();
};

int student::Getage()
{
    return m_age;
}

int main()
{
    list<student> L;
    student s1("aa",20);
    student s2("bb",21);
    student s3("cc",22);
    student s4("dd",23);
    student s5("ee",24);
    //元素入容器;
    L.push_back(s1);
    L.push_back(s2);
    L.push_front(s3);
    L.push_front(s4);
  //打印容器内元素信息
    for(list<student>::iterator it = L.begin();it != L.end();it++)
    {
        cout<<"name:"<<it->Getname()<<" "<<"age:"<<it->Getage()<<endl;
    }
    cout<<"*************************************"<<endl;
    L.reverse();       //将容器元素反序
    for(list<student>::iterator it = L.begin();it != L.end();it++)
    {
        cout<<"name:"<<it->Getname()<<" "<<"age:"<<it->Getage()<<endl;
    }
    cout<<"*************************************"<<endl;
    L.front()=s5;
    for(list<student>::iterator it = L.begin();it != L.end();it++)
    {
        cout<<"name:"<<it->Getname()<<" "<<"age:"<<it->Getage()<<endl;
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值