Pages
Home
Projects
Education
E-learning
Enterprise Solution
Careers
Contact
About Us
Friday, December 30, 2011
iterator design pattern example
/******************************************************************* * File : iterator-pattern.cpp * Author : Saurabh Gupta * Description : iterator design pattern example c++ * Date : PM 07:09 30 December 2011 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : ******************************************************************/ #include <iostream> #include <cstring> using namespace std; class CStringIteratorPattern { private: const char *m_cposition, *m_cend; public: CStringIteratorPattern(const char * str) : m_cposition(str), m_cend(str + strlen(str)) { } bool at_end() const { return m_cposition == m_cend; } void m_advance() { ++m_cposition; } char m_getchar() const { return *m_cposition; } }; void helloworld(CStringIteratorPattern & iterator) { for(CStringIteratorPattern i=iterator; !i.at_end(); i.m_advance()) { cout << i.m_getchar(); } } int main() { CStringIteratorPattern iterator("Hello world!\n"); helloworld(iterator); return 0; } /* * OUTPUT * [sgupta@rhel6x64 iterator]$ c++ iterator-pattern.cpp -o iterator-pattern [sgupta@rhel6x64 iterator]$ ./iterator-pattern Hello world! [sgupta@rhel6x64 iterator]$ */
See Also:
Design pattern Concept and Sample Codes
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment