• deque-insert


    iterator insert(iterator it,const T& x):双端队列中某一元素前增加一个元素x
    返回安插点的位置
    源码如下:

    ....
    *pos = x_cpoy;
    return  *pos; //pos即为新插入点的位置
    
    ////////////////////////////////////////
    //     	2018/04/24 7:38:54
    //      deque-insert
    // 在pos之前插入数据
    #include <iostream>
    #include <deque>
    #include <iterator>
    #include <algorithm>
    
    using namespace std;
    
    template<class T>
    class Print{
    public:
    	void operator()(T& t){
    		cout << t << " ";
    	}
    };
    //=========================
    
    int main(){
    	int ary[5] = { 1, 2, 3, 4, 5 };
    	//fill(ary, ary + 5, 1);
    
    	deque<int> d;
    	deque<int>::iterator it;
    	Print<int> print;
    	copy(ary,ary+5,back_inserter(d));
    	cout << "deque d:" << endl;
    	for_each(d.begin(),d.end(),print);
    	cout << endl;
    
    	it = d.begin();
    	// insert value "5" at the position "it"
    	cout << "d.insert(it,5)" << endl;
    	d.insert(it, 5);
    	for_each(d.begin(),d.end(),print);
    	cout << endl;
    
    	// insert range ary+2 - ary+5 at the position "it"
    	it = d.begin() + 5;
    	cout << "d.insert(it,ary+2,ary+5)" << endl;
    	// it 指向  5 1 2 3 4 5 中的最后一个5,并向其前加入元素
    	// ary+2 ~ ary+5, 也就是 3 4 5
    	d.insert(it, ary + 2, ary + 5);
    	for_each(d.begin(),d.end(),print);
    	cout << endl;
    
    	// insert 2 value of "20" at the position "it"
    	it = d.end() - 2;
    	cout << "d.insert(it,2,20)" << endl;
    	d.insert(it, 2, 20);
    	for_each(d.begin(),d.end(),print);
    	cout << endl;
    	return 0;
    }
    
    /*
    OUTPUT:
    	deque d:
    	1 2 3 4 5
    	d.insert(it,5)
    	5 1 2 3 4 5
    	d.insert(it,ary+2,ary+5)
    	5 1 2 3 4 3 4 5 5
    	d.insert(it,2,20)
    	5 1 2 3 4 3 4 20 20 5 5
    */ 
    
  • 相关阅读:
    Django框架 之 logging配置
    Django框架 之 中间件
    Django框架 之 form组件
    Django框架 之 Ajax
    Django框架 之 Pagination分页实现
    3张图带你看懂扩展KMP(EXKMP)
    [TJOI2018]游园会(状压dp+LCS)
    [BZOJ 2959] 长跑(LCT+并查集)
    [NAIPC2016]Jewel Thief(决策单调性+分治)
    [BZOJ 1563] [NOI 2009] 诗人小G(决策单调性)
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537998.html
Copyright © 2020-2023  润新知