std::string::remove_copy(), std::string::remove_copy_if() in C++
Last Updated :
29 May, 2018
remove_copy()
It is an STL function in c++ which is defined in algorithm library. It copies the elements in the range [first, last) to the range beginning at result, except those elements that compare equal to given elements.
- The resulting range is shorter than [first,last) by as many elements as matches in the sequence, which are "removed".
- The relative order of the elements not removed is preserved.
- The function uses operator == to compare the individual elements to given value.
Function Template
ResultIterator remove_copy(ForwardIterator first, ForwardIterator last,
ResultIterator result ,const T& ele);
first, last : Forward iterators to the initial and final positions
in a sequence. The range used is [first, last), which contains all the elements
between first and last, including the element pointed by first but not
the element pointed by last.
result : Output iterator to the initial position of the range
where the resulting sequence is stored. The pointed type shall support being
assigned the value of an element in the range [first, last).
ele : element to be removed.
Examples:
Input : b d a f g h a k given element is a
Output :b d f g h k _ _
Input : b k c s n m c l given element is c
Output : b k s n m l _ _
'_' represent remove places
CPP
// CPP code to demonstrate remove_copy()
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
// Function to remove_copy from v1 result vector is v2
void removecopyDemo(vector <int> &v1)
{
remove_copy(v1.begin(), v1.end(), v1.begin(), 3);
}
// Function to print content of vector
void print(vector<int>&v)
{
int len = v.size();
for (int i = 0; i < len; i++)
cout << v[i] << " ";
cout << endl;
}
// Driver code
int main()
{
// vector
vector <int> v1, v2(10);
// push data in vector
for(int i = 10; i <= 25; i++)
v1.push_back(i % 6);
cout << "elements of v1 before remove_copy: "<<endl;
print(v1);
removecopyDemo(v1);
cout << "After removing element 3" <<endl;
print(v1);
return 0;
}
Output:
elements of v1 before remove_copy:
4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1
After removing element 3
4 5 0 1 2 4 5 0 1 2 4 5 0 1 0 1
Complexity: Linear O(n)
remove_copy_if
It is a STL function in c++ which is defined in algorithm library. It copies the elements in the range [first, last) to the range beginning at result, except those elements that meets to a given condition (like odd number, even number, prime number, non-prime number etc ).
- Copies the elements in the range [first, last) to the range beginning at result, except those elements for which condition function returns true.
- The resulting range is shorter than [first, last) by as many elements as matches, which are "removed".
Function Template
ResultIterator remove_copy_if(ForwardIterator first, ForwardIterator last,
ResultIterator result, UnaryPredicate pred);
pred : Unary function that accepts an element in the range as
argument, and returns a value convertible to bool. The value returned indicates
whether the element is to be removed (if true, it is removed).
Examples:
Input : 1 2 3 4 5 6 7 8 9 check if a number is prime and remove
Output :1 4 6 8 9 0 0 0 0
Input :1 2 3 4 5 6 7 8 9 check if a number is even and remove
Output :1 3 5 7 9 0 0 0 0
CPP
// CPP code to demonstrate remove_copy_if()
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool IsOdd(int i) { return ((i % 2) != 0); }
// Function to remove_copy from v1 result vector is v2
void remove_copy_ifDemo(vector <int> &v1, vector<int> &v2)
{
remove_copy_if(v1.begin(), v1.end(), v2.begin(), IsOdd);
}
// Function to print content of vector
void print(vector<int>&v)
{
int len = v.size();
for (int i = 0; i < len; i++)
cout << v[i] << " ";
cout << endl;
}
// Driver code
int main()
{
// declare vector v1, v2
vector <int> v1, v2(10);
// push data in vector
for(int i = 10; i <= 20; i++)
v1.push_back(i);
cout << "elements of v1 before remove_copy: ";
print(v1);
remove_copy_ifDemo(v1,v2);
cout << "elements of v1 after remove_copy: ";
print(v1);
cout << "After removing Odd Numbers from v1"
" copy result in vector v2" <<endl;
print(v2);
return 0;
}
Output:
elements of v1 before remove_copy: 10 11 12 13 14 15 16 17 18 19 20
elements of v1 after remove_copy: 10 11 12 13 14 15 16 17 18 19 20
After removing Odd Numbers from v1 copy result in vector v2
10 12 14 16 18 20 0 0 0 0
Complexity: Linear O(n)
List of <algorithms> library in C++ STL
All STL articles of C++
Similar Reads
std::string::replace_copy(), std::string::replace_copy_if in C++ replace_copy replace_copy() is a combination of copy() and replace(). It copies the elements in the range [first, last) to the range beginning at result, replacing the appearances of old_value by new_value.The range copied is [first, last), which contains all the elements between first and last, inc
4 min read
Remove duplicates from a string using STL in C++ Given a string S, remove duplicates in this string using STL in C++ Examples: Input: Geeks for geeks Output: Gefgkors Input: aaaaabbbbbb Output: ab Approach: The consecutive duplicates of the string can be removed using the unique() function provided in STL. Below is the implementation of the above
1 min read
std::string::append vs std::string::push_back() vs Operator += in C++ To append characters, you can use operator +=, append(), and push_back(). All of them helps to append character but with a little difference in implementation and application. Operator += : appends single-argument values. Time complexity : O(n)append() : lets you specify the appended value by using
6 min read
std::string::length, std::string::capacity, std::string::size in C++ STL Prerequisite: String in C++ String class is one of the features provided by the Standard template library to us, So it comes up with great functionality associated with it. With these Functionalities, we can perform many tasks easily. Let's see a few of the functionalities string class provides. Hea
6 min read
string::rbegin() and string::rend() in C++ The std::string::rbegin() and std::string::rend() functions in C++ are used to fetch the reverse iterators to the string. They are the member function of std::string and are used when we want to iterate the string in reverse. In this article, we will learn how to use string::rbegin() and string::ren
2 min read