Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string transform with different input and output

Reply
Thread Tools

string transform with different input and output

 
 
edgekaos
Guest
Posts: n/a
 
      11-03-2005
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.

 
Reply With Quote
 
 
 
 
int2str@gmail.com
Guest
Posts: n/a
 
      11-03-2005

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

 
Reply With Quote
 
 
 
 
int2str@gmail.com
Guest
Posts: n/a
 
      11-03-2005
Sorry to reply to my own post, but...

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-04-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use different input & output in bidirectional lines nicx82 VHDL 0 01-27-2010 09:41 AM
Newbie help with transform - force output element not in input Flomo Togba Kwele XML 2 05-02-2007 09:15 PM
Pass input Array value to function to calc different input value Susan Cranford Javascript 2 07-05-2005 02:53 AM
blocking I/O with javax.xml.parsers.DocumentBuilder.parse() and javax.xml.transform.Transformer.transform() jazzdman@gmail.com Java 1 03-27-2005 06:56 AM
Xalan transform output to string Rene van Hoek XML 1 11-23-2004 10:47 AM



Advertisments