How to Replace All Occurrences of an Element in a List in C++? Last Updated : 09 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a std::list that represents a doubly linked list is a sequence containers that allow non-contiguous memory allocation. In this article, we will learn how to replace all the occurrences of a specific element in a list using C++ STL. Example: Input: myList = {10, 20, 30, 10, 30, 30, 50}; oldElement = 30; newElement = 100;Output: myList = {10, 20, 100, 10, 100, 100, 50};Replacing All Occurrences of a Specific Element in a List in C++To replace all occurrences of a specific element in a std::list, we can use the std::replace() function from the <algorithm> library that replaces all occurrences of a specific value in a range with another value. We just have to pass the beginning and ending iterators of the list to the replace() function along with the element we want to replace and the new element. Syntax of std::replace()replace(begin, end, oldValue, newValue)C++ Program to Replace All Occurrences of an Element in a ListThe below example demonstrates the use of the std::replace() function to replace all occurrences of a specific element in a std::list in C++ STL. C++ // C++ program to illustrate how to replace all the // occurences of a specific element #include <algorithm> #include <iostream> #include <list> using namespace std; int main() { // intitialize a list list<int> myList = { 10, 20, 30, 10, 30, 30, 50 }; // Declare the value you want to replace int oldVal = 30; int newVal = 100; // Print the original list cout << "Original List: "; for (int element : myList) { cout << element << " "; } cout << endl; // Replace all occurrences of oldValue with newValue replace(myList.begin(), myList.end(), oldVal, newVal); // Print the updated list cout << "Updated List: "; for (int element : myList) { cout << element << " "; } cout << endl; return 0; } OutputOriginal List: 10 20 30 10 30 30 50 Updated List: 10 20 100 10 100 100 50 Time Complexity: O(n), where n is the number of elements in the list.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Replace All Occurrences of an Element in a List in C++? P pantharshx9d9 Follow Improve Article Tags : C++ Programs C++ STL cpp-list CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Replace All Occurrences of an Element in a Multiset in C++? In C++, multisets are associative containers similar to sets, but unlike sets, they allow the users to store duplicate elements. In this article, we learn how to replace all the occurrences of a specific element in a multiset in C++. Example Input:myMultiset = { 1,2,2,2,3,4,5 } Output:myMultiset = { 2 min read How to Replace All Occurences of an Element in a Set in C++? In C++, a set is an associative container that stores unique elements in a sorted order. In this article, we will learn how to replace all occurrences of a specific element in a set in C++. Example: Input: mySet = {1,2,3,2,4,5,2}; target = 2 replacement = 6 Output: After Replacement: 1 3 4 5 6 Repla 2 min read How to Replace All Occurrences of an Element in a Deque in C++? In C++, we have a container called deque (short for double-ended queue) that allows the insertion and removal of elements from both ends. In this article, we will see how to replace all occurrences of a specific element in a deque in C++. Example: Input: myDeque = {10,20,30,40,20,60} target = 30 new 2 min read How to Find All Occurrences of an Element in a List in C++? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to find all occurrences of a specific element in a list in C++. Example: Input: myList = {7, 5, 16, 2 min read How to Find All Occurrences of an Element in a Multiset in C++? In C++, a multiset is a container similar to a set but it allows multiple occurrences of its elements i.e. duplicate values. In this article, we will learn how to find all occurrences of a specific element in a multiset in C++. Example: Input: myMultiset = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};target = 3Ou 2 min read How to Remove All Occurrences of an Element from List in C++? In C++, Lists are sequence containers that allow non-contiguous memory allocation. In this article, we will learn how to remove an element from a list in C++. Example Input: myList = {100, 78, 120, 12, 56, 78, 78}target = 78Output:// Removed element 78 from the list{ 100, 120, 12, 56}Remove an Eleme 2 min read How to Find All Occurrences of an Element in a Set in C++? Finding the all occurrences of a specific element in a set using the C++ STL is a very efficient process that is done with the help of std::set::distance() member function. In this article, we'll explore how to find the first element in a set using the C++ STL. For Example,Input:mySet = {1, 2, 4, 3, 2 min read How to Remove All Occurrences of an Element from Multiset in C++? In C++, a multiset is a container that stores a sorted collection of elements in sorted order, and we can also insert duplicate elements. In this article, we will learn how to remove all the occurrences of a specific element in C++. Example Input: myMultiset = {10, 10, 10, 20, 30, 40}; Target= 10 Ou 2 min read How to Replace an Element in a List in C++? In C++, a std::list represents a doubly linked list, a sequence container that stores data in non-contiguous memory. In this article, we will learn how to replace a specific element in a list using C++. Example: Input: myList = {10, 20, 30, 60, 40, 12, 50}; oldElement = 60; newElement = 100;Output: 2 min read How to Find the Last Occurrence of an Element in a Set in C++? In C++, a set is a container that stores unique elements in a sorted order and elements are accessed and traversed using iterators. In this article, we will learn how to find the last occurrence of a specific element in a set in C++. Example Input:set<int> s = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Key 2 min read Like