第19章STL list

1.std::list是一個雙向鏈表

包含頭文件<list>

std::list <int> a;

a.push_front(2);

a.push_front(3);

a.push_back(4);

2.插入數據

list <int> a;

a.insert(a.begin(), 1);//在a的開頭插入一個1

a.insert(a.begin(), 3, 2);//在a的開頭插入3個2

list <int> b;

b.insert(b.begin(), a.begin(), a.end());//在b的開頭插入a鏈表從開頭到結尾的數據

3刪除數據

std::list <int> a;

a.push_front(3);

a.push_front(4);

list <int>::iterator i;

i = a.insert(a.begin(), 9);//在a的開頭插入數據9,並把插入數據9所在的位置傳遞給i

a.push_front(0);

a.push_back(8);

a.erase(a.begin(), i);//從a的開頭到i所在位置之前的數據刪除

a.erase(i);//刪除i所在位置的數據

4.反轉排序

std::list <int> a;

a.push_front(3);

a.push_front(2);

a.push_front(1);

a.reverse();//a內的順序反轉

 

sort()可以實現對list排序

std::list<int> a;

a.push_front(222);

a.push_front(3);

a.push_front(444);

a.push_front(-1);

a.sort();//按照數值的大小進行了排序結果爲-1 3 222 444

a.sort(sort_desc);//這裏定義了一個函數sort_desc,它有兩個參數,sort會傳遞給它,然後判斷兩個數的大小,返回一個bool類型,sort根據這個bool值來排序

bool sort_desc(const int &x, const int &y)

{

return (x < y);

}

5.例子

#include <list>

#include <string>

#include <iostream>

using namespace std;

enum menuoptionselection

{

insertcontactlistentry = 0,

sortnoname = 1,

sortonnumber = 2,

displayentries = 3,

eraseentry = 4,

quitcontactlist = 5

};

struct contactlistitem

{

string strcontactsname;

string strphonenumber;

contactlistitem(const string &strname, const string &strnumber)

{

strcontactsname = strname;

strphonenumber = strnumber;

}

bool operator == ( const contactlistitem & itemtocompare) const

{

return (itemtocompare.strcontactsname == this->strcontactsname);

}

bool operator < (const contactlistitem & itemtocompare) const

{

return (this->strcontactsname < itemtocompare.strcontactsname);

}

int showmenu();

contactlistitem getcontactinfo();

void displaycontactlist(const list <contactlistitem> & listcontacts);

void eraentryfromlist(list <contactlistitem> & listcontacts);

bool predicate_checkitemsonnumber(const contactlistitem& item1,const contactlistitems & item2);

int main()

{

list <constatlistitem> listcontacts;

int nuserselection = 0;

while((nuserselection == showmenu()) != (int) quitcontactlist)

{

switch(nuserselection)

{

case insertcontactlistenrty:

listcontacts.push_back(getcontactinfo());

cout << "contacts list updated" << endl;

break;

 

case sortnoname:

listconstacts.sort();

displaycontactlist(listcontacts);

break;

 

case sortn\onnumber:

listcontacts.sort(predicate_checkitemsonnumber);

displaycontactlist(listcontacts);

break;

 

case displayentries:

displaycontactlist(listcontacts);

break;

case eraseentry:

eraseentryfromlist(listcontacts);

displaycontactlist(listcontacts);

break;

 

case quitcontactlist:

cout << "ending application " << endl;

break;

 

default:

cout << "invalid input" << endl;

break;

 

return 0;

}

 

int showmenu()

{

cout << "enter  o to feed a name and phone number" << endl;

cout << "enter 1 to sort by name" << endl;

cout << "enter 2 to sort by number" << endl;

cout << "enter 3 to display all " << endl;

cout << "enter 4 to erase " << endl;

cout << "enter 5 to quit" << endl;

int noptionselectied = 0;

cin >> noptionselected;

return noptionselected;

}

bool predicate_checkitemsonnumber(const contactlistitem & items1, const contactlistitem& item2)

{

return (item1.strphonenumber < item2.strphonenumber);

}

contactlistitem getcontactinfo()

{

string strname;

cin >> strname;

string strphonenumber;

cin >> strphonenumber;

return contactlistitem(strname, strphonenumber);

void displaycontactlist (const list<contactlistitem> & listcontacts)

{

list <contactlistitem>::const_iterator icontact;

for(icontact = listcontacts.begin(); icontact != listcontacts.end(); ++ icontact)

{

cout << "name: " << icontact->strcontactsname;

cout << "number: " << icontact->strphonenumber << endl;

}

void eraseentryfromlist(list<contactlistitem> & listcontacts)

{

string strnametoerase;

cin >> strnametoerase;

listcontacts.remove(strnametoerase, ""));

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章