2003

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                         FILE: DECEMBER.CPP */
/*                                                                            */
/*  Did you know that the delimiter of an ostream_iterator can be modified ?  */
/*  ========================================================================  */
/*                                                                            */
/*  Compiled and tested with Visual C++ .NET                                  */
/*                                                                            */
/*  V1.00   31-DEC-2003   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/******************************************************************************/

#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std ;

int main()
{
  string msg          = "Hlowrd!" ;
  char  *ptr          = "el ol \n" ;
  char   delimiter[2] = " " ;
 
  ostream_iterator<char> oipc(cout,delimiter) ;

  for( string::iterator it = msg.begin(); it != msg.end(); ++it )
  {
     delimiter[0] = *ptr++ ;
     *oipc++      = *it ;
  }

  return 0 ;
}

Update 31. December 2023

The compiler wants a const pointer to the string literal, which makes sense.

/******************************************************************************/
/*                                                                            */
/*                                                         FILE: DECEMBER.CPP */
/*                                                                            */
/*  Did you know that the delimiter of an ostream_iterator can be modified ?  */
/*  ========================================================================  */
/*                                                                            */
/*  Compiled and tested with Visual C++ .NET                                  */
/*                                                                            */
/*  V1.00   31-DEC-2003   P. Tellenbach   https://www.heimetli.ch             */
/*                                                                            */
/*  Compiled and tested with g++ version 11.2.0                               */
/*                                                                            */
/*  V1.10   31-DEC-2023   P. Tellenbach   https://www.heimetli.ch             */
/*                                                                            */
/******************************************************************************/

#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std ;

int main()
{
  string      msg          = "Hlowrd!" ;
  const char *ptr          = "el ol \n" ;
  char        delimiter[2] = " " ;
 
  ostream_iterator<char> oipc(cout,delimiter) ;

  for( string::iterator it = msg.begin(); it != msg.end(); ++it )
  {
     delimiter[0] = *ptr++ ;
     *oipc++      = *it ;
  }

  return 0 ;
}