How to Replace a Value in an Array in C++? Last Updated : 01 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to replace a value in an array in C++. Example: Input: arr: {10 20 30 30 20 10 10 20} Element_to_replace= 20 replacement=99 Output: Array after replacement: {10 99 30 30 99 10 10 99}Replace Element of an Array With Another in C++To replace an element of an array, we can use the std::replace() that assigns new_value to all the elements in the range [first, last) that are equal to old_value. This function uses the operator == to compare the individual elements to the old_value. Syntax of std::replacevoid replace(first, last, old_value, new_value) Here, first, last: Forward iterators to the initial and final positions in a sequence of elements.old_value: Value to be replaced.new_value: Replacement value.C++ Program to Replace a Value in an Array The below program demonstrates how we can replace a value in an array with another value. C++ // C++ Program to find and replace the value #include <algorithm> #include <iostream> using namespace std; int main() { int arr[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; int n = sizeof(arr) / sizeof(arr[0]); // variable containing the old and new values int old_val = 20, new_val = 99; // print old array cout << "Original Array:"; for (int i = 0; i < n; i++) cout << ' ' << arr[i]; cout << endl; // Function used to replace the values replace(arr, arr + n, old_val, new_val); // new array after using std::replace cout << "New Array:"; for (int i = 0; i < n; i++) cout << ' ' << arr[i]; cout << endl; return 0; } OutputOriginal Array: 10 20 30 30 20 10 10 20 New Array: 10 99 30 30 99 10 10 99 Time Complexity: O(N)Auxilliary Space: O(1) Comment More infoAdvertise with us Next Article How to Replace a Value in an Array in C++? A akshitsaxena2 Follow Improve Article Tags : C++ Programs C++ STL cpp-array CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Replace a Key-Value Pair in a Map in C++? A map in C++ is a part of the Standard Template Library (STL) that allows the user to store data in key-value pairs where each key in a map must be unique. In this article, we will learn how to replace a specific key-value pair in a Map in C++. Example: Input : map<int, string> mp={{1,"John"}, 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 Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Reset int Array to Zero in C++? In C++, arrays store data of the same type in a contiguous memory location. To reset an int array means setting all elements to zero. In this article, we will see how to reset an integer array elements to zero in C++. For Example, Input: int arr[] = { 10, 20, 30, 40, 50 };Output:Array after resettin 2 min read How to Sort an Array in C++? Sorting an array involves rearranging its elements in a specific order such as from smallest to largest element or from largest to smallest element, etc. In this article, we will learn how to sort an array in C++.Example:Input: arr ={5,4,1,2,3}Output: 1 2 3 4 5Explanation: arr is sorted in increasin 4 min read How to Count the Number of Occurrences of a Value in an Array in C++? In C++, an array is a data structure that stores the collection of the same type of data in a contiguous memory location. In this article, we will learn how to count the number of occurrences of a value in an array in C++. Example: Input: arr= {2, 4, 5 ,2 ,4 , 5, 2 , 3 ,8}Target = 2Output: Number of 2 min read How to Initialize a Set with an Array in C++? In C++, an array stores data in contiguous memory locations and can have duplicates as well whereas a set stores unique elements only in a sorted order. In this article, we will learn how to initialize a set with an array in C++. For Example, Input:arr[]={2, 1, 5, 4, 3, 5};Output:Element in Set are: 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 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 Like