How to Replace All Occurrences of an Element in a List in C++?
Last Updated :
09 Apr, 2024
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 List
The 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)
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