您的位置:首页 > 其它

STL容器使用DEMO-list

2009-08-24 16:28 483 查看
Code:

//////////////////////////////////////////////////////////////////////////

// CopyRight(c) 2009, YOYO, All Rights Reserved.

// Author: LIN YiQian

// Created: 2009/08/24

// Describe: STL list 使用DEMO

//////////////////////////////////////////////////////////////////////////

#include <iostream>

#include <list>



using namespace std;



typedef list<int> INT_LST;



// 打印List

void PrintList(INT_LST lstInt)

{

for (INT_LST::iterator lstIter = lstInt.begin(); lstIter != lstInt.end(); ++lstIter)

{

cout << *lstIter;

}

cout << endl;

}



// 逆向打印List

void PrintListReverse(INT_LST lstInt)

{

for (INT_LST::reverse_iterator lstRIter = lstInt.rbegin(); lstRIter != lstInt.rend(); ++lstRIter)

{

cout << *lstRIter;

}

cout << endl;

}



void main(void)

{

INT_LST lstInt1;

cout << "lstInt1: "; PrintList(lstInt1);



// push_front() & push_back()

{

lstInt1.push_front(3);

lstInt1.push_front(2);

lstInt1.push_back(8);

lstInt1.push_back(9);

lstInt1.push_back(7);

cout << "lstInt1 after Push: "; PrintList(lstInt1);

}



// insert()

{

lstInt1.insert(++ ++lstInt1.begin(), 5);

lstInt1.insert(++ lstInt1.begin(), 6);

cout << "lstInt1 after Insert: "; PrintList(lstInt1);

}



// sort()

{

lstInt1.sort();

cout << "lstInt1 after Sort: "; PrintList(lstInt1);

}



// size() & max_size()

{

cout << "lstInt1 size: " << lstInt1.size() << endl;

cout << "lstInt1 max_size: " << lstInt1.max_size() << endl;

}



// empty()

{

cout << "lstInt1 Empty?: " << boolalpha << lstInt1.empty() << endl;

}



// reverse()

{

cout << "lstInt1 reverse: "; PrintListReverse(lstInt1);

}



// front() & back()

{

cout << "lstInt1 front: " << lstInt1.front() << endl;

cout << "lstInt1 back: " << lstInt1.back() << endl;

}



INT_LST lstInt2(7, 9);

cout << "lstInt2: "; PrintList(lstInt2);



INT_LST lstInt3(lstInt1);

cout << "lstInt3: "; PrintList(lstInt3);



// pop_front() & pop_back()

{

lstInt3.pop_front();

lstInt3.pop_back();

cout << "lstInt3 after Pop: "; PrintList(lstInt3);

}



// erase()

{

lstInt3.erase(++lstInt3.begin());

cout << "lstInt3 after Erase: "; PrintList(lstInt3);



lstInt3.erase(++lstInt3.begin(), --lstInt3.end());

cout << "lstInt3 after Erase: "; PrintList(lstInt3);

}



INT_LST lstInt4(lstInt2.begin(), lstInt2.end());

cout << "lstInt4: "; PrintList(lstInt4);



// assign()

{

lstInt4.assign(9, 2);

cout << "lstInt4 after Assign: "; PrintList(lstInt4);

}



// operator cmp

{

cout << "lstInt4 == lstInt2?: " << boolalpha << (lstInt2 == lstInt4) << endl;

cout << "lstInt4 <= lstInt2?: " << boolalpha << (lstInt4 <= lstInt2) << endl;

}



// splice()

{

lstInt4.splice(++lstInt4.begin(), lstInt2, ++ ++lstInt2.begin(), --lstInt2.end());

cout << "lstInt4 after Splice: "; PrintList(lstInt4);

cout << "lstInt2 after Splice: "; PrintList(lstInt2);



lstInt4.splice(lstInt4.begin(), lstInt1, ++ lstInt1.begin());

cout << "lstInt4 after Splice: "; PrintList(lstInt4);

cout << "lstInt1 after Splice: "; PrintList(lstInt1);



lstInt4.splice(lstInt4.begin(), lstInt3);

cout << "lstInt4 after Splice: "; PrintList(lstInt4);

cout << "lstInt3 after Splice: "; PrintList(lstInt3);

}



// swap()

{

lstInt4.swap(lstInt1);

cout << "lstInt4 after Swap: "; PrintList(lstInt4);

cout << "lstInt1 after Swap: "; PrintList(lstInt1);

}



// clear()

{

lstInt4.clear();

cout << "lstInt4 after Clear: "; PrintList(lstInt4);

cout << "lstInt4 size: " << lstInt4.size() << endl;

cout << "lstInt4 Empty?: " << boolalpha << lstInt4.empty() << endl;

}



system("pause");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: