/******************************************************************************/ /* */ /* FILE: january.cpp */ /* */ /* From Hello.begin() to World.end() with the STL */ /* ============================================== */ /* */ /* Compiled and tested with Visual C++ V6.0 */ /* */ /* V1.00 17-JAN-2001 P. Tellenbach http://www.heimetli.ch/ */ /* */ /******************************************************************************/ #include <iostream> #include <algorithm> class oi : public std::ostream_iterator<char> { public: oi( ostream_type &os ) : std::ostream_iterator<char>( os ) { } template< typename V > int operator ()( V value ) { return !value ; } } ; int main( ) { std::string Hello = "Hello World !\n" ; std::string &World = Hello ; return std::copy( Hello.begin(), World.end(), oi(std::cout) )( 42 ) ; }
Update 12. January 2021
The compiler complained about missing templates. Including the iterator-Header fixed that.
No other changes were necessary.
/******************************************************************************/ /* */ /* FILE: january.cpp */ /* */ /* From Hello.begin() to World.end() with the STL */ /* ============================================== */ /* */ /* Compiled and tested with Visual C++ V6.0 */ /* */ /* V1.00 17-JAN-2001 P. Tellenbach https://www.heimetli.ch/ */ /* */ /* Compiled and tested with g++ V8.3.0 */ /* */ /* V2.00 12-JAN-2021 P. Tellenbach https://www.heimetli.ch/ */ /* */ /******************************************************************************/ #include <iostream> #include <algorithm> #include <iterator> class oi : public std::ostream_iterator<char> { public: oi( ostream_type &os ) : std::ostream_iterator<char>( os ) { } template< typename V > int operator ()( V value ) { return !value ; } } ; int main( ) { std::string Hello = "Hello World !\n" ; std::string &World = Hello ; return std::copy( Hello.begin(), World.end(), oi(std::cout) )( 42 ) ; }