How to Replace an Element in a List in C++?
Last Updated :
15 Apr, 2024
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:
myList = {10, 20, 30, 100, 40, 12, 50};
Replace Specific Element in a List in C++ STL
In C++, we can easily replace an element in the std::list by using the std::replace()
function from the <algorithm>
library that replaces a given value with another new value. We need to pass the beginning and ending iterators of the list to the replace() function along with the element you want to replace and the new element.
Syntax of std::replace()
replace(begin, end, old_value, new_value);
C++ Program to Replace an Element in a List
The below example demonstrates the use of the std::replace() function to replace a specific element in a std::list in C++ STL.
C++
// C++ program to illustrate how to replace a specific
// element in a list
#include <algorithm>
#include <iostream>
#include <list>
using namespace std;
int main()
{
// Creating a list of integers
list<int> nums = { 10, 20, 30, 60, 40, 12, 50 };
// Elements to replace
int oldElement = 60;
int newElement = 100;
// Replacing the old element with the new element
replace(nums.begin(), nums.end(), oldElement,
newElement);
// Printing the list after replacement
cout << "List after replacement : ";
for (int num : nums) {
cout << num << " ";
}
cout << endl;
return 0;
}
OutputList after replacement : 10 20 30 100 40 12 50
Time Complexity: O(N), where N is the number of elements in the list.
Auxiliary Space: O(1)
Similar Reads
How to Replace an Element in a Deque in C++? In C++, deque (short for double-ended queue) allows fast insertions and deletions at both its beginning and its end. In this article, we will learn how to replace a specific element in a deque in C++. Example: Input: myDeque: {1, 2, 3, 4, 5, 3, 7} Output: // Replaced element 3 with 10 Updated Deque:
2 min read
How to Replace an Element in a Vector in C++? In this article, we will learn how to replace an element in a vector in C++.The most efficient method to replace the old value with a new value is by using replace() method. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1,
2 min read
How to Replace All Occurrences of an Element in a List in C++? 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}; oldEl
2 min read
How to Remove an Element from a List in C++? In C++, the STL provides a std::list container that represents a doubly linked list to store the sequential data in non-contiguous memory locations. In this article, we will learn how to remove an element from a list in C++. Example: Input: myList = {1, 2, 3, 4, 5, 6, 7, 8} Target = 5 Output: // rem
2 min read
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