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");
}
//////////////////////////////////////////////////////////////////////////
// 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");
}
相关文章推荐
- STL容器使用DEMO-vector
- STL容器使用DEMO-queue
- STL容器使用DEMO-bitset
- 关于在STL容器list中使用find_if搜索特定的节点
- 使用STL中的list容器实现单链表的操作
- STL基础6:list容器的使用总结
- STL容器使用DEMO-deque
- STL中的容器使用比较(string vector list deque set map)
- STL容器使用DEMO-map
- STL 中 容器 list 的使用
- 关于在STL容器list中使用find_if 函数
- 浅谈STL list<T>链表容器和迭代器的使用C++实现
- C++ STL容器的学习使用(vector、queue、list、set、map)
- STL容器使用DEMO-multimap
- STL--> list 双向循环链表容器 接口使用及介绍。 模拟实现 STL list容器
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- STL容器使用DEMO-set
- 使用STL中的list容器实现单链表的操作
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- STL-list容器使用