c++容器笔记

首先,先声明一个头文件

然后,创建一个容器L,然后用迭代器去读取里面的数,

for(list<int>::const_iterator it=L.begin();it!=L.end();it++)

{

cout<<*it;

}

有一些迭代器是可以随机访问的,有一些是双向的,根据迭代器的不同而不同,

利用L.push_front(),去前面插入,利用L.push_back,在后面插入,一般的调换位置可以利用L.sort(),默认是升序,要改成降序的话,需要传一个布尔函数进去,让第一个>第二个

高级运用

list<Person>L

创建一个类

class Person

{

int age;

string name;

int height;

};

void test05()
{
    //高级排序
    list<Person>L;

    Person p1("刘备", 35, 175);
    Person p2("关羽", 45, 170);
    Person p3("张飞", 40, 180);
    Person p4("lzl",25,176);
    //插入数据
    L.push_back(p1);
    L.push_back(p2);
    L.push_back(p3);
    L.push_back(p4);
    for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
    {
        cout << (*it).m_Age << "  " << (*it).m_Height << "  " << (*it).m_Name << endl;
    }
    //排序
    L.sort(comPare);
    cout << "---------------------"<<endl;
    for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
    {
        cout << (*it).m_Age << "  " << (*it).m_Height << "  " << (*it).m_Name << endl;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值