首页 > 代码库 > 第四讲 deque

第四讲 deque

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

//对一个int型的deque进行首尾添加操作
#include "stdafx.h"#include <iostream>#include <deque>using namespace std;int main(){ deque<int> oInt; //0,1,2,3,4 for(int i = 0; i < 5; ++i){ oInt.push_back(i); //尾部添加 } //4,3,2,1,0,0,1,2,3,4 for(int i = 0; i < 5; ++i){ oInt.push_front(i); //首部添加 } for(int i = 0; i < oInt.size(); ++i){ cout << oInt[i] << endl; } return 0;}
对已string型deque进行添加,删除,查找,插入操作
//对已string型deque进行添加,删除,查找,插入操作#include "stdafx.h"#include <iostream>#include <string>#include <deque>using namespace std;int main(){    deque<string> oString;    //插入数据2 3 1 4     oString.push_front("2.jiesoon.com");            //首部添加 2位    oString.push_back("3.jiesoon.com");                //尾部添加 3    oString.push_front("1.jiesoon.com");            //首部添加 1    oString.push_back("4.jiesoon.com");                //尾部添加 4    // 输出string是特有的size_type    for(deque<string>::size_type i = 0; i < oString.size(); ++i){        cout << oString[i] << endl;    }    cout << "**************************************************" <<endl;    //删除数据1 4    oString.pop_front();    oString.pop_back();    for(deque<string>::size_type i = 0; i < oString.size(); ++i){        cout << oString[i] << endl;    }        cout << "**************************************************" <<endl;    for(deque<string>::iterator itString = oString.begin(); itString != oString.end(); ++itString){        cout << *itString << endl;    }        cout << "**************************************************" <<endl;    //查找数据    deque<string>::iterator itString =find(oString.begin(),oString.end(),"2.jiesoon.com");//find()    if(itString != oString.end()){        cout << *itString <<endl;    }    else{        cout << "can‘t find 2.jiesoon.com" <<endl;    }    cout << "**************************************************" <<endl;    //插入数据    oString.insert(itString,"1.jiesoon.com");        //insert()    for(deque<string>::size_type i = 0; i < oString.size(); ++i){        cout << oString[i] << endl;    }    return 0;}