C++/Deque/deque insert

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Insert elements in vector to deque

   
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
int main()
{
   int data[5] = { 6, 8, 7, 6, 5 };
   vector<int> v(5, 6);            //5 element vector
   deque<int> d(data, data + 5);
   deque<int>::iterator p;
   cout << "\nDeque values" << endl;
   for (p = d.begin(); p != d.end(); ++p)
      cout << *p << "\t";      
   cout << endl;
   d.insert(d.begin(), v.begin(), v.end());
   for (p = d.begin(); p != d.end(); p++)
   cout << *p << "\t";   
}


Insert the remaining elements from the vector between the first and last deque elements

  
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
const int SIZE = 100;
double sum(const deque<double> &dq)
{
   deque<double>::const_iterator p;
   double s = 0;
   for (p=dq.begin(); p != dq.end(); ++p)
      s += *p ;
   return s;
}
int main()
{
  vector<double> vec(SIZE, 0);   
  deque<double> deq;             
  int i;
  double sumTotal;
                                 
  for(i = 0; i < SIZE; i++){
     vec[i] = i * 0.6; cout << vec[i] << endl;
  }
    deq.push_front(vec.front()); 
    deq.push_back(vec.back());  
    deq.insert(deq.begin()+1, vec.begin()+1, vec.end()-1);
    sumTotal = sum(deq);
    cout << "sum:" << sumTotal;
}