setw and setfill

From: Paul F. Johnson (paul_at_all-the-johnsons.co.uk)
Date: 02/29/04


Date: Sun, 29 Feb 2004 16:24:55 GMT

Hi,

I'm trying to replace some inefficient code with some nice new code, but
have hit a problem.

Basically, I'm trying to construct a std::string so that I can have
output such as #01020344.

The code I have at the moment won't work, but it is as follows

std::string tmp;
tmp << setw(2) << setfill('0') << val1 << setw(2) << setfill('0') << val2
... etc

I can replace tmp with an overloaded ostream via (in the header)

friend ostream &overload(const ostream &o, const int *v);

and in the code

ostream classname::&overload(const ostream &o, const int *v)
{
  size_t vsize = sizeof(v) / sizeof(*v); // I think!
  for (int i = 0; i < vsize; ++i)
    o << setw(2) << setfill('0') << v[i];
  return 0;
}

but I'm unsure what I'd have to assign it to a std::string.

Am I doing this the right way and how do I assign it to a std::string?

TTFN

Paul

-- 
One OS to fool them all
One browser to find them
One email client to bring them all
And through security holes, blind them...


Relevant Pages

  • Re: Newbie looking for some direction
    ... TMP, or too many points, is a concept much like duplication. ... It gets you thinking about backgammon and it doesn't overwhelm ... You're obviously a strong player. ... your opponent's 5 point to hit. ...
    (rec.games.backgammon)
  • Re: ifstream and reading way past the EOL
    ... > I am parsing a text file, when I know that I have hit a certain section ... tellg() doesn't return an int. ... buffer.insert, tmp, tmp+amount); ...
    (microsoft.public.vc.stl)
  • Never mind
    ... fseek (tmp, 0, SEEK_SET); ... As soon as I hit the send button, ... I'll be taking a nap now. ... Prev by Date: ...
    (comp.lang.c)