双端队列的定位

1:可用迭代器

2:地址访问

#include <iostream>
#include <string>
#include <deque> //头文件不能少

using namespace std;

deque<string> deq;//这里用一个string类型的deque来做演示,初始为空
deque<string>::iterator it; //提前准备一个迭代器,写法一如既往 

int main() {

    deq.push_back("last string");
    deq.push_front("front string");
    for (it = deq.begin(); it != deq.end(); it++) {
        cout << *it << endl;
    } //遍历及输出元素 
    cout << endl;

    it = deq.begin();
    it++;
    deq.insert(it, "middle"); 
    //将字符加到队列中第2个位置  
    for (it = deq.begin(); it != deq.end(); it++) {
        cout << *it << endl;
    } 
    cout << endl;
   
    deq.insert(deq.begin()+2, "test"); 
     //将字符加到队列中第3个位置  
    for (it = deq.begin(); it != deq.end(); it++) 
        cout << *it << endl;
    cout<<endl;
    
    deq.erase(deq.begin() + 1);
    //删除队列中第2个字符串 
     for (it = deq.begin(); it != deq.end(); it++) 
        cout << *it << endl;
    return 0;
}

  

posted @ 2023-04-23 20:25  我微笑不代表我快乐  阅读(14)  评论(0编辑  收藏  举报