STL-list支持的集中常见操作的用法

list支持的常见的操作:
#include<iostream>
#include <list>
#include <vector>
#include <cctype>
#include <cmath>


using namespace std;

//list表示的是顺序容器,允许在列表中的任意位置进行插入和删除操作,并且可以在两个方向上面进行迭代
//同时list是双向循环链表,每一个元素都知道前一个和后一个元素的位置,但是forward_list是单向链表,
//同时插入和删除是很快速的,但是随机访问元素就会十分复杂;
//对于list以及forward_list都是通过模板实现的泛型编程list的模板形式为:template<class T,class A
//lloc=allocator<T>>class list;

bool myconparsion(double first,double second){
    return (int(first)<int(second));
}

bool is_old(const int& value){
    return (value%2==0);
}

bool compare_nocase(const string& first,const string& second){
    unsigned int i=0;
    while(i<first.length() && i<second.length()){
        if(tolower(first[i]<tolower(second[i]))) return true;
        else if (tolower(first[i]>tolower(second[i]))) return false;
        ++i;
    }
    return (first.length() < second.length());
}

bool same_intergal_part(double first,double second){
    return (int(first)==int(second));
}

class is_near{
    bool operator()(double first,double second){
        return (fabs(first-second)<3);
    }
};


int main()
{
    //.assign():class Template<class InputIterator> void assign(InputIterator first,InputIter
    //ator last):表示接受的是一个迭代器的范围,用来构造list;很明显进行构造的过程中,原始空间的数
    //据就会被销毁;
    list<int> first, second;
    first.assign(7, 100);

    //.assign():void assign(size_t n,const value_type& val);
    second.assign(first.begin(), first.end()); //执行的是一个copy操作;
    vector<int> myvector = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    first.assign(myvector.begin(), myvector.end());
    cout << "list first contains: ";
    for (auto &X : first)
        cout << X << " ";
    cout << endl;
    cout << "second contains: ";
    for (auto &X : second)
        cout << X << " ";
    cout << endl;
    //.back()对于这种访问元素的类型,一般有两种:reference back():表示元素是可以进行修改的;cons
    //t_reference()const;表示里面的元素是不可以进行修改的;类似的还有.front();

    first.back() = 10;
    cout << first.back() << endl;

    //.begin() .end()是两个迭代器通常使用来访问里面的元素的;
    list<int>::iterator It;
    cout << "first changed: ";
    for (It = first.begin(); It != first.end(); ++It)
    {
        *It += 1;
        cout << ' ' << *It;
    }
    cout << endl;

    //.cbegin()以及.cend()是用来表示不进行元素的修改操作的;
    cout << "first changed: ";
    for (decltype(first.cbegin()) It = first.cbegin(); It != first.cend(); ++It)
    {
        cout << ' ' << *It;
    }
    cout << endl;

    //.clear()用于清空里面的所有元素,函数原型是void clear();


    //.crbegin(),crend():是两个用来进行反向迭代,并且不能够进行修改元素的迭代器;
    cout << "first changed: ";
    for (decltype(first.crbegin()) It = first.crbegin(); It != first.crend(); ++It)
    {
        cout << ' ' << *It;
    }
    cout << endl;

    //.rgegin(),.rend() 是用来进行反向迭代,并且能够修改元素的迭代器
    cout << "first changed: ";
    for (decltype(first.rbegin()) It = first.rbegin(); It != first.rend(); ++It)
    {
        *It += 2;
        cout << ' ' << *It;
    }
    cout << endl;

    //.emplace()通过提供一个位置进行新元素的插入,新元素的构造可以使用其构造函数进行,函数原型包
    //template<class... Args> iterator emplace(const_iterator position,Args&&.. args);

    list<pair<int, char>> mynewlist;
    mynewlist.emplace(mynewlist.begin(), 100, 'x');
    mynewlist.emplace(mynewlist.begin(), 10, 'n');
    mynewlist.emplace(mynewlist.begin(), 1, 'x');
    mynewlist.emplace(mynewlist.begin(), 100, 'x');
    mynewlist.emplace(mynewlist.begin(), 10, 'n');
    mynewlist.emplace(mynewlist.begin(), 100, 'x');
    mynewlist.emplace(mynewlist.begin(), 10, 'n');

    cout << "mynewlist contains: ";
    for (auto X : mynewlist)
        cout << "(" << X.first << " " << X.second << ")" << " ";
    cout << endl;
    //.emplace_back():用于将插入的元素放到当前数组元素的最后面;

    mynewlist.emplace_back(1, 'a');
    mynewlist.emplace_back(2, 'b');
    mynewlist.emplace_back(3, 'c');
    mynewlist.emplace_back(4, 'd');

    cout << "mynewlist contains: ";
    for (auto X : mynewlist)
        cout << "(" << X.first << " " << X.second << ")" << " ";
    cout << endl;
    //.front_back()用于将元素放到当前list的最前面;
    mynewlist.emplace_front(4, 'd');
    mynewlist.emplace_front(3, 'c');
    mynewlist.emplace_front(2, 'b');
    mynewlist.emplace_front(1, 'a');
    cout << "mynewlist contains: ";
    for (auto X : mynewlist)
        cout << "(" << X.first << " " << X.second << ")" << " ";
    cout << endl;

    //.empty()用于判断是否为空的操作;

    cout << boolalpha << mynewlist.empty() << noboolalpha <<  endl;

    //.erase()通常包含两种声明iterator erase(iterator position);这个适用于函数某个位置的元素,
    cout << "mynewlist.erase(position): ";
    list<pair<int, char>>::iterator it1, it2;
    it1 = it2 = mynewlist.begin();
    advance(it1, 1);
    //需要了解的是这里的新的返回值是必须进行接收的,因为删除元素之后it1的位置已经失效,所以应该
    //进行保存;
    it1 = mynewlist.erase(it1);
    for (auto X : mynewlist)
        cout << "(" << X.first << " " << X.second << ")" << " ";
    cout << endl;

    //iterator erase(iterator first,iterator last):这个是用于删除某个迭代器范围里面的元素;

    cout << "mynewlist.rease(iterator):";
    advance(it2, 4);

    mynewlist.erase(it1, it2);
    for (auto X : mynewlist)
        cout << " (" << X.first << X.second << ")" << " ";
    cout << endl;

    //.get_allocator:用于返回一个list容器分配的空间适配器的拷贝;

    list<int> mylist;
    int *p;
    p = mylist.get_allocator().allocate(5);

    for (int i = 0; i < 5; i++)
        p[i] = i;
    cout << "The list arry contains: ";
    for (int i = 0; i < 5; i++)
        cout << " " << p[i] << " ";
    cout << endl;
    mylist.get_allocator().deallocate(p, 5);


    //void insert(iterator position,size_tpye n,const value_type& val);
    //template<class InputIterator>
    //void insert(iterator positon,InputIterator first,InpurIterator last);
    //.insert 的函数一共包含三个:iterator insert(iterator position,const value_type& val);
    list<int> mylist1;
    for (int i = 0; i < 10; ++i)
        mylist1.push_back(i * 2);
    cout << "mynewlist1 contains: ";
    for (auto X : mylist1)
        cout <<  X << " ";
    cout << endl;
    list<int>::iterator It1;
    It1 = mylist1.begin();
    advance(It1, 3);
    mylist1.insert(It1, 10);
    mylist1.insert(It1, 2, 1);
    cout << "mynewlist1 contains: ";
    for (auto X : mylist1)
        cout << X << " ";
    cout << endl;

    vector<int> myvector1;
    for (int i = 0; i < 10; ++i)
        myvector1.push_back(i + 11);
    mylist1.insert(It1, myvector1.begin(), myvector1.end());
    cout << "mylist1 contains: ";
    for (auto X : mylist1)
        cout << X << " ";
    cout << endl;

    //.merge()包含void merge(list& X);以及template<class Compare> void merge(list& X,Compare
    //comp);对于merge来说,两个序列之前必须是有序的;

    list<double> newfirst, newsecond;
    newfirst.push_back(1.2);
    newfirst.push_back(2.1);
    newfirst.push_back(1.1);
    newfirst.push_back(1.0);
    newfirst.push_back(0.99);
    newfirst.push_back(3.4);

    newsecond.push_back(11.3);
    newsecond.push_back(13.2);
    newsecond.push_back(2.7);
    newsecond.push_back(12.5);

    newfirst.sort();
    newsecond.sort();
    newfirst.merge(newsecond);
    cout << "newfirst contains: ";
    for(auto X:newfirst)
        cout << X << " ";
    cout <<endl;
    newsecond.push_back(2.1);
    first.merge(second,myconparsion);

    cout << "newfirst contains: ";
    for(auto X:newfirst)
        cout << X << " ";
    cout <<endl;

    //.=运算符重载,主要是用于两个list对象之间进行赋值操作,两者之间的大小没有要求;
    mylist=mylist1;

    //.pop_back(),pop_front()分别用于弹出最后一个元素和第一个元素,效率比较高;
    mylist1.pop_back();
    mylist1.pop_front();

    //.push_back(),push_front(),分别用于在当前链表的开始和结束地方添加元素
    mylist.push_back(2);
    mylist.push_front(1);

    //.remove()用于移除某些元素,可以直接是元素的值,可以用于移除里面的所有等于这个元素的值的元
    //素,void remove(const value_type& val);
    cout << "mylist1.remove() contains: ";
    mylist1.remove(14);
    for(auto X:mylist1)
        cout << X << " ";
    cout << endl;
    //.remove_if()用于移除某些曼泽条件的值,通常需要传递一个bool类型的函数;
    mylist1.remove_if(is_old);
    cout << "newfirst contains: ";
    for(auto X:mylist1)
        cout << X << " ";
    cout <<endl;

    //.resize()在C++11里面有两个版本:void  resize(size_type n)以及void resize(size_type n,
    //const value_type& val);这两个的作用都是用来扩展list 的大小用于容纳更多的元素,前者用于
    //扩展到指定的大小,后者可以用于添加重复的值

    list<int> mylist2;
    mylist2.resize(3);
    cout << "mylist2 contains: ";
    for(auto X:mylist2)
        cout << X << " ";
    cout <<endl;
    mylist2.resize(13,6);
    cout << "mylist2 contains: ";
    for(auto X:mylist2)
        cout << X << " ";
    cout <<endl;

    //.reverse()用于翻转list链表
    mylist1.reverse();
    cout << "mylist1 contains: ";
    for(auto X:mylist1)
        cout << X << " ";
    cout << endl;

    //.size()用于获取list的大小,函数声明为:size_type size() const noexcept;


    //.sort()可以用于list进行排序的方法,函数的声明包括:void sort(),template <class Compare>
    //void sort(Compare comp);
    list<string> liststring;
    list<string>::iterator StringIt;
    liststring.push_back("One");
    liststring.push_back("Two");
    liststring.push_back("Three");

    liststring.sort();
    cout << "liststring contains: ";
    for(auto X:liststring)
        cout << X << " ";
    cout << endl;


    liststring.sort(compare_nocase);
    cout << "liststring contains: ";
    for(auto X:liststring)
        cout << X << " ";
    cout << endl;

    //.splice()方法表示的是:用于将元素X进行转换,并且插入到指定位置,在插入元素的同时修改容器的
    //大小这个操作是不涉及构造函数或者是析构函数的,也不关心X是左值还是右值或者这个类型是否支持
    //移动构造函数;
    //通常包含三个版本:
    //1.void splice(const_iterator position,list& x)
    //1.1.void splice(const_iterator position,list&& x) //这里涉及右值引用,也就是移动构造函数;
    //2.0 void splice(const_iterator position,list& x,const_iterator i)
    //2.1 void splice(const_iterator position,list&& x,const_iteraotr i);
    //3.1 void splice(const_iterator position, list& x,const_iteraotr first,const_iterator last);
    //3.2 void splice(const_iterator position, list&& x,const_iterator first,const_iterator last);
    //对于涉及右值引用的知识,可以参考移动构造函数的相关函数;

    list<int> listmy1;
    list<int> listmy2;
    list<int>::iterator ListIt;
    for(int i=0;i<9;++i)
        listmy1.push_back(i+2);
    for(int i=0;i<8;++i)
        listmy2.push_back(i*10);
    ListIt = listmy1.begin();
    advance(ListIt,1);
    listmy1.splice(ListIt,listmy2);
    listmy2.splice(listmy2.begin(),listmy1,ListIt);
    ListIt=mylist1.begin();

    advance(ListIt,3);
    mylist1.splice(mylist1.begin(),mylist1,ListIt,listmy1.end());

    cout << "listmy1 contains: ";
    for(auto X:listmy1)
        cout << X << " ";
    cout << endl;

    cout << "listmy2 contains: ";
    for(auto X:listmy2)
        cout << X << " ";
    cout << endl;

    //.swap()是用来交换两个list对象的;函数声明:void swap(list &X);
    listmy1.swap(listmy2);
    cout << "listmy2 contains: ";
    for(auto X:listmy2)
        cout << X << " ";
    cout << endl;

    //.unique 包含两个版本:void unique()用于去除相邻元素之间的重复元素;
    //template <class BinaryPredicate> void unique(BinaryPredict binary_pred),可以传递某个函数
    //删除;
    double newdoubles[]={1.2,3.4,1.5,5.6,7.0,1.4,12,23.0,45.8,21.7,12.9,16.7,11.4,12.7,18.9};   
    list<double> doublelist(newdoubles,newdoubles+16);

    doublelist.sort();
    doublelist.unique();

    doublelist.unique(same_intergal_part);
    doublelist.unique(is_near());

    cout << " doublelist contains: ";
    for(list<double>::iterator It=doublelist.begin();It!=doublelist.end();++It)
        cout << *It << " ";
    cout << endl;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值