码迷,mamicode.com
首页 > 其他好文 > 详细

第四讲 deque

时间:2014-06-26 17:46:27      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   color   com   

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;
}

 

 

 

第四讲 deque,布布扣,bubuko.com

第四讲 deque

标签:style   class   blog   code   color   com   

原文地址:http://www.cnblogs.com/zenseven/p/3808605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!