Boost logo

Boost :

From: Raja R Harinath (harinath_at_[hidden])
Date: 2001-08-13 18:55:08


"George A. Heintzelman" <georgeh_at_[hidden]> writes:
[snip]
> Now, what I WOULD like is an accumulate-like algorithm which takes
> functors which take the first argument as a non-const reference, and
> actually modify the first object. It needs to copy on the way in and
> the way out, but not intermediate temporaries. This would avoid a bunch
> of maybe needless copying, especially for string functions:
>
> struct combiner {
> combiner(const string &separator): mSep(separator);
> string & operator()(string &a, const string &b) {
> a += separator;
> a += b;
> return a;
> }

That'd be an application for foreach.

  struct str_accum {
    string accum;
    bool first;
    str_accum(const string &sep): accum(""), mSep(sep), first(true) {}
    string &operator(const string &str)
    {
      if (first) { first = false; }
      else { accum += mSep; }
      accum += str;
    }
    operator string() { return accum; }
  }

  // can be used like
  out = foreach(foo.begin(), foo.end(), str_accum());

- Hari

-- 
Raja R Harinath ------------------------------ harinath_at_[hidden]
"When all else fails, read the instructions."      -- Cahn's Axiom
"Our policy is, when in doubt, do the right thing."   -- Roy L Ash

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk