STL中list学习笔记

5 篇文章 0 订阅
#include <iostream>

#include<list>

using namespace std;

#if 0

int main()

{

/**************************************************************************

//    list构造函数:

//    (1)explicit list (const allocator_type& alloc = allocator_type());

//    (2)explicit list (size_type n, const value_type& val = value_type(),

//                const allocator_type& alloc = allocator_type());

//    (3)template <class InputIterator>

//            list (InputIterator first, InputIterator last,

//    (4)list (const list& x);



    list<int> first;//创建一个空list,其中元素类型为int

    list<int> second (4,100);//list容器中为4个100

    list<int> third (second.begin(),second.end());//通过iterator类型构造

    list<int> fourth (third);//通过一个list类型构造另一个list



    int myints[] = {16,2,77,29};

    list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );//通过一个数组来构造list

*******************************************************************************/

/**************************************************************************

//    list的erase()函数:

//    iterator erase (iterator position);

//    iterator erase (iterator first, iterator last);



//    list的front()函数:

//    reference front();

//    const_reference front() const;



    list<int> mylist;

    list<int>::iterator it1,it2;

    for (int i=1; i<10; ++i) mylist.push_back(i*10);//向尾部添加元素,容器中是:10 20 30 40 50 60 70 80 90

    it1 = it2 = mylist.begin();

    advance (it2,6);//相当于it2+=6,但不能直接写it2+=6这种形式

    cout<<*it2<<endl;//输出70

    ++it1;

    it1 = mylist.erase (it1);//容器中是:10 30 40 50 60 70 80 90

    it2 = mylist.erase (it2);//容器中是:10 30 40 50 60 80 90

    ++it1;

    --it2;

    mylist.erase (it1,it2);//容器中是:10 30 60 80 90

    int a=mylist.front();//返回容器中第一个元素

    cout<<a;//输出10

************************************************************************/

    return 0;

}

#endif

#if 0

int main()

{

/***********************************************************

//    list的insert()函数:

//     (1)iterator insert (iterator position, const value_type& val);

//     (2)void insert (iterator position, size_type n, const value_type& val);

//     (3))template <class InputIterator>

//            void insert (iterator position, InputIterator first, InputIterator last);

    list<int> mylist;

    list<int>::iterator it;

    for (int i=1; i<=5; ++i)

        mylist.push_back(i);//mylist中现在有1 2 3 4 5

    it = mylist.begin();

    ++it;//it的位置会指向2所在的位置



    mylist.insert (it,10);//mylist中现在是1 10 2 3 4 5,但是it的位置会指向2所在的位置



    mylist.insert (it,2,20);//mylist中现在是1 10 20 20 2 3 4 5,但是it的位置会指向第二个20所在的位置

    --it;//it的位置会指向第一个20所在的位置



    vector<int> myvector (2,30);

    mylist.insert (it,myvector.begin(),myvector.end());//mylist中现在是1 10 20 30 30 20 2 3 4 5,it的位置会指向第二个20所在的位置

**********************************************************************/



/*******************************************************

//    list的remove()函数:

//    void remove (const value_type& val);



    int myints[]= {17,89,7,14};

    list<int> mylist(myints,myints+4);



    mylist.remove(89);//移除容器中为89的元素,若没有,则不做任何操作。

    list<int>::iterator it=mylist.begin();

    while(it!=mylist.end())

    {

        cout<<*it<<" ";

        it++;

    }

//    输出:17 7 14

*******************************************************/



    double mydoubles[]={ 12.15,  2.72, 73.0,  12.77,  3.14,

                       12.77, 73.35, 72.25, 15.3,  72.25 };

    list<double> mylist (mydoubles,mydoubles+10);



    mylist.sort();//元素从小到大排序

    mylist.unique(); //去掉重复元素



    return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值