C++ deque(double-ended-queue)双端队列

ostream_iterator输出流迭代器模板的使用,选自MSDN

#include <iterator>		//迭代器头文件
#include <vector>		//向量容器头文件
#include <iostream>
using namespace std;

int main(void)
{
   // ostream_iterator for stream cout
   ostream_iterator<int> intOut ( cout , "\n" );
   *intOut = 10;
   intOut++;
   *intOut = 20;
   intOut++;
   *intOut = 30;
   cout<<endl;

   int i;
   vector<int> vec;
   for ( i = 1 ; i < 7 ; ++i )
   {
      vec.push_back (  i );
   }

   // Write elements to standard output stream
   cout << "Elements output without delimiter: ";
   copy ( vec.begin ( ), vec.end ( ), ostream_iterator<int> ( cout ) );
   cout << endl <<endl;

   // Write elements with delimiter " : " to output stream
   cout << "Elements output with delimiter: ";
   copy ( vec.begin ( ), vec.end ( ), ostream_iterator<int> ( cout, " : " ) );
   cout << endl <<endl;

   system("pause");
}

deque容器的使用关于deque容器类详见MSDN

#include <iostream>  
#include <deque>		//使用双端队列是使用的头文件  
#include <string>  
#include <algorithm>	//STL中重要的头文件,提供了大量基于迭代器的非成员模版函数
 
using namespace std; 
 
int main(void)
{ 
	deque<string> strDeq; 

    strDeq.assign(4,string("liaoning"));	//将4个字符串对象插入到队列中
    strDeq.push_back("first_string"); 
    strDeq.push_front("last_string"); 
 
    copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout,"--")); 
    cout << endl<< endl;
 
    for(int i = 0; i<strDeq.size(); ++i)	//输出方式1
	{
        cout << "strDeq[" << i << "] : " << strDeq[i] << endl;
	}
    cout << endl; 
     
    for(i = 0;i < strDeq.size();++i)		//输出方式2
	{
        cout << "strDeq.at(" << i << ") : " << strDeq.at(i) << endl; 
	}
    cout << endl; 
 
    strDeq.pop_front(); 
    strDeq.pop_back();
	
	//显示首尾出队后的结果
    copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," ")); 
    cout << endl; 
    cout << endl; 
 
    for(i = 0;i < strDeq.size();++i)	//给队中的每个元素加上前缀
	{
        strDeq[i] = "pre" + strDeq[i]; 
	}
    copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," ")); 
    cout << endl; 
    cout << endl; 
 
    strDeq.resize(6,"cjc");				//调整队列的大小,如果6大于原大小,则补齐
										//小于,则保留原大小
    copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," ")); 
    cout << endl; 
    cout << endl; 

	system("pause");
}

参考以及详细原理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值