欣乐

The eagles are coming!

导航

第7章 deque双端队列容器

 

 

 

 

 

/*

  第7章 deque双端队列容器
   7.1 deque技术原理
   7.2 deque应用基础
   7.3 本章小结

*/


//  第7章 deque双端队列容器

//   7.1 deque技术原理 ---------------------------------------------------------------------------------------

//   7.2 deque应用基础 ---------------------------------------------------------------------------------------


// 110, []
#include <deque>
#include <iostream>    
int main(void)
{
  using namespace std;
  deque < int > d;
  d.push_back(13);
  d.push_back(32);
  d.push_back(29);
  for(size_t i = 0; i < d.size(); i++)
    cout << "d[" << i << "] = " << d[i] << endl;
  return 0;
}



// 110, it

#include <deque>
#include <string>
#include <iostream>
int main(void)
{
  using namespace std;
  deque < string > d;
  d.push_back("北京");
  d.push_back("2008");
  d.push_back("奥运");
  deque < string > ::iterator i, iend;
  iend = d.end();
  int j;
  for(i = d.begin(), j = 0; i != iend; i++, j++)
    cout <<  *i;
  cout << endl;
  return 0;
}


// 111 , push_front

#include <deque>
#include <iostream>
int main(void)
{
  using namespace std;
  deque < int > d;
  d.push_back(6);
  d.push_back(7);
  //头部插入
  d.push_front(5);
  for(size_t i = 0; i < d.size(); i++)
  //打印5 6 7
    cout << d[i] << ' ';
  cout << endl;
  //中间位置插入
  d.insert(d.begin() + 1, 9); //在第2个元素前插入9,即5 9 6 7
  for(size_t j = 0; j < d.size(); j++)
    cout << d[j] << ' ';
  cout << endl;
  return 0;
}


// 112, erase, pop
#include <deque>
#include <iostream>
int main(void)
{
  using namespace std;
  deque < int > d;
  d.push_back(4);
  d.push_back(5);
  d.push_back(3);
  d.push_back(3);
  d.push_back(3);
  d.push_back(6);
  for(size_t i = 0; i < d.size(); i++)
    cout << d[i] << ' ';
  cout << endl;
  //头尾和任意位置删除元素
  d.erase(d.begin() + 1);
  d.pop_front();
  d.pop_back();
  for(size_t j = 0; j < d.size(); j++)
    cout << d[j] << ' ';
  cout << endl;
  //删除所有元素
  d.clear();
  cout << "执行clear()" << endl << "deque元素全部清除" << endl;
  return 0;
}



// 113 reverse_iterator
#include <deque>
#include <iostream>
int main(void)
{
  using namespace std;
  deque < int > d;
  d.push_back(1);
  d.push_back(3);
  d.push_back(5);
  d.push_back(7);
  d.push_back(9);
  //deque元素的前向遍历
  deque < int > ::iterator i, iend;
  iend = d.end();
  for(i = d.begin(); i != iend; i++)
    cout <<  *i << ' ';
  cout << endl;
  //deque元素的反向遍历
  deque < int > ::reverse_iterator ri, riend;
  riend = d.rend();
  for(ri = d.rbegin(); ri != riend; ri++)
    cout <<  *ri << ' ';
  cout << endl;
  return 0;
}


// 114 , swap
#include <deque>
#include <iostream>
using namespace std;
void print(deque < int >  &d);
int main(void)
{
  //d1
  deque < int > d1;
  d1.push_back(11);
  d1.push_back(12);
  d1.push_back(13);
  cout << "d1 = ";
  print(d1);
  //d2
  deque < int > d2;
  d2.push_back(90);
  d2.push_back(91);
  // d2.push_back(92);
  cout << "d2 = ";
  print(d2);
  //d1和d2交换
  d1.swap(d2); // 元素个数不同,一样可以交换
  cout << "d1与d2交换后" << endl;
  cout << "d1 = ";
  print(d1);
  cout << "d2 = ";
  print(d2);
  return 0;
}

//deque元素打印
void print(deque < int >  &d)
{
  for(size_t i = 0; i < d.size(); i++)
    cout << d[i] << ' ';
  cout << endl;
}



// 115
#include <deque>
#include <string>
#include <iostream>
int main(void)
{
  using namespace std;
  deque < string > d;
  //打印deque为空
  cout << "d是否为空: " << d.empty() << endl;
  //装入元素
  d.push_back("红楼梦");
  d.push_back("源氏物语");
  d.push_back("教父");
  d.push_back("水浒传");
  d.push_back("24史");
  //打印deque所有元素
  deque < string > ::iterator i, iend;
  iend = d.end();
  for(i = d.begin(); i != iend; i++)
    cout <<  *i << "   ";
  cout << endl;
  //打印首元素
  cout << "deque首元素为: " << d.front() << endl;
  //打印末元素
  cout << "deque末元素为: " << d.back() << endl;
  //打印元素个数
  cout << "deque元素个数为: " << d.size() << endl;
  //打印可支持的最大deque元素个数
  cout << "deque最大元素个数为: " << d.max_size() << endl;
  return 0;
}


//   7.3 本章小结 ---------------------------------------------------------------------------------------

 

 

 

 

 

 

TOP

 

posted on 2014-11-20 14:02  欣乐  阅读(139)  评论(0编辑  收藏  举报