![]() |
In C++, vectors are dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. In this article, we will see how to replace a particular element in a vector in C++. Example: Input: Replace a Particular Element in a Vector in C++To replace a specific element in a std::vector in C++, we can use the std::replace() function provided by the STL. The std::replace() function replaces all occurrences of a specific value in a range with another value. Approach
C++ Program to Replace a Specific Element in a VectorC++
Output
The initial vector is: 1 2 5 3 4 8 6 8 Replacing 5 with 8 After Replacing vector is: 1 2 8 3 4 8 6 8 Time complexity: O(N), N is size of vector. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |