C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm remove_copy()C++ Algorithm remove_copy() function is used to copy all elements which are not equal to val from the range [first, last) to provide result without disturbing the order of the remaining elements. This function cannot alter the size of the container.
Syntaxtemplate <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& val); Parameterfirst: A forward iterator pointing the position of the first element in the range from which elements are being removed. last: A forward iterator pointing the position one past the final element in the range from which elements are being removed. result: An output iterator pointing to the initial position of the range to which elements are being removed. val: The value that is to be removed from the range [first, last). Return valueA forward iterator pointing the new end position (last) of the copied range, which includes all elements in [first, last) except those compare equal to val. ComplexityComplexity is linear in the range [first, last): compare each element, and performs assignment operation for those which are not removed. Data racesThe objects in the range [first, last) are accessed. The objects in the range between result and the returned value are changed. Exception safetyThis function throws an exception if any of element comparison, the element assignments or the operations on iterator throws an exception. Note: Invalid parameters may cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of remove_copy(): #include <algorithm> #include <iostream> #include <vector> #include <iterator> using namespace std; int main() { vector<int> v = { 2,3,1,2,1 }; remove_copy(v.begin(), v.end(), ostream_iterator<int>(cout, ","), 1); return 0; } Output: 2,3,2, Example 2Let's see another simple example: #include <iostream> // std::cout #include <algorithm> // std::remove_copy #include <vector> // std::vector using namespace std; int main () { int myints[] = {10,20,50,30,20,10,40,20}; vector<int> myvector (8); remove_copy (myints,myints+8,myvector.begin(),20); cout << "myvector contains:"; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) cout << ' ' << *it; cout << '\n'; return 0; } Output: myvector contains: 10 50 30 10 40 0 0 0 Example 3Let's see another simple example to remove all the spaces from a given text: #include <algorithm> #include <iterator> #include <string> #include <iostream> using namespace std; int main() { string str = "Text with some spaces"; cout << "before: " << str << "\n"; cout << "after: "; remove_copy(str.begin(), str.end(), ostream_iterator<char>(cout), ' '); cout << '\n'; return 0; } Output: before: Text with some spaces after: Textwithsomespaces Example 4Let's see another simple example: #include <vector> #include <algorithm> #include <iostream> int main() { using namespace std; vector <int> v1, v2(10); vector <int>::iterator Iter1, Iter2, new_end; int i; for ( i = 0 ; i <= 9 ; i++ ) v1.push_back( i ); int ii; for ( ii = 0 ; ii <= 3 ; ii++ ) v1.push_back( 7 ); random_shuffle (v1.begin( ), v1.end( ) ); cout << "The original vector v1 is: ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")." << endl; // Remove elements with a value of 7 new_end = remove_copy ( v1.begin( ), v1.end( ), v2.begin( ), 7 ); cout << "Vector v1 is left unchanged as ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")." << endl; cout << "Vector v2 is a copy of v1 with the value 7 removed:\n ( " ; for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ ) cout << *Iter2 << " "; cout << ")." << endl; } Output: The original vector v1 is: ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ). Vector v1 is left unchanged as ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ). Vector v2 is a copy of v1 with the value 7 removed: ( 4 0 5 1 6 9 3 8 2 0 ).
Next TopicC++ Algorithm
|