加文

希望是美好的……
随笔 - 0, 文章 - 209, 评论 - 0, 引用 - 0
数据加载中……

queue insert

#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];
    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                : ";
    for_each(d.begin(),d.end(),print);
    cout << endl;
    It = d.begin();
    // insert value "5" at the position "It"
    cout << "d.insert(It,5)          : ";
    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 : ";
    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)       : ";
    d.insert(It,2,20);
    for_each(d.begin(),d.end(),print);
    cout << endl;
    getchar();
    return 0;
}

posted on 2012-04-11 23:42 加文 阅读(393) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理