第四讲 deque

deque  提供了对首部数据进行删除/插入操作

 1 //对一个int型的deque进行首尾添加操作
 2 #include "stdafx.h"
 3 #include <iostream>
 4 
 5 #include <deque>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     deque<int> oInt;
11     //0,1,2,3,4
12     for(int i = 0; i < 5; ++i){
13         oInt.push_back(i);            //尾部添加
14     }
15     //4,3,2,1,0,0,1,2,3,4
16     for(int i = 0; i < 5; ++i){
17         oInt.push_front(i);            //首部添加
18     }
19 
20     for(int i = 0; i < oInt.size(); ++i){
21         cout << oInt[i] << endl;
22     }
23     return 0;
24 }

 

对已string型deque进行添加,删除,查找,插入操作
 1 //对已string型deque进行添加,删除,查找,插入操作
 2 #include "stdafx.h"
 3 #include <iostream>
 4 #include <string>
 5 #include <deque>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     deque<string> oString;
11     //插入数据2 3 1 4 
12     oString.push_front("2.jiesoon.com");            //首部添加 2位
13     oString.push_back("3.jiesoon.com");                //尾部添加 3
14     oString.push_front("1.jiesoon.com");            //首部添加 1
15     oString.push_back("4.jiesoon.com");                //尾部添加 4
16     // 输出string是特有的size_type
17     //vector<string>::size_type 是在vector类中typedef的类型
18     for(deque<string>::size_type i = 0; i < oString.size(); ++i){
19         cout << oString[i] << endl;
20     }
21     cout << "**************************************************" <<endl;
22     //删除数据1 4
23     oString.pop_front();
24     oString.pop_back();
25     for(deque<string>::size_type i = 0; i < oString.size(); ++i){
26         cout << oString[i] << endl;
27     }
28     
29     cout << "**************************************************" <<endl;
30     for(deque<string>::iterator itString = oString.begin(); itString != oString.end(); ++itString){
31         cout << *itString << endl;
32     }
33     
34     cout << "**************************************************" <<endl;
35     //查找数据
36     deque<string>::iterator itString =find(oString.begin(),oString.end(),"2.jiesoon.com");//find()
37     if(itString != oString.end()){
38         cout << *itString <<endl;
39     }
40     else{
41         cout << "can't find 2.jiesoon.com" <<endl;
42     }
43 
44     cout << "**************************************************" <<endl;
45     //插入数据
46     oString.insert(itString,"1.jiesoon.com");        //insert()
47     for(deque<string>::size_type i = 0; i < oString.size(); ++i){
48         cout << oString[i] << endl;
49     }
50 
51     return 0;
52 }

 

 

 

posted @ 2014-06-25 18:12  击进的Cocos  阅读(265)  评论(0编辑  收藏  举报