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 276 views
2 answers 226 views
2 answers 261 views
1 answer 198 views
1 answer 155 views
1 answer 196 views
2 answers 302 views
...