comp.lang.ada
 help / color / mirror / Atom feed
From: Hyman Rosen <hyrosen@mail.com>
Subject: Re: Real data for a change in the assignment operators and Bounded_String discussions.
Date: Mon, 07 Jul 2003 14:52:03 -0400
Date: 2003-07-07T14:52:03-04:00	[thread overview]
Message-ID: <1057603924.174628@master.nyc.kbcfp.com> (raw)
In-Reply-To: <3F09999F.1040206@attbi.com>

Robert I. Eachus wrote:
> I originally posted a working Ada program, with output.  Would you care 
> to do the same for your C and/or C++ versions?

Sorry about that. I read your code too hastily and misinterpreted
what it was doing. Here we go -

#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
#include <iomanip>
#include <ctype.h>

template <typename In, typename Out, typename Pred>
Out copy_if(In b, In e, Out o, Pred p)
{
     for (; b != e; ++b)
         if (p(*b))
             *o++ = *b;
     return o;
}

void process(std::istream &in, std::ostream &out)
{
     typedef std::vector<char> buf_type;
     buf_type buf;
     copy_if(std::istream_iterator<char>(in),
             std::istream_iterator<char>(),
             std::back_inserter(buf),
             isdigit);
     buf_type::iterator b = buf.begin(), e = buf.end();
     unsigned long c1d[10] = { 0 }, c2d[10][10] = { 0 };
     out << "There were " << buf.size() << " digits.\n\n";
     while (b != e)
     {
         char d = *b++ - '0';
         ++c1d[d];
         if (b != e)
             ++c2d[d][*b - '0'];
     }
     for (int i = 0; i < 10; ++i)
         out << i << "=" << std::setw(3) << c1d[i] << " ";
     out << "\n\n";
     for (int j = 0; j < 10; ++j)
     {
         for (int i = 0; i < 10; ++i)
             out << j << i << "=" << std::setw(3) << c2d[j][i] << " ";
         out << "\n";
     }
}

int main(int c, char **v)
{
     std::ifstream is;
     if (c > 1) is.open(v[1]);
     std::ofstream os;
     if (c > 2) os.open(v[2]);
     process(is.is_open() ? is : std::cin,
             os.is_open() ? os : std::cout);
}




      reply	other threads:[~2003-07-07 18:52 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-04  3:41 Real data for a change in the assignment operators and Bounded_String discussions Robert I. Eachus
2003-07-05  1:29 ` Inc (was: Real data for a change in the assignment operators and Bounded_String discussions. ) Alexander Kopilovitch
2003-07-07 21:31   ` Gautier Write-only
2003-07-07 21:35     ` Inc (was: Real data for a change in the assignment operators and Larry Kilgallen
2003-07-08  3:53       ` Gautier Write-only
2003-07-08 20:50       ` Alexander Kopilovitch
2003-07-07 21:42     ` Inc (was: Real data for a change in the assignment operators and Bounded_String discussions. ) Vinzent Hoefler
2003-07-08  4:04       ` Gautier Write-only
2003-07-07  4:27 ` Real data for a change in the assignment operators and Bounded_String discussions Hyman Rosen
2003-07-07  9:27   ` Georg Bauhaus
2003-07-07  9:41     ` Georg Bauhaus
2003-07-07 14:29       ` Hyman Rosen
2003-07-07 17:29         ` Georg Bauhaus
2003-07-07 19:01           ` Hyman Rosen
2003-07-07 15:49       ` Robert I. Eachus
2003-07-07 13:46     ` Hyman Rosen
2003-07-07 16:03       ` Robert I. Eachus
2003-07-07 18:52         ` Hyman Rosen [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox