1: //remove_copy_if1a.cpp 3: #include <iostream> 4: #include <vector> 5: #include <algorithm> 6: using namespace std; 8: /** 9: Determines if an integer is divisible by 3. 10: Pre:\n n contains an integer. 11: Post:\n Returns true if n is divisible by 3, and otherwise false. 12: */ 13: bool isDivisibleByThree 14: ( 15: int n //in 16: ) 17: { 18: return (n%3 == 0); 19: } 21: int main() 22: { 23: cout << "\nThis program illustrates the use of the STL " 24: "remove_copy_if() algorithm\nto copy all values except those " 25: "satisfying a particular condition from\none vector of integers " 26: "to another."; 27: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 29: int a1[] = {1, 2, 2, 3, 4, 5, 2, 3, 6, 7, 2, 8, 3, 9, 10, 2}; 30: vector<int> v1(a1, a1+16); 32: cout << "\nHere are the sixteen values in v1:\n"; 33: for (vector<int>::size_type i=0; i<v1.size(); i++) 34: cout << v1.at(i) << " "; 35: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 37: int a2[] = {101, 102, 103, 104, 105, 106, 107, 108, 38: 109, 110, 111, 112, 113, 114, 115, 116}; 39: vector<int> v2(a2, a2+16); 41: cout << "\nHere are the sixteen values in v2:\n"; 42: for (vector<int>::size_type i=0; i<v2.size(); i++) 43: cout << v2.at(i) << " "; 44: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 46: cout << "\nNow we copy all values except those that are divisible " 47: "\nby 3 from v1 to v2, starting at the 3rd value of v2."; 48: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 50: vector<int>::iterator p = remove_copy_if(v1.begin(), v1.end(), 51: v2.begin()+2, isDivisibleByThree); 53: cout << "\nHere are the revised values in v2:\n"; 54: for (vector<int>::size_type i=0; i<v2.size(); i++) 55: cout << v2.at(i) << " "; 56: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 58: cout << "\nThe iterator returned by the algorithm is pointing at " 59: "the value " << *p << "."; 60: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 61: }