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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM