TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm remove_copy() function

C++ algorithm remove_copy() function with c++ tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

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.

  • It returns an iterator to the new end of the range.
  • Remove is stable. It means, the relative order of the elements that are not removed is remain unchanged.
  • This function uses operator== to compare the elements to given val.

Syntax

template <class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& val);

Parameter

first: 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 value

A 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.

Complexity

Complexity is linear in the range [first, last): compare each element, and performs assignment operation for those which are not removed.

Data races

The objects in the range [first, last) are accessed.

The objects in the range between result and the returned value are changed.

Exception safety

This 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 1

Let'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 2

Let'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 3

Let'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 4

Let'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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf