STL中list学习笔记

#include 

#include

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 first;//创建一个空list,其中元素类型为int

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

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

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



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

    list 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 mylist;

    list::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<

    ++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<

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

    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 mylist;

    list::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 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 mylist(myints,myints+4);



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

    list::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 mylist (mydoubles,mydoubles+10);



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

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



    return 0;

}

#endif // 1

你可能感兴趣的:(c++,STL)