How to Find the Sum of All Elements in a List in C++ STL? Last Updated : 21 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows to store data in non-contiguous memory locations. In this article, we will learn how to find the sum of all elements in a list in C++. Example: Input:myList = {10, 20, 30, 40, 50};Output: The sum of all elements in the list is: 150Sum of All Elements in a List in C++ To find the sum of all elements in a std::list in C++, we can use the std::accumulate() function provided by the <numeric> header that finds the sum of all the elements in the given range with the variable sum. Syntax to Find the Sum of All Elements in a Listaccumulate(first, last, sum);Here, first and last are iterators pointing to beginning and one position past the end of list respectively.sum is initial value of the sum (0 here)C++ Program to Find the Sum of All Elements in a List The following program demonstrates how we can use the std::accumulate function to find the sum of all elements in a list in C++. C++ // C++ program to find the sum of all elements in a list #include <iostream> #include <list> #include <numeric> using namespace std; int main() { // Initializing a list of integers list<int> myList = { 10, 20, 30, 40, 50 }; // Finding the sum of all elements int result = accumulate(myList.begin(), myList.end(), 0); // initial value of sum is 0 here // Printing the sum of all elements in the list cout << "The sum of all elements in the list is : " << result; return 0; } OutputThe sum of all elements in the list is : 150Time Complexity: O(N), where N is the size of the list.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Sum of All Elements in a List in C++ STL? P pantharshx9d9 Follow Improve Article Tags : C++ Programs C++ STL cpp-list CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find the Mode of All Elements in a List in C++? In C++, a list is a sequence container used to store data of similar type in non-contiguous memory locations. The mode of a list represents the element that occurs most frequently in the container. In this article, we will learn how to find the mode of a list in C++. Example: Input:myList = {1, 2, 3 2 min read How to Find the Sum of All Elements in a Deque in C++? 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 d 2 min read How to Find the Median of All Elements in a List in C++? In C++, a list is a container that allows us to store data in non-contiguous memory locations. The median of a list is defined as the middle element when the list size is odd, and the average of the two middle elements when the list size is even. In this article, we will learn how to find the median 3 min read How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read How to Find the Minimum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that stores data in non-contiguous memory locations efficiently. In this article, we will learn how to find the minimum element in a list in C++. Example: Input: myList = {30, 10, 20, 50, 40};Output: The minimum element in the list i 2 min read Like