How to Replace an Element in a Vector in C++? Last Updated : 26 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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, 3, 6, 2, 7, 2}; // Replace all 2s with 22 replace(v.begin(), v.end(), 2, 22); for (auto i : v) cout << i << " "; return 0; } Output1 3 6 22 7 22 Explanation: The replace() method replace all the occurrences of the given element in the vector with the new value.There are also some other method in C++ by which we can replace the given old values with new value. Some of them are as follows:Using transform()The transform() method allows us to apply a transformation (e.g., replacing elements) to all elements in the vector based on a transformation function. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 6, 2, 7, 2}; // Replacing all 2s in vector with 22 transform(v.begin(), v.end(), v.begin(), [](int i) { return i == 2 ? 22 : i; }); for (auto i : v) cout << i << " "; return 0; } Output1 3 6 22 7 22 Manually Using LoopIn this method, iterate through the vector using a loop and check the element at every index. If it is equal to given value, then assign it the new value. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 6, 2, 7, 2}; // Replacing all 2s in vector with 22 for (auto &i : v) { if (i == 2) i = 22; } for (auto i : v) cout << i << " "; return 0; } Output1 3 6 22 7 22 Comment More infoAdvertise with us Next Article How to Add Elements in a Vector in C++? T the_star_emperor Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to 3 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 How to Change an Element in a Vector in C++? In C++, vectors are dynamic containers that store the data in a contiguous memory location but unlike arrays, they can resize themselves to store more elements. In this article, we will learn how to change a specific element by its index in a vector in C++.The simplest way to modify an element is by 3 min read How to Insert an Element in a Sorted Vector in C++? In C++, inserting element in a sorted vector should be done such that it preserves the order of elements. In this article, we will learn different methods to insert an element in a sorted vector in C++.The recommended way to insert an element is to first find the position of insertion using lower_bo 4 min read How to Remove an Element from Vector in C++? In this article, we will learn how to remove a given element from the vector in C++.The most straightforward method to delete a particular element from a vector is to use the vector erase() method. Let's look at a simple example that shows how to use this function:C++#include <bits/stdc++.h> u 2 min read Like