- //
- // CopyRight(c) 2009, YOYO, All Rights Reserved.
- // Author: LIN YiQian
- // Created: 2009/08/24
- // Describe: STL deque 使用DEMO
- //
- #include <iostream>
- #include <deque>
- using namespace std;
- typedef deque<int> INT_DEQ;
- // 打印Deque
- void PrintDeque(INT_DEQ deqInt)
- {
- for (INT_DEQ::iterator deqIter = deqInt.begin(); deqIter != deqInt.end(); ++deqIter)
- {
- cout << *deqIter;
- }
- cout << endl;
- }
- // 逆向打印Deque
- void PrintDequeReverse(INT_DEQ deqInt)
- {
- for (INT_DEQ::reverse_iterator deqRIter = deqInt.rbegin(); deqRIter != deqInt.rend(); ++deqRIter)
- {
- cout << *deqRIter;
- }
- cout << endl;
- }
- void main(void)
- {
- INT_DEQ deqInt1;
- cout << "deqInt1: "; PrintDeque(deqInt1);
- // push_front() & push_back()
- {
- deqInt1.push_front(3);
- deqInt1.push_front(4);
- deqInt1.push_back(6);
- deqInt1.push_back(9);
- cout << "deqInt1 after Push: "; PrintDeque(deqInt1);
- }
- // reverse()
- {
- cout << "deqInt1 Reverse: "; PrintDequeReverse(deqInt1);
- }
- // size()
- {
- cout << "deqInt1 size: " << deqInt1.size() << endl;
- }
- // max_size()
- {
- cout << "deqInt1 max_size: " << deqInt1.max_size() << endl;
- }
- INT_DEQ deqInt2(5, 5);
- cout << "deqInt2(5, 5): "; PrintDeque(deqInt2);
- // erase()
- {
- deqInt2.erase(deqInt2.begin()+2);
- cout << "deqInt2 after Erase: "; PrintDeque(deqInt2);
- deqInt2.erase(deqInt2.begin()+1, deqInt2.end()-1);
- cout << "deqInt2 after Erase: "; PrintDeque(deqInt2);
- }
- // empty()
- {
- cout << "deqInt2 empty?: " << boolalpha << deqInt2.empty();
- }
- INT_DEQ deqInt3(deqInt1);
- cout << "deqInt3(deqInt1): "; PrintDeque(deqInt3);
- // pop_front() & pop_back()
- {
- deqInt3.pop_front();
- deqInt3.pop_back();
- cout << "deqInt3 after Pop: "; PrintDeque(deqInt3);
- }
- // assign()
- {
- deqInt3.assign(6, 6);
- cout << "deqInt3 after Assign: "; PrintDeque(deqInt3);
- }
- INT_DEQ deqInt4(deqInt3.begin()+2, deqInt3.end());
- cout << "deqInt4(deqInt3.begin()+2, deqInt3.end()): "; PrintDeque(deqInt4);
- // at() and operator[]
- {
- cout << "deqInt4 at(3): " << deqInt4.at(3) << endl;
- cout << "deqInt4[2]: " << deqInt4[2] << endl;
- }
- // front() and back()
- {
- cout << "deqInt4 front: " << deqInt4.front() << endl;
- cout << "deqInt4 back: " << deqInt4.back() << endl;
- }
- // operator cmp
- {
- cout << "deqInt4 == deqInt2?: " << boolalpha << (deqInt4 == deqInt2) << endl;
- cout << "deqInt4 >= deqInt2?: " << boolalpha << (deqInt4 >= deqInt2) << endl;
- }
- // swap()
- {
- deqInt4.swap(deqInt1);
- cout << "deqInt1 after Swap: "; PrintDeque(deqInt1);
- cout << "deqInt4 after Swap: "; PrintDeque(deqInt4);
- }
- // clear()
- {
- deqInt4.clear();
- cout << "deqInt4 after Clear: "; PrintDeque(deqInt4);
- cout << "deqInt4 size: " << deqInt4.size() << endl;
- cout << "deqInt4 empty?: " << boolalpha << deqInt4.empty() << endl;
- }
- system("pause");
- }
最后
以上就是老实睫毛为你收集整理的STL容器使用DEMO-deque的全部内容,希望文章能够帮你解决STL容器使用DEMO-deque所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复