![]() |
In C++, double-ended queues are sequence containers similar to vectors but are more efficient in the case of insertion and deletion of elements at both ends. In this article, we will learn how to find the sum of all elements in a deque in C++. Example Input: myDeque ={1,2,3,4,5} Output: Sum of all deque elements is 15. Sum Up All Elements of a Deque in C++To find the sum of all the elements in a std::deque in C++, we can use the std::accumulate() method which finds the sum of the elements in the given range. Syntaxaccumulate(first, last, 0);
where,
C++ Program to Find the Sum of All Elements in a DequeThe following program illustrates how we can find the sum of all elements in a deque in C++:
Output Deque Elements: 1 2 3 4 5 Sum of elements in the Deque: 15 Time Complexity: O(N) here N denotes the size of the deque. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |