How to insert an element at the beginning of deque in C++

2 Answers

0 votes
#include <iostream> 
#include <deque> 
  
void printdq(std::deque <int> dq) { 
    for (auto &n: dq)
        std::cout << n << ", ";
    std::cout << '\n'; 
} 
  
int main() 
{ 
    std::deque<int> dq = { 5, 2, 9, 12, 7, 9, 13, 89 }; 
      
    printdq(dq);
      
    dq.emplace(dq.begin(), 100);
 
    printdq(dq);
    
    return 0; 
} 
    
    
    
/*
run:
    
5, 2, 9, 12, 7, 9, 13, 89, 
100, 5, 2, 9, 12, 7, 9, 13, 89, 
    
*/

 



answered Jul 30, 2020 by avibootz
edited Jul 30, 2020 by avibootz
0 votes
#include <iostream> 
#include <deque> 
  
void printdq(std::deque <int> dq) { 
    for (auto &n: dq)
        std::cout << n << ", ";
    std::cout << '\n'; 
} 
  
int main() 
{ 
    std::deque<int> dq = { 5, 2, 9, 12, 7, 9, 13, 89 }; 
      
    printdq(dq);
      
    dq.emplace_front(99);
 
    printdq(dq);
    
    return 0; 
} 
    
    
    
/*
run:
    
5, 2, 9, 12, 7, 9, 13, 89, 
99, 5, 2, 9, 12, 7, 9, 13, 89, 

*/

 



answered Jul 30, 2020 by avibootz

Related questions

1 answer 255 views
2 answers 220 views
2 answers 254 views
1 answer 192 views
1 answer 146 views
1 answer 185 views
2 answers 292 views
...