码迷,mamicode.com
首页 > 编程语言 > 详细

c++ list使用方式

时间:2020-01-25 10:17:12      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:tor   clear   std   return   const   include   区间   cout   素数   

c++ list

定义和初始化

    listlst1; // 创建空list
    listlst2(6); //创建含有6个元素的list
    listlst3(3, 2); // 创建含有三个元素的list
    listlst4(lst2); // 使用ls2初始化ls4
    listlst5(lst2.begin(), lst2.end()); // 同ls4

list常用操作函数

    lst1.assign() // 给list赋值
    lst1.front() // 返回第一个元素
    lst1.back() // 返回最后一个元素
    lst1.begin() // 返回指向第一个元素的迭代器
    lst1.end() // 返回末尾的迭代器
    lst1.insert() // 插入一个元素到list中
    lst1.erase() // 删除一个元素
    lst1.pop_back() // 删除最有一个元素
    lst1.pop_front() // 删除第一个元素
    lst1.clear() // 删除所有元素
    lst1.remove(const T & val) // 删除和val相等的元素
    lst1.push_back() // 在list的末尾添加一个元素
    lst1.push_front() // 在list的首部添加一个元素
    lst1.empty() // 判断,若list为空返回true
    lst1.max_size() // 返回list能容纳的最大元素数量
    lst1.sort() // 给list排序(顺序)
    list.reverse() // 把list中的元素倒转
    lst1.merge(lst2) // 合并lst2到lst1,并清空lst2
    lst1.unique() // 删除所有和前一个元素相等的元素

    // 在位置i前面插入链表x中的区间 [begin, end), 并在链表x中删除该区间(链表自身和链表x可以是用一个链表,只要i不在 [begin, end) 中即可
    void splice(iterator i, list & x, iterator begin, iterator end)

遍历List

    //迭代器法
   for(list<int>::const_iteratoriter = lst1.begin();iter != lst1.end();iter++)  
     {  
       cout<<*iter;  
     }  
     cout<<endl;  

案例:

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
class INT {
public:
        INT(int x):n(x){}
        friend bool operator < (const INT & int_l, const INT & int_2);
        friend bool operator == (const INT & int_1, const INT & int_2);
        friend ostream & operator << (ostream & out, const INT & int_x);
private:
        int n;
};

bool operator < (const INT & int_1, const INT & int_2) {
        return int_1.n < int_2.n;
}

bool operator == (const INT & int_1, const INT & int_2) {
        return int_1.n == int_2.n;
}

ostream & operator << (ostream & out, const INT & int_x) {
        out << int_x.n;
        return out;
}

template <class T>
void Plist(T begin, T end) {
        for(; begin != end; ++begin)
                cout << *begin << " ";
        cout << endl;
}
int main(void) {
        INT a[6] = {1, 4, 5, 2, 9, 2};
        int b[7] = {11, 32, 23, 35, 2, 4, 4};
        list<INT> lst1(a,  a+6), lst2(b, b+7);

        lst1.sort(); //顺序排序
        cout << "1. "; Plist(lst1.begin(), lst1.end());
        lst1.remove(2); //删除所有与2相等的元素
        cout << "2. "; Plist(lst1.begin(), lst1.end());

        lst2.pop_front(); //删除第一个元素
        cout << "3. "; Plist(lst2.begin(), lst2.end());
        lst2.unique(); //删除所有和前一个元素相等的元素
        cout << "4. "; Plist(lst2.begin(), lst2.end());

        lst2.sort();
        lst1.merge(lst2); //合并lst2到lst1并清空lst2
        cout << "5. "; Plist(lst1.begin(), lst1.end());
        cout << "6. "; Plist(lst2.begin(), lst2.end());

        lst1.reverse(); //将lst1倒置
        cout << "7. "; Plist(lst1.begin(), lst1.end());
        lst2.insert(lst2.begin(), a + 1, a + 4); //在lst2中插入3, 2, 4三个元素
        cout << "8. "; Plist(lst2.begin(), lst2.end());
        list<INT>::iterator p1, p2, p3; // 定义三个迭代器
        p1 = find(lst1.begin(), lst1.end(), 23); //查找元素
        p2 = find(lst2.begin(), lst2.end(), 4);
        p3 = find(lst2.begin(), lst2.end(), 5);

        lst1.splice(p1, lst2, p2, p3); //将p2, p3插入p1之前,并从lst2中删除p2, p3

        cout << "9. "; Plist(lst1.begin(), lst1.end());
        cout << "10. "; Plist(lst2.begin(), lst2.end());

        return 0;
}

c++ list使用方式

标签:tor   clear   std   return   const   include   区间   cout   素数   

原文地址:https://www.cnblogs.com/lyxf/p/12232861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!