Boost logo

Boost Users :

From: Martin Dluhoš (martin_at_[hidden])
Date: 2005-02-11 13:52:30


Hi,
just an idea. What about:

s1 << fastcat() + a + b + c;

where:

class fastcat
{
vector<std::string *> list;
size_t len = 0;

friend concat operator+ (const concat &, const std::string &s)
{
        list.push_back(&s);
        len += s.size();
}

operator std::string()
{
        std::string s;

        s.reserve (len);

        for (int I = 0; I <list.size(); i++) s+=list[i];

        return s;
}

I didn't test it.

Martin

-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of t. scott urban
Sent: Friday, February 11, 2005 6:06 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] Concatenation of many std::string

On Fri, 2005-02-11 at 11:22 +0100, Chateauneu Remi wrote:
> > On Thu, 2005-02-10 at 18:48 +0100, Chateauneu Remi wrote:
> > > std::string a = my_special_object() + b + c + d + e ;
> > >
> > std::string s1;
> > s1 << concat () + a + b + c;
> > std::string s2 = concat () + a + b + c;
> >
> > If you want to see it, let me know.
> Yes, thanks ! If you wish, I can send mine (330 lines, 11 k-bytes).
> Could we take the best of both, and maybe put it in the string algo lib ?

Attached. The way I did it is pretty short - 50 lines or so of
implementation, but like I said it relies on lifetimes of temporaries and
order of operations which I think are guaranteed, but not certain.
Also, a quick stress test doesn't show any improvement in speed over the naive
method, but that would depend greatly on the lifetime of the destination
string, size and number of strings you're concatenating, the behavior of your
implementation (e.g. mine penalizes multi-threaded std::string usage heavily),
etc.

Perhaps you have a way to solve it in a more effective manner.

In case I haven't put enough caveats on this, this is just toy code, I haven't
and wouldn't put this in production code without more testing, profiling, etc.

Regards

--
t. scott urban <scottu_at_[hidden]>

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net