============================MODULE32======================================= | | | 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========================== //insert_iterator, operator++ //the increment... #include #include #include using namespace std; int main() { int i; vector vec; for(i = 10; i<=15; ++i) vec.push_back(i); vector ::iterator veciter; cout<<"The vector vec data: "; //iterate all the elements and print... for(veciter = vec.begin(); veciter != vec.end(); veciter++) cout<<*veciter<<" "; cout< > j(vec, vec.begin()); *j = 17; j++; *j = 9; cout<<"After the insertions, the vector vec data:\n"; for(veciter = vec.begin(); veciter != vec.end(); veciter++) cout<<*veciter<<" "; cout< #include #include using namespace std; int main() { int i; list::iterator lstiter; list lst; for(i = 10; i<=15; ++i) lst.push_back(i); cout<<"The list lst data: "; for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++) cout<<*lstiter<<" "; cout< > Iter(lst, lst.begin()); *Iter = 12; *Iter = 7; *Iter = 33; *Iter = 24; cout<<"\nOperation: Iter(lst, lst.begin()) then *Iter = 12...\n"; cout<<"After the insertions, the list lst data:\n"; for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++) cout<<*lstiter<<" "; cout< #include #include using namespace std; int main() { typedef istream_iterator::char_type chtype; typedef istream_iterator::traits_type tratype; //Standard iterator interface for reading //elements from the input stream... cout<<"Enter integers separated by spaces & then\n" <<" any character e.g.: '3 4 7 T': "; //istream_iterator for reading int stream istream_iterator intread(cin); //End-of-stream iterator istream_iterator EOFintread; while(intread != EOFintread) { cout<<"Reading data: "<<*intread< #include #include #include using namespace std; int main() { //Used in conjunction with copy algorithm //to put elements into a vector read from cin vector vec(5); vector::iterator Iter; cout<<"Enter 5 integers separated by spaces & then\n" <<" a character e.g: '4 6 2 7 11 R': "; istream_iterator intvecread(cin); //Default constructor will test equal to end of stream //for delimiting source range of vector copy(intvecread, istream_iterator(), vec.begin()); cin.clear(); cout<<"vec data: "; for(Iter = vec.begin(); Iter != vec.end(); Iter++) cout<<*Iter<<" "; cout< #include #include #include using namespace std; int main() { typedef istreambuf_iterator::char_type chatype; typedef istreambuf_iterator::traits_type tratype; cout<<"Enter line of text, then press Return key to \n" <<"insert into the output, & use a ctrl-Z Enter key\n" <<"combination to exit: "; //istreambuf_iterator for input stream istreambuf_iterator< chatype, tratype> charInBuf(cin); ostreambuf_iterator charOut(cout); //Used in conjunction with replace_copy algorithm //to insert into output stream and replaces spaces //with hash sign replace_copy(charInBuf, istreambuf_iterator(), charOut, ' ', '#'); return 0; } ----------------------------------------------------------------------------------------- //istreambuf_iterator, int_type #include #include using namespace std; int main() { cout<<"Operation: int_type intype = 77\n"; istreambuf_iterator::int_type intype = 77; cout<<"The int_type type = "< #include using namespace std; int main() { cout<<"\nOperation: bol = readchinpt1.equal(readchinpt2)\n"; cout<<"Enter a line of text then an Enter key to\n" <<"insert into the output:\n"; istreambuf_iterator readchinpt1(cin); istreambuf_iterator readchinpt2(cin); bool bol = readchinpt1.equal(readchinpt2); if(bol) cout<<"The iterators are equal."< #include #include #include using namespace std; int main() { istreambuf_iterator::istream_type &istrm = cin; istreambuf_iterator::streambuf_type *strmbf = cin.rdbuf(); cout<<"Enter a line of text, then an Enter key to insert into\n" <<"the output, (& use a ctrl-Z Enter key combination to exit):\n"; istreambuf_iterator charReadIn(cin); ostreambuf_iterator charOut(cout); //Used in conjunction with replace_copy algorithm //to insert into output stream and replace spaces //with hyphen-separators replace_copy(charReadIn, istreambuf_iterator(), charOut, ' ', '-'); return 0; } ----------------------------------------------------------------------------------------- //istreambuf_iterator, operator++ #include #include using namespace std; int main() { cout<<"Type a line of text & enter to output it, with stream\n" <<"buffer iterators, repeat as many times as desired,\n" <<"then keystroke ctrl-Z Enter to exit program: \n"; istreambuf_iterator inpos(cin); istreambuf_iterator endpos; ostreambuf_iterator outpos(cout); while(inpos != endpos) { *outpos = *inpos; //Increment istreambuf_iterator ++inpos; ++outpos; } return 0; } ----------------------------------------------------------------------------------------- //ostream_iterator, ostream_iterator #include #include #include using namespace std; int main() { //ostream_iterator for stream cout ostream_iterator intOut(cout, "\n"); *intOut = 12; intOut++; *intOut = 33; intOut++; int i; vector vec; for(i = 10; i<=15; ++i) vec.push_back(i); cout<<"Operation: with and without delimiter...\n"; //Write elements to standard output stream cout<<"Elements output without delimiter: "; copy(vec.begin(), vec.end(), ostream_iterator (cout)); cout< (cout, " ")); cout< #include #include using namespace std; int main() { //ostream_iterator for stream cout //with new line delimiter ostream_iterator intOut(cout, "\n"); //Standard iterator interface for writing //elements to the output stream cout<<"Elements written to output stream:\n"; *intOut = 12; *intOut = 21; //No effect on iterator position intOut++; *intOut = 9; *intOut = 7; return 0; } ----------------------------------------------------------------------------------------- //ostreambuf_iterator, ostreambuf_iterator #include #include #include using namespace std; int main() { // ostreambuf_iterator for stream cout ostreambuf_iterator charOut(cout); *charOut = '7'; charOut ++; *charOut = 'T'; charOut ++; *charOut = 'W'; cout<<" are characters output."< strOut(cout); string str = "These characters are being written to the output stream.\n "; copy(str.begin(), str.end(), strOut); return 0; } ----------------------------------------------------------------------------------------- //ostreambuf_iterator, failed() #include #include #include using namespace std; int main() { //ostreambuf_iterator for stream cout ostreambuf_iterator charOut(cout); *charOut = 'T'; charOut ++; *charOut = '7'; charOut ++; *charOut = 'R'; cout<<" are characters output"< #include #include #include using namespace std; int main() { int i; vector vec; for(i = 10; i<=17; ++i) vec.push_back(i); cout<<"Normal....\n"; vector ::iterator vIter; cout<<"The vector vec data: "; for(vIter = vec.begin(); vIter != vec.end(); vIter++) cout<<*vIter<<" "; cout<::reverse_iterator rvIter; cout<<"The vector vec reversed data: "; for(rvIter = vec.rbegin(); rvIter != vec.rend(); rvIter++) cout<<*rvIter<<" "; cout<::iterator pos; pos = find(vec.begin(), vec.end(), 15); cout<<"The iterator pos points to: "<<*pos<::iterator> rpos(pos); //Declare a difference type for a parameter reverse_iterator::iterator>::difference_type diff = 3; cout<<"\nOperation: rpos(pos)\n"; cout<<"The iterator rpos points to: "<<*rpos<::iterator>::reference refrpos = rpos[diff]; cout<<"The iterator rpos now points to: "< #include #include #include #include using namespace std; int main() { typedef vector > pVector; pVector vec; vec.push_back(pVector::value_type(1, 2)); vec.push_back(pVector::value_type(3, 4)); vec.push_back(pVector::value_type(5, 6)); pVector::iterator pvIter; cout<<"Operation: pvIter->first and pvIter->second\n"; cout<<"The vector vec of integer pairs is: \n"; for(pvIter = vec.begin(); pvIter != vec.end(); pvIter++) cout<first<<", "<second<first and rpvIter->second"; cout<<"\nThe vector vec reversed is: \n"; for(rpvIter = vec.rbegin(); rpvIter != vec.rend(); rpvIter++) cout<first<< ", " <second<first<< ", " <second<first<< ", " <second< #include #include #include using namespace std; int main() { int i; vector vec; for(i = 10; i<=15; ++i) vec.push_back(i); vector ::iterator vIter; cout<<"The vector vec data: "; for(vIter = vec.begin(); vIter != vec.end(); vIter++) cout<<*vIter<<" "; cout<::reverse_iterator rvIter; cout<<"The vector vec reversed data: "; for(rvIter = vec.rbegin(); rvIter != vec.rend(); rvIter++) cout<<*rvIter<<" "; cout<::iterator pos, bpos; pos = find(vec.begin(), vec.end(), 13); cout<<"The iterator pos points to: "<<*pos<::iterator>::iterator_type it_vec_int_type; cout<<"\nFinding data, reverse...\n"; cout<<"Operation: rpos(pos)\n"; reverse_iterator rpos(pos); cout<<"The reverse_iterator rpos points to: "<<*rpos< #include #include #include #include #include using namespace std; int main() { list lst; list ::iterator lstIter; //insert elements from 1 to 10 into the lst list for(int i=1; i<=10; ++i) lst.push_back(i); cout<<"Operation: lst.push_back(i)\n"; cout<<"lst data: "; for(lstIter = lst.begin(); lstIter != lst.end(); lstIter++) cout<<*lstIter<<' '; cout< vec; vector ::iterator Iter; //from source to destination... copy(lst.begin(), lst.end(), back_inserter(vec)); cout<<"\nOperation: copy(lst.begin(), lst.end(), back_inserter(vec))\n"; cout<<"vec data: "; for(Iter = vec.begin(); Iter != vec.end(); Iter++) cout<<*Iter<<" "; cout< deq; deque ::iterator deqIter; copy(lst.begin(), lst.end(), front_inserter(deq)); cout<<"\nOperation: copy(lst.begin(), lst.end(), front_inserter(deq))\n"; cout<<"deq data: "; for(deqIter = deq.begin(); deqIter != deq.end(); deqIter++) cout<<*deqIter<<" "; cout< st; set::iterator stIter; copy(lst.begin(), lst.end(), inserter(st, st.begin())); cout<<"\nOperation: copy(lst.begin(), lst.end(), inserter(st, st.begin()))\n"; cout<<"set data: "; for(stIter = st.begin(); stIter != st.end(); stIter++) cout<<*stIter<<" "; cout< #include #include #include using namespace std; int main() { vector strvec; vector ::iterator Iter; //read from the standard input until EOF/error //the EOF is platform dependent... //then copy (inserting) to strvec vector... //copy from begin to end of source, to destination copy(istream_iterator(cin), istream_iterator(), back_inserter(strvec)); cout<<"\nstrvec data: "; for(Iter = strvec.begin(); Iter != strvec.end(); Iter++) cout<<*Iter<<" "; cout< (cout, "\n")); return 0; } ----------------------------------------------------------------------------------------- //reverse iterator using //rbegin() and rend() #include #include #include using namespace std; int main() { vector vec; //insert elements from 1 to 10 for(int i=1; i<=10; ++i) vec.push_back(i); //print all element in reverse order copy(vec.rbegin(), vec.rend(), ostream_iterator (cout," ")); cout< #include #include using namespace std; int main() { //ostream_iterator for stream cout ostream_iterator intOut(cout, "\n"); *intOut = 12; intOut++; *intOut = 33; intOut++; int i; vector vec; for(i = 10; i<=15; ++i) vec.push_back(i); cout<<"Operation: with and without delimiter...\n"; //Write elements to standard output stream cout<<"Elements output without delimiter: "; copy(vec.begin(), vec.end(), ostream_iterator (cout)); cout< (cout, " ")); cout< #include #include #include #include #include using namespace std; int main() { list lst; list ::iterator lstIter; //insert elements from 1 to 10 into the lst list for(int i=1; i<=10; ++i) lst.push_back(i); cout<<"Operation: lst.push_back(i)\n"; cout<<"lst data: "; for(lstIter = lst.begin(); lstIter != lst.end(); lstIter++) cout<<*lstIter<<' '; cout< vec; vector ::iterator Iter; //from source to destination... copy(lst.begin(), lst.end(), back_inserter(vec)); cout<<"\nOperation: copy(lst.begin(), lst.end(), back_inserter(vec))\n"; cout<<"vec data: "; for(Iter = vec.begin(); Iter != vec.end(); Iter++) cout<<*Iter<<" "; cout< deq; deque ::iterator deqIter; copy(lst.begin(), lst.end(), front_inserter(deq)); cout<<"\nOperation: copy(lst.begin(), lst.end(), front_inserter(deq))\n"; cout<<"deq data: "; for(deqIter = deq.begin(); deqIter != deq.end(); deqIter++) cout<<*deqIter<<" "; cout< st; set::iterator stIter; copy(lst.begin(), lst.end(), inserter(st, st.begin())); cout<<"\nOperation: copy(lst.begin(), lst.end(), inserter(st, st.begin()))\n"; cout<<"set data: "; for(stIter = st.begin(); stIter != st.end(); stIter++) cout<<*stIter<<" "; cout<