zoukankan      html  css  js  c++  java
  • c++ list使用方式

    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;
    }
    
    
  • 相关阅读:
    S1-概论
    AngularJS--day01介绍使用基本语法
    原生ajax--2
    原生ajax---1
    操作元素--字符串对象-日期对象-Array对象(数组)-Math对象-计时器
    HTTP协议系列教材 (三)- 通过Firefox火狐调试工具观察 HTTP 请求协议
    HTTP协议系列教材 (二)- 借助FireFox火狐调试工具学习HTTP协议
    HTTP协议系列教材 (一)- 教程
    Servlet系列教材 (二十七)- JSON
    Servlet系列教材 (二十六)- JSON
  • 原文地址:https://www.cnblogs.com/lyxf/p/12232861.html
Copyright © 2011-2022 走看看