// Iterators.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; class ForwardString:public string { public: ForwardString(_TCHAR *pString):string(pString) { } }; class ReverseString:public string { public: ReverseString(_TCHAR *pString):string(pString) { } bool traverse() { for (reverse_iterator i=this->rbegin();i!=this->rend();++i) if (this->processItem(*i)) return true; return false; } protected: virtual bool processItem(_TCHAR c) { cout << c; return false; } }; static void testForward1() { string message(_T("Hello, world!")); // Without the cast, the following error message is produced: // error C2664: 'std::copy': cannot convert parameter 3 from 'std::ostream' to 'std::ostream' copy(message.begin(),message.end(),(ostream_iterator<_TCHAR>) cout); cout << endl; } static void testForward2() { string message(_T("Hello, world!")); // copy utility amounts to approximately the following code string::iterator first=message.begin(); string::iterator last=message.end(); ostream_iterator<_TCHAR> dest=cout; for (;first!=last;++dest,++first) *dest=*first; cout << endl; } static void testForward3() { string message(_T("Hello, world!")); // This is an example of an external iterator for (string::iterator i=message.begin();i!=message.end();++i) cout << *i; cout << endl; } static void testForward4() { ForwardString message(_T("Hello, world!")); for (string::iterator i=message.begin();i!=message.end();++i) cout << *i; cout << endl; } static void testReverse1() { string message(_T("Hello, world!")); // This is an example of an external iterator for (string::reverse_iterator i=message.rbegin();i!=message.rend();++i) cout << *i; cout << endl; } static void testReverse2() { ReverseString message(_T("Hello, world!")); message.traverse(); cout << endl; } class SinglePalindromeTest { protected: string::reverse_iterator reverseIterator; public: SinglePalindromeTest(string &aString):reverseIterator(aString.rbegin()) { } bool operator()(_TCHAR c) { bool mismatch=(*reverseIterator!=c); ++reverseIterator; return mismatch; } }; static bool testPalindrome1() { string message("poordanisinadroop"); return find_if(message.begin(),message.end(),SinglePalindromeTest(message))==message.end(); } static bool testPalindrome2() { bool isPalindrome=true; string message("drawalevelaward"); string::iterator forwardIterator(message.begin()); string::reverse_iterator reverseIterator(message.rbegin()); while (forwardIterator!=message.end() && reverseIterator!=message.rend()) { if (*forwardIterator!=*reverseIterator) isPalindrome=false; ++forwardIterator; ++reverseIterator; } return isPalindrome; } template bool isPalindrome(Container &container) { Container::iterator forwardIterator(container.begin()); Container::reverse_iterator reverseIterator(container.rbegin()); while (forwardIterator!=container.end() && reverseIterator!=container.rend()) { if (*forwardIterator!=*reverseIterator) return false; ++forwardIterator; ++reverseIterator; } return true; } static bool testPalindrome3() { return isPalindrome(string("madamimadam")); } static void testReverse() { testReverse1(); testReverse2(); } static void testForward() { testForward1(); testForward2(); testForward3(); testForward4(); } static void testPalindrome() { cout << (testPalindrome1() ? "true" : "false") << endl; cout << (testPalindrome2() ? "true" : "false") << endl; cout << (testPalindrome3() ? "true" : "false") << endl; } int _tmain(int argc,_TCHAR *argv[]) { testForward(); testReverse(); testPalindrome(); return 0; }