天天看點

【C++學習紀錄】list容器——疊代器 & 反轉 & 排序

一、list容器的疊代器

1、list容器的疊代器是不支援随機通路的。它是雙向疊代器。

2、這意味着:假設p1、p2是list疊代器,p1++、++p1、p1–、--p1、p1 == p2這樣的操作是合法的,而p1 += num不合法。

3、若p是一個list容器,通過p[num]的方式通路資料是不合法的。

二、reverse( ) 反轉函數

#include<iostream>
#include<list>
using namespace std;

void printList(const list<int> &temp)
{
    for (list<int>::const_iterator it = temp.begin(); it != temp.end(); ++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
}

int main()
{
    list<int> temp;
    for (int i = 1; i <= 5; i++)
    {
        temp.push_back(i);
    }
    printList(temp);
    cout << endl;
    temp.reverse();
    printList(temp);
    system("pause");
}
           

運作結果:

1 2 3 4 5

5 4 3 2 1
請按任意鍵繼續. . .
           

三、sort()排序函數

因為list容器的疊代器不支援随機通路,是以無法調用< algorithm >類中的全局函數sort()。這裡調用的sort函數是list類的成員函數。

1、普通int類型的排序

#include<iostream>
#include<list>
using namespace std;

void printList(const list<int> &temp)
{
    for (list<int>::const_iterator it = temp.begin(); it != temp.end(); ++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
}

int main()
{
    list<int> temp;
    //插入100、27、78、25、391五個數字
    temp.push_back(100);
    temp.push_back(27);
    temp.push_back(78);
    temp.push_back(25);
    temp.push_back(391);
    printList(temp);
    cout << endl;
    //預設對容器進行升序排序
    temp.sort();
    printList(temp);
    system("pause");
}
           

運作結果:

100 27 78 25 391

25 27 78 100 391
請按任意鍵繼續. . .
           

可如果我想進行降序排序呢?

隻需要添加一個函數作為sort()的參數~

#include<iostream>
#include<list>
using namespace std;

void printList(const list<int> &temp)
{
    for (list<int>::const_iterator it = temp.begin(); it != temp.end(); ++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
}

//注意,這個函數傳回值是布爾類型的
bool func(int num1, int num2)
{
    return num1 > num2;
}

int main()
{
    list<int> temp;
    //插入100、27、78、25、391五個數字
    temp.push_back(100);
    temp.push_back(27);
    temp.push_back(78);
    temp.push_back(25);
    temp.push_back(391);
    printList(temp);
    cout << endl;
    //預設對容器進行升序排序
    temp.sort(func);
    printList(temp);
    system("pause");
}
           

運作結果:

100 27 78 25 391

391 100 78 27 25
請按任意鍵繼續. . .
           

2、排序算法對類進行排序

這裡要求對容器中成員按年齡作降序排列。若年齡相同,則按身高降序排列。

#include<iostream>
#include<list>
#include<string>
using namespace std;

class person
{
    public:
    person(string name, int years, int height)
    {
        this->pname = name;
        this->pyears = years;
        this->pheight = height;
    }
    string pname;
    int pyears;
    int pheight;
};

void printList(const list<person> &temp)
{
    for (list<person>::const_iterator it = temp.begin(); it != temp.end(); ++it)
    {
        cout << (*it).pname << ' ' << (*it).pyears << ' ' << (*it).pheight << endl;
    }
}

//注意,這個函數傳回值是布爾類型的
bool func(person &p1, person &p2)
{
    if(p1.pyears == p2.pyears)
    {
        return p1.pheight > p2.pheight;
    }
    else
    {
        return p1.pyears > p2.pyears;
    }
    
}

int main()
{
    person p1("Ann", 20, 170);
    person p2("Joe", 22, 173);
    person p3("Frank", 24, 180);
    person p4("Lynn", 18, 167);
    person p5("Eric", 22, 182);
    person p6("Sue", 22, 187);
    list<person> temp;
    //插入六個person類
    temp.push_back(p1);
    temp.push_back(p2);
    temp.push_back(p3);
    temp.push_back(p4);
    temp.push_back(p5);
    temp.push_back(p6);
    printList(temp);
    cout << endl;
    //預設對容器進行升序排序
    temp.sort(func);
    printList(temp);
    system("pause");
}
           

運作結果:

Ann 20 170
Joe 22 173
Frank 24 180
Lynn 18 167
Eric 22 182
Sue 22 187

Frank 24 180
Sue 22 187
Eric 22 182
Joe 22 173
Ann 20 170
Lynn 18 167
請按任意鍵繼續. . .