============================MODULE34======================================= | | | 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========================== //algoritm, adjacent_find() #include #include #include using namespace std; //Returns whether second element is twice the first bool twice(int elem1, int elem2) {return (elem1 * 2 == elem2);} int main() { list lst; list::iterator Iter; list::iterator result1, result2; lst.push_back(14); lst.push_back(17); lst.push_back(31); lst.push_back(31); lst.push_back(10); lst.push_back(20); cout << "List lst data: "; for(Iter = lst.begin(); Iter != lst.end(); Iter++) cout<<*Iter<< " "; cout< #include #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() { list lst; list::iterator Iter; bool b1, b2; lst.push_back(13); lst.push_back(23); lst.push_back(10); lst.push_back(33); lst.push_back(35); lst.push_back(9); lst.sort(); cout<<"List lst data: "; for(Iter = lst.begin(); Iter != lst.end(); Iter++) cout<<*Iter<<" "; cout<()); if(b2) cout<<"\nThere is an element in list lst with a\nvalue equivalent to 13 " <<"under greater than."< vec; vector ::iterator Iter1; int i; for(i = -3; i <= 5; i++) vec.push_back(i); sort(vec.begin(), vec.end(), mod_lesser); cout<<"\nOrdered under mod_lesser, vector vec data:\n"; for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { vector vec1, vec2; vector ::iterator Iter1, Iter2; int i; for(i = 0; i <= 5; i++) vec1.push_back(i); int j; for(j = 10; j <= 20; j++) vec2.push_back(j); cout<<"vec1 data: "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { vector vec1, vec2; vector ::iterator Iter1, Iter2; int i; for(i = 10; i <= 15; i++) vec1.push_back(i); int j; for(j = 0; j <= 10; j++) vec2.push_back(j); cout<<"vec1 data: "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { vector vec; vector ::iterator Iter; vec.push_back(12); vec.push_back(22); vec.push_back(12); vec.push_back(31); vec.push_back(12); vec.push_back(33); cout<<"vec data: "; for(Iter = vec.begin(); Iter != vec.end(); Iter++) cout<<*Iter<<" "; cout< #include #include using namespace std; bool isgreat(int value) {return value >8;} int main() { vector vec; vector ::iterator Iter; vec.push_back(13); vec.push_back(21); vec.push_back(9); vec.push_back(31); vec.push_back(8); vec.push_back(10); cout<<"vec data: "; for(Iter = vec.begin(); Iter != vec.end(); Iter++) cout<<*Iter<<" "; cout< #include #include #include #include using namespace std; //Return true if string str starts with letter 'C' int MatchFirstChar(const string& str) { string s("C"); return s == str.substr(0, 1); } int main() { const int VECTOR_SIZE = 110; //Define a template class vector of strings typedef vector StringVector; //Define an iterator for template class vector of strings typedef StringVector::iterator StringVectorIt; //vector containing names StringVector NamesVect(VECTOR_SIZE); StringVectorIt start, end, it; //stores count of elements that match value. ptrdiff_t result = 0; //Initialize vector NamesVect NamesVect[0] = "Learn"; NamesVect[1] = "C"; NamesVect[2] = "and"; NamesVect[3] = "C++"; NamesVect[4] = "also"; NamesVect[5] = "Visual"; NamesVect[6] = "C++"; NamesVect[7] = "and"; NamesVect[8] = "C++"; NamesVect[9] = ".Net"; //location of first element of NamesVect start = NamesVect.begin(); //one past the location last element of NamesVect end = NamesVect.end(); //print content of NamesVect cout<<"NamesVect: "; for(it = start; it != end; it++) cout<<*it<<" "; 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 vec1; vector ::iterator Iter1; pair < vector ::iterator, vector ::iterator > Result1, Result2, Result3; //Constructing vectors vec1 with default less than ordering int i; for(i = -2; i <= 4; i++) vec1.push_back(i); int j; for(j =1; j <= 5; j++) vec1.push_back(j); sort(vec1.begin(), vec1.end()); cout<<"vec1 data with range sorted by the binary predicate less than is:\n"; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< vec2(vec1); vector ::iterator Iter2; sort(vec2.begin(), vec2.end(), greater()); cout<<"\nvec2 data with range sorted by the binary predicate greater than is:\n"; for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++) cout<<*Iter2<<" "; cout< vec3(vec1); vector ::iterator Iter3; sort(vec3.begin(), vec3.end(), mod_lesser); cout<<"\nvec3 data with range sorted by the binary predicate mod_lesser is:\n"; for(Iter3 = vec3.begin(); Iter3 != vec3.end(); Iter3++) cout<<*Iter3<<" "; cout<<"\n\n"; //equal_range of 4 in vec1 with default binary predicate less () Result1 = equal_range(vec1.begin(), vec1.end(), 4); cout<<"lower_bound in vec1 for the element with a value of 4 is: "<<*Result1.first<() Result2 = equal_range(vec2.begin(), vec2.end(), 4, greater ()); cout<<"lower_bound in vec2 for the element with a value of 4 is: "<<*Result2.first< #include #include using namespace std; int main() { vector vec; vector ::iterator Iter1; int i; for(i = 10; i <= 20; i++) vec.push_back(i); cout<<"Vector vec data: "; for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { vector vec; vector ::iterator Iter1; int i; for(i = 10; i <= 20; i++) vec.push_back(i); cout<<"Vector vec data: "; for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include using namespace std; int main() { list lst; list ::iterator Iter; list ::iterator result; lst.push_back(9); lst.push_back(21); lst.push_back(14); lst.push_back(10); lst.push_back(16); lst.push_back(31); cout<<"List lst data: "; for(Iter = lst.begin(); Iter != lst.end(); Iter++) cout<<*Iter<<" "; 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 lst; vector ::iterator Iter1, Iter2; list ::iterator lst_Iter, lst_inIter; int i; for(i = 10; i <= 15; i++) vec1.push_back(i); int j; for(j = 11; j <= 14; j++) lst.push_back(j); int k; for(k = 12; k <= 14; k++) vec2.push_back(2*k); cout<<"Vector vec1 data: "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout<::iterator result1; result1 = find_end(vec1.begin(), vec1.end(), lst.begin(), lst.end()); if(result1 == vec1.end()) cout<<"There is no match of lst in vec1."<::iterator result2; result2 = find_end(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice); if(result2 == vec1.end()) cout<<"\nThere is no match of lst 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; list lst; vector ::iterator Iter1, Iter2; list ::iterator lst_Iter, lst_inIter; int i; for(i = 0; i <= 5; i++) vec1.push_back(5*i); int j; for(j = 3; j <= 4; j++) lst.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 = find_first_of(vec1.begin(), vec1.end(), lst.begin(), lst.end()); if(result1 == vec1.end()) cout<<"\nThere is no match of lst in vec1."<::iterator result2; result2 = find_first_of(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice); if(result2 == vec1.end()) cout<<"\nThere is no match of lst in vec1."< #include #include using namespace std; bool great(int value) {return value>13;} int main() { list lst; list ::iterator Iter; list ::iterator result; lst.push_back(13); lst.push_back(9); lst.push_back(10); lst.push_back(22); lst.push_back(31); lst.push_back(17); cout<<"List lst data: "; for(Iter = lst.begin(); Iter != lst.end(); Iter++) cout<<*Iter<<" "; cout< #include #include using namespace std; //The function object multiplies an element by a Factor template class MultValue { private: //The value to multiply by Type Factor; public: //Constructor initializes the value to multiply by MultValue(const Type& _Val) : Factor(_Val) {} //The function call for the element to be multiplied void operator()(Type& elem) const {elem *= Factor;} }; //The function object to determine the average class Average { private: //The number of elements long num; //The sum of the elements long sum; public: //Constructor initializes the value to multiply by Average() : num(0), sum(0){} //The function call to process the next elment void operator()( int elem ) \ { //Increment the element count num++; //Add the value to the partial sum sum += elem; } //return Average operator double() { return (static_cast (sum))/(static_cast (num)); } }; int main() { vector vec; vector ::iterator Iter1; //Constructing vector vec int i; for(i = -3; i <= 4; i++) vec.push_back(i); cout<<"vector vec data: "; for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++) cout<<*Iter1<<" "; cout<(-2)); cout<<"\nMultiplying the elements of the vector vec\n" <<"by the factor -2 gives:\nvecmult1 data: "; for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++) cout<<*Iter1<<" "; cout<(5)); cout<<"\nMultiplying the elements of the vector vecmult1\n" <<"by the factor 5 gives:\nvecmult2 data: "; for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++) cout<<*Iter1<<" "; cout< #include #include #include using namespace std; int main() { //Assigning random values to vector integer elements vector vec(7); vector ::iterator Iter1; deque deq(7); deque ::iterator deqIter; cout<<"\nOperation: generate_n(vec.begin(), 7, rand)\n"; generate_n(vec.begin(), 7, rand); cout<<"Vector vec data: "; for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++) cout<<*Iter1<<" "; 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 vec1, vec2; vector ::iterator Iter1, Iter2; //Constructing vectors vec1 & vec2 with default less-than ordering int i; for(i = -2; i <= 4; i++) vec1.push_back(i); int j; for(j =-2; j <= 3; j++) vec2.push_back(j); cout<<"vector vec1 data with range sorted by the " <<"binary predicate\nless than is: "; for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout<<*Iter1<<" "; cout< vec3(vec1), vec4(vec2); vector ::iterator Iter3, Iter4; sort(vec3.begin(), vec3.end(), greater()); sort(vec4.begin(), vec4.end(), greater()); vec3.pop_back(); cout<<"\nvector vec3 data with range sorted by the " <<"binary predicate\ngreater is: "; for(Iter3 = vec3.begin(); Iter3 != vec3.end(); Iter3++) cout<<*Iter3<<" "; cout< vec5(vec1), vec6(vec2); vector ::iterator Iter5, Iter6; reverse(vec5.begin(), vec5.end()); vec5.pop_back(); vec5.pop_back(); sort(vec5.begin(), vec5.end(), mod_lesser); sort(vec6.begin(), vec6.end(), mod_lesser); cout<<"\nvector vec5 data with range sorted by the " <<"binary predicate\nmod_lesser is: "; for(Iter5 = vec5.begin(); Iter5 != vec5.end(); Iter5++) cout<<*Iter5<<" "; cout<() bool Result1; Result1 = includes(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()); if(Result1) cout<<"\nAll the elements in vector vec2 are contained in vector vec1."<() bool Result2; Result2 = includes(vec3.begin(), vec3.end(), vec4.begin(), vec4.end(), greater()); if(Result2) cout<<"\nAll the elements in vector vec4 are contained\nin vector vec3."< #include #include using namespace std; int main() { vector vec1, vec2; vector ::iterator Iter1, Iter2; int i; for(i = 0; i <= 5; i++) vec1.push_back(i); int j; for(j = 10; j <= 20; j++) vec2.push_back(j); cout<<"vec1 data: "; 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; list lst; vector ::iterator Iter1, Iter2; list ::iterator lst_Iter, lst_inIter; int i; for(i = 0; i <= 5; i++) vec1.push_back(5*i); int j; for(j = 3; j <= 4; j++) lst.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 = find_first_of(vec1.begin(), vec1.end(), lst.begin(), lst.end()); if(result1 == vec1.end()) cout<<"\nThere is no match of lst in vec1."<::iterator result2; result2 = find_first_of(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice); if(result2 == vec1.end()) cout<<"\nThere is no match of lst in vec1."<