============================MODULE36======================================= | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Examples compiled using g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #include //for system() #include ... { C++ codes... } ------------------------------------------------- should be changed to: ------------------------------------------------- #include //use C++ wrapper to call C functions from C++ programs... #include using namespace std; ... { C++ codes... } ------------------------------------------------- In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore. It should be C++ header, so that it comply to the standard. In older Borland C++ compiler this still works, but not proper any more... and for standard C/C++ the portability should be no problem or better you read Module23 at http://www.tenouk.com/Module23.html to get the big picture...For C codes, they still C codes :o) ========================================================================= ============================HERE, ALL C++ codes========================== //algorithm, push_heap() //make_heap() #include #include #include #include using namespace std; int main() { vector vec1; vector ::iterator Iter1; int i; for(i = 1; i <= 10; i++) vec1.push_back(i); random_shuffle(vec1.begin(), vec1.end()); cout<<"Given vector vec1 data: "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout<()); cout<<"The greater than heaped version of vec1 data:\n "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout<()); cout<<"The greater than reheaped vec1 with 13 added is:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include #include using namespace std; int main() { vector vec1; vector ::iterator Iter1; int i; for(i = 1; i <= 10; i++) vec1.push_back(i); cout<<"The original of vector vec1 data:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { vector vec1; vector ::iterator Iter1, new_end; int i; for(i = 1; i <= 10; i++) vec1.push_back(i); int j; for(j = 0; j <= 2; j++) vec1.push_back(8); cout<<"Vector vec1 original data: is\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { vector vec1, vec2(10); vector ::iterator Iter1, Iter2, new_end; int i; for(i = 0; i <= 10; i++) vec1.push_back(i); int j; for(j = 0; j <= 2; j++) vec1.push_back(5); cout<<"The original vector vec1 data:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; bool greathan(int value) { return value >7;} int main() { vector vec1, vec2(14); vector ::iterator Iter1, Iter2, new_end; int i; for(i = 0; i <= 10; i++) vec1.push_back(i); int j; for(j = 0; j <= 2; j++) vec1.push_back(5); cout<<"The original vector vec1 data:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; bool greathan(int value) {return value >5;} int main() { vector vec1; vector ::iterator Iter1, new_end; int i; for(i = 0; i <= 9; i++) vec1.push_back(i); int j; for(j = 0; j <= 2; j++) vec1.push_back(4); cout<<"Original vector vec1 data is:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { vector vec1; vector ::iterator Iter1; int i; for(i = 0; i <= 9; i++) vec1.push_back(i); int j; for(j = 0; j <= 2; j++) vec1.push_back(5); random_shuffle(vec1.begin(), vec1.end()); cout<<"The original random shuffle vector vec1 data is:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include #include using namespace std; int main() { vector vec1; list lst1 (15); vector ::iterator Iter1; list ::iterator lstIter; int i; for (i = 0; i <= 9; i++) vec1.push_back(i); int j; for (j = 0; j <= 3; j++) vec1.push_back(7); cout<<"The original vector vec1 data is:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include #include using namespace std; bool greaterthan(int value) {return value > 5;} int main() { vector vec1; list lst1 (13); vector ::iterator Iter1; list ::iterator lstIter1; int i; for (i = 0; i <= 9; i++) vec1.push_back(i); int j; for (j = 0; j <= 3; j++) vec1.push_back(7); cout<<"The original vector vec1 data is:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; bool greaterthan(int value) {return value > 4;} int main() { vector vec1; vector ::iterator Iter1; int i; for (i = 1; i <= 10; i++) vec1.push_back(i); int j; for (j = 0; j <= 2; j++) vec1.push_back(8); random_shuffle(vec1.begin(), vec1.end()); cout<<"The original random shuffle vector vec1 data is:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { vector vec1; vector ::iterator Iter1; int i; for(i = 11; i <= 20; i++) vec1.push_back(i); cout<<"The original vector vec1 is:\n "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { vector vec1, vec2(11); vector ::iterator Iter1, Iter2; int i; for(i = 10; i <= 20; i++) vec1.push_back(i); cout<<"The original vector vec1 data is:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include #include using namespace std; int main() { vector vec1; deque deq1; vector ::iterator vec1Iter1; deque::iterator deq1Iter1; int i; for(i = -4; i <= 4; i++) vec1.push_back(i); int j; for(j = -3; j <= 3; j++) deq1.push_back(j); cout<<"Vector vec1 data is: "; for(vec1Iter1 = vec1.begin(); vec1Iter1 != vec1.end(); vec1Iter1++) cout<<*vec1Iter1 <<" "; cout< #include #include #include using namespace std; int main() { vector vec1, vec2(9); deque deq1, deq2(6); vector ::iterator vec1Iter, vec2Iter; deque::iterator deq1Iter, deq2Iter; int i; for(i = -3; i <= 5; i++) vec1.push_back(i); int j; for(j =0; j <= 5; j++) deq1.push_back(j); cout<<"Vector vec1 data is: "; for(vec1Iter = vec1.begin(); vec1Iter != vec1.end(); vec1Iter++) cout<<*vec1Iter<<" "; cout< #include #include #include using namespace std; //Return whether second element is twice the first bool twice(int elem1, int elem2) {return (2 * elem1 == elem2);} int main() { vector vec1, vec2; list lst1; vector ::iterator Iter1, Iter2; list ::iterator lst1_Iter, lst1_inIter; int i; for(i = 0; i <= 5; i++) vec1.push_back(5*i); for(i = 0; i <= 5; i++) vec1.push_back(5*i); int j; for(j = 4; j <= 5; j++) lst1.push_back(5*j); int k; for(k = 2; k <= 4; k++) vec2.push_back(10*k); cout<<"Vector vec1 data: "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout<::iterator result1; result1 = search (vec1.begin(), vec1.end(), lst1.begin(), lst1.end()); if(result1 == vec1.end()) cout<<"There is no match of lst1 in vec1."<::iterator result2; result2 = search(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice); if(result2 == vec1.end()) cout<<"\nThere is no match of lst1 in vec1."< #include #include #include using namespace std; //Return whether second element is twice the first bool twice(int elem1, int elem2) {return 2 * elem1 == elem2;} int main() { vector vec1, vec2; vector ::iterator Iter1; int i; for(i = 0; i <= 5; i++) vec1.push_back(5*i); for(i = 0; i <= 2; i++) vec1.push_back(5); for(i = 0; i <= 5; i++) vec1.push_back(5*i); for(i = 0; i <= 2; i++) vec1.push_back(5); cout<<"Vector vec1 data: "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout<::iterator result1; result1 = search_n(vec1.begin(), vec1.end(), 3, 5); if(result1 == vec1.end()) cout<<"\nThere is no match for a sequence (5 5 5) in vec1."<::iterator result2; result2 = search_n(vec1.begin(), vec1.end(), 3, 5); if(result2 == vec1.end()) cout<<"\nThere is no match for a sequence (5 5 5) in vec1" <<" under the equivalence predicate twice."< #include //For greater() #include #include using namespace std; //Return whether modulus of elem1 is less than modulus of elem2 bool mod_lesser(int elem1, int elem2) { if (elem1 < 0) elem1 = - elem1; if (elem2 < 0) elem2 = - elem2; return (elem1 < elem2); } int main() { vector vec1a, vec1b, vec1(12); vector ::iterator Iter1a, Iter1b, Iter1, Result1; //Constructing vectors vec1a & vec1b with default less-than ordering int i; for(i = -2; i <= 3; i++) vec1a.push_back(i); int j; for(j =-2; j <= 1; j++) vec1b.push_back(j); cout<<"Original vector vec1a with range sorted by the\n" <<"binary predicate less than is: "; for(Iter1a = vec1a.begin(); Iter1a != vec1a.end(); Iter1a++) cout<<*Iter1a<<" "; cout< vec2a(vec1a), vec2b(vec1b), vec2(vec1); vector ::iterator Iter2a, Iter2b, Iter2, Result2; sort(vec2a.begin(), vec2a.end(), greater()); sort(vec2b.begin(), vec2b.end(), greater()); cout<<"\nOriginal vector vec2a with range sorted by the\n" <<"binary predicate greater is: "; for(Iter2a = vec2a.begin(); Iter2a != vec2a.end(); Iter2a++) cout<<*Iter2a<<" "; cout< vec3a(vec1a), vec3b(vec1b), vec3(vec1); vector ::iterator Iter3a, Iter3b, Iter3, Result3; sort(vec3a.begin(), vec3a.end(), mod_lesser); sort(vec3b.begin(), vec3b.end(), mod_lesser); cout<<"\nOriginal vector vec3a with range sorted by the\n" <<"binary predicate mod_lesser() is: "; for(Iter3a = vec3a.begin(); Iter3a != vec3a.end(); Iter3a++) cout<<*Iter3a<<" "; cout<() Result1 = set_difference(vec1a.begin(), vec1a.end(), vec1b.begin(), vec1b.end(), vec1.begin()); cout<<"\nset_difference() of source ranges with default order," <<"\nvector vec1mod = "; for(Iter1 = vec1.begin(); Iter1 != Result1; Iter1++) cout<<*Iter1<<" "; cout<() Result2 = set_difference(vec2a.begin(), vec2a.end(), vec2b.begin(), vec2b.end(), vec2.begin(), greater ()); cout<<"\nset_difference() of source ranges with binary" <<" predicate\ngreater specified, vector vec2mod: "; for(Iter2 = vec2.begin(); Iter2 != Result2; Iter2++) cout<<*Iter2<<" "; cout< #include //For greater() #include #include using namespace std; //Return whether modulus of elem1 is less than modulus of elem2 bool mod_lesser(int elem1, int elem2) { if(elem1 < 0) elem1 = - elem1; if(elem2 < 0) elem2 = - elem2; return (elem1 < elem2); } int main() { vector vec1a, vec1b, vec1(12); vector ::iterator Iter1a, Iter1b, Iter1, Result1; //Constructing vectors vec1a & vec1b with default less than ordering int i; for(i = -2; i <= 2; i++) vec1a.push_back(i); int j; for(j = -4; j <= 0; j++) vec1b.push_back(j); cout<<"Original vector vec1a with range sorted by the\n" <<"binary predicate less than is: "; for(Iter1a = vec1a.begin(); Iter1a != vec1a.end(); Iter1a++) cout<<*Iter1a<<" "; cout< vec2a(vec1a), vec2b(vec1b), vec2(vec1); vector ::iterator Iter2a, Iter2b, Iter2, Result2; sort(vec2a.begin(), vec2a.end(), greater()); sort(vec2b.begin(), vec2b.end(), greater()); cout<<"\nOriginal vector vec2a with range sorted by the\n" <<"binary predicate greater is: "; for(Iter2a = vec2a.begin(); Iter2a != vec2a.end(); Iter2a++) cout<<*Iter2a<<" "; cout<vec3a(vec1a), vec3b(vec1b), vec3(vec1); vector::iterator Iter3a, Iter3b, Iter3, Result3; sort(vec3a.begin(), vec3a.end(), mod_lesser); sort(vec3b.begin(), vec3b.end(), mod_lesser); cout<<"\nOriginal vector vec3a with range sorted by the\n" <<"binary predicate mod_lesser() is: "; for(Iter3a = vec3a.begin(); Iter3a != vec3a.end(); Iter3a++) cout<<*Iter3a<<" "; cout<() Result1 = set_intersection(vec1a.begin(), vec1a.end(), vec1b.begin(), vec1b.end(), vec1.begin()); cout<<"\nset_intersection() of source ranges with default order,\nvector vec1mod: "; for(Iter1 = vec1.begin(); Iter1 != Result1; Iter1++) cout<<*Iter1<<" "; cout<() Result2 = set_intersection(vec2a.begin(), vec2a.end(), vec2b.begin(), vec2b.end(), vec2.begin(), greater()); cout<<"\nset_intersection() of source ranges with binary predicate\ngreater " <<"specified, vector vec2mod: "; for(Iter2 = vec2.begin(); Iter2 != Result2; Iter2++) cout<<*Iter2<<" "; cout< #include #include #include using namespace std; int main() { vector vec1; vector ::iterator Iter1; int i; for(i = 1; i <= 10; i++) vec1.push_back(i); cout<<"The original of vector vec1 data:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include #include using namespace std; //Return whether second element is twice the first bool twice(int elem1, int elem2) {return 2 * elem1 == elem2;} int main() { vector vec1, vec2; vector ::iterator Iter1; int i; for(i = 0; i <= 5; i++) vec1.push_back(5*i); for(i = 0; i <= 2; i++) vec1.push_back(5); for(i = 0; i <= 5; i++) vec1.push_back(5*i); for(i = 0; i <= 2; i++) vec1.push_back(5); cout<<"Vector vec1 data: "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout<::iterator result1; result1 = search_n(vec1.begin(), vec1.end(), 3, 5); if(result1 == vec1.end()) cout<<"\nThere is no match for a sequence (5 5 5) in vec1."<::iterator result2; result2 = search_n(vec1.begin(), vec1.end(), 3, 5); if(result2 == vec1.end()) cout<<"\nThere is no match for a sequence (5 5 5) in vec1" <<" under the equivalence predicate twice."<