How can I transform source range to destination range that is thesame as source?

L

Lambda

I'd like to change all the character in a array to lower case.
I find transform() is convenient.

Because the array is large, I don't want to define another array. I
tried:

transform(buffer, buffer + size, buffer, tolower);

From the output, I find it works.
But when I compile it with Visual C++,
I find a warning that says 'Function call with parameters that may be
unsafe'.

I shouldn't use transform that way?
I must define another array to store the result or
do it without transform?
 
M

Michael DOUBEZ

Lambda a écrit :
I'd like to change all the character in a array to lower case.
I find transform() is convenient.

Because the array is large, I don't want to define another array. I
tried:

transform(buffer, buffer + size, buffer, tolower);

From the output, I find it works.
But when I compile it with Visual C++,
I find a warning that says 'Function call with parameters that may be
unsafe'.

I shouldn't use transform that way?

This is a MS feature called check iterator. The compiler warns you that
you may write out of bounds. Are you sure the size is correct ?
I must define another array to store the result or
do it without transform?

If you are sure you are right you can silence the compiler by defining:
#define _SECURE_SCL 0

Or using a Windows specific extension:
transform(buffer,buffer+size,
stdext::checked_array_iterator<char*>(buffer, size),
tolower);
 
J

James Kanze

I'd like to change all the character in a array to lower case.
I find transform() is convenient.
Because the array is large, I don't want to define another array. I
tried:
transform(buffer, buffer + size, buffer, tolower);
From the output, I find it works.

Depending on the includes, it's likely to not even compile. And
if it does, the results have undefined behavior.
But when I compile it with Visual C++,
I find a warning that says 'Function call with parameters that
may be unsafe'.

Which one?
I shouldn't use transform that way?
No.

I must define another array to store the result or
do it without transform?

No, but you do have to provide a functional object that is
unambiguous and that works correctly on the data type. You also
should be using std::vector instead of whatever; it would make
life a lot easier.

And finally, of course, you should be aware that upper to lower
case is not necessarily a one to one mapping, and that it is
very locale dependent.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top