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

来源:互联网 发布:淘宝童装店名大全 编辑:程序博客网 时间:2024/05/21 17:50

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
原创粉丝点击