2003

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                           FILE: august.cpp */
/*                                                                            */
/*  This is interesting, but will probably not become popular                 */
/*  =========================================================                 */
/*                                                                            */
/*  Compiled and tested with Visual C++ 6.0                                   */
/*                                                                            */
/*  V1.00   31-AUG-2003   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/******************************************************************************/

#include <iostream>
#include <string>

using namespace std ;

template< typename T >
ostream &operator<<( ostream &os, const basic_string<T> &str )
{
   for( basic_string<T>::const_iterator it = str.begin(); it != str.end(); ++it )
      cout << (char) *it ;

   return os ;
}

int main()
{
  double values[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', ' ', '!' } ; 

  basic_string<double> dblstring( values, values + 13 ) ;

  cout << dblstring << endl ;

  return 0 ;
}

Update 31. August 2023

The compiler did not recognize basic_string<T>::const_iterator as a type and suggested to put typename in front of it.

Instead of adding typename I replaced the declaration with auto.

It is also possible to replace the loop with a for each loop, but I wanted the code to be as similar to the original as possible.

/******************************************************************************/
/*                                                                            */
/*                                                           FILE: august.cpp */
/*                                                                            */
/*  This is interesting, but will probably not become popular                 */
/*  =========================================================                 */
/*                                                                            */
/*  Compiled and tested with Visual C++ 6.0                                   */
/*                                                                            */
/*  V1.00   31-AUG-2003   P. Tellenbach   https://www.heimetli.ch             */
/*                                                                            */
/*  Compiled and tested with g++ version 11.2.0                               */
/*                                                                            */
/*  V1.10   31-AUG-2023   P. Tellenbach   https://www.heimetli.ch             */
/*                                                                            */
/******************************************************************************/

#include <iostream>
#include <string>

using namespace std ;

template< typename T >
ostream &operator<<( ostream &os, const basic_string<T> &str )
{
   for( auto it = str.begin(); it != str.end(); ++it )
      cout << (char) *it ;

   return os ;
}

int main()
{
  double values[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', ' ', '!' } ; 

  basic_string<double> dblstring( values, values + 13 ) ;

  cout << dblstring << endl ;

  return 0 ;
}