string transform with different input and output

Discussion in 'C++' started by edgekaos, Nov 3, 2005.

  1. edgekaos

    edgekaos Guest

    Is method 2 valid?

    Method 1:
    wstring input = L"STRING";
    wstring output = input;
    transform(output.begin(), output.end(), output.begin(), towupper);

    Method 2:
    wstring input = L"STRING";
    wstring output;
    transform(input.begin(), input.end(), output.begin(), towupper);

    I"m seeing crash under Visual Studio 2003 with method2.
    edgekaos, Nov 3, 2005
    #1
    1. Advertising

  2. edgekaos

    Guest

    edgekaos wrote:
    > Is method 2 valid?
    >
    > Method 2:
    > wstring input = L"STRING";
    > wstring output;
    > transform(input.begin(), input.end(), output.begin(), towupper);


    No, it's not. The output iterator needs to be the same length as the
    input iterator.

    Cheers,
    Andre
    , Nov 3, 2005
    #2
    1. Advertising

  3. edgekaos

    Guest

    Sorry to reply to my own post, but...

    wrote:
    > edgekaos wrote:
    > > Is method 2 valid?
    > >
    > > Method 2:
    > > wstring input = L"STRING";
    > > wstring output;
    > > transform(input.begin(), input.end(), output.begin(), towupper);

    >
    > No, it's not. The output iterator needs to be the same length as the
    > input iterator.


    OR, you can also use std::back_inserter(). Like so:

    string input = "Hello World!";
    string output;

    transform( input.begin(), input.end(), back_inserter( output ), toupper
    );

    Cheers,
    Andre
    , Nov 3, 2005
    #3
  4. edgekaos wrote:
    > Is method 2 valid?
    >
    > Method 1:
    > wstring input = L"STRING";
    > wstring output = input;
    > transform(output.begin(), output.end(), output.begin(), towupper);
    >
    > Method 2:
    > wstring input = L"STRING";
    > wstring output;
    > transform(input.begin(), input.end(), output.begin(), towupper);
    >
    > I"m seeing crash under Visual Studio 2003 with method2.
    >


    The latter method is not valid. In order to make it valid you should either
    pre-set the size of the destination string

    wstring input = L"STRING";
    wstring output(input.length(), 0);

    transform(input.begin(), input.end(), output.begin(), towupper);

    or use the 'back_insert_iterator' as the output iterator (see 'int2str's
    messages), optionally reserving the required amount of memory in advance

    wstring input = L"STRING";
    wstring output;

    output.reserve(input.length()); // <- optional

    transform(input.begin(), input.end(), back_inserter(output), towupper);

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rene van Hoek

    Xalan transform output to string

    Rene van Hoek, Nov 22, 2004, in forum: XML
    Replies:
    1
    Views:
    822
    Rene van Hoek
    Nov 23, 2004
  2. Flomo Togba Kwele
    Replies:
    2
    Views:
    355
    Flomo Togba Kwele
    May 2, 2007
  3. pradeep
    Replies:
    3
    Views:
    796
    cwdjrxyz
    Jun 7, 2007
  4. Smurff
    Replies:
    10
    Views:
    2,360
    Keith Thompson
    Nov 21, 2008
  5. Susan Cranford
    Replies:
    2
    Views:
    392
Loading...

Share This Page