您好,登錄后才能下訂單哦!
/*首先我是c語(yǔ)言的愛(ài)好者,之前沒(méi)接觸過(guò)c++,現(xiàn)在開(kāi)始學(xué)習(xí)c++,因此總會(huì)和c語(yǔ)言進(jìn)行對(duì)比,所謂的容器,其實(shí)就是鏈表的!在c語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中有所提及,c++把其進(jìn)行封裝成了庫(kù),不需要像以前學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)那樣具體去實(shí)現(xiàn),只要學(xué)院、會(huì)調(diào)用就可以了。
*/
#include "stdafx.h"
#include <iostream>
#include <list>
#include <windows.h>
using namespace std;
typedef list<int> INTLIST; //定義一個(gè)數(shù)列INTLIST,下一次遇見(jiàn)他的時(shí)候就會(huì)可以等價(jià)一個(gè)數(shù)組或者是一個(gè)容器或者是一個(gè)迭代器。
//從前向后顯示list隊(duì)列的全部元素
void put_list(INTLIST list, char *name)
{
INTLIST::iterator plist;
cout << "The contents of " << name << " : ";
for (plist = list.begin(); plist != list.end(); plist++)
cout << *plist << " ";
cout << endl;
}
//測(cè)試list容器的功能
void main(void)
{
//list1對(duì)象初始為空
INTLIST list1;
INTLIST list2(5, 1);
INTLIST list3(list2.begin(), --list2.end());
//聲明一個(gè)名為i的雙向迭代器
INTLIST::iterator i;
put_list(list1, "list1"); //print the lists
put_list(list2, "list2");
put_list(list3, "list3");
list1.push_back(7); //mask the lseven num put out
list1.push_back(8);
cout << "list1.push_back(7) and list1.push_back(8):" << endl;
put_list(list1, "list1");
list1.push_front(6); //把第一個(gè)元素6壓入第一個(gè)元素內(nèi)
list1.push_front(5);
cout << "list1.push_front(6) and list1.push_front(5):" << endl;
put_list(list1, "list1");
list1.insert(list1.begin(), 3, 9); //list1的首位直添加3個(gè)9.
cout << "list1.insert(list1.begin()+1,3,9):" << endl;
put_list(list1, "list1");
//測(cè)試引用類(lèi)函數(shù)
cout << "list1.front()=" << list1.front() << endl;
cout << "list1.back()=" << list1.back() << endl;
list1.pop_front();
list1.pop_back();
cout << "list1.pop_front() and list1.pop_back():" << endl;
put_list(list1, "list1");
list1.erase(++list1.begin());//擦出第一個(gè)元素
cout << "list1.erase(++list1.begin()):" << endl;
put_list(list1, "list1");
list2.assign(8, 1);
cout << "list2.assign(8,1):" << endl;
put_list(list2, "list2");
cout << "list1.max_size(): " << list1.max_size() << endl;
cout << "list1.size(): " << list1.size() << endl;
cout << "list1.empty(): " << list1.empty() << endl;
put_list(list1, "list1");
put_list(list3, "list3");
cout << "list1>list3: " << (list1 > list3) << endl;
cout << "list1<list3: " << (list1 < list3) << endl;
list1.sort();
put_list(list1, "list1");
list1.splice(++list1.begin(), list3);
put_list(list1, "list1");
put_list(list3, "list3");
Sleep(10000);
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。