Converting wstring bytes to string...

This is a discussion on Converting wstring bytes to string... within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get the bytes from a std::wstring and put them into a std::string. Unfortunately, I'm somewhat lacking when ...

Converting wstring bytes to string...

I'm trying to get the bytes from a std::wstring and put them into a std::string. Unfortunately, I'm somewhat lacking when it comes to c-style string. I've made this code (and it works), but is there an easier (and more efficient) way?

It compiles fine. Just a small typo there: whar_t should of course be wchar_t.
Also make sure to include necessary headers:

#include <string>
#include <iterator>
#include <algorithm>

The function isn't all that complicated either. All it does is take an input range (first two parameters), an output iterator, and a function to perform the "transformation".
Then all it does is for each it in [begin, end), *out = transform(*it).

What the holy hell is that?? It looks more like Groovy code than C++. Is it some kind of C++0x syntax?

Lambda function syntax from the next version of C++. Considering that Ronix's suggestion should be one of the first things that come to mind, I suspect Elysia was just looking for an excuse to demonstrate the use of a lambda function

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

But, all in all, I'd seriously question the idea of converting from wchar_t into char without considering encodings or even the case of a wchar_t greater than CHAR_MAX.

Ahhh, that's even better. Damn, I didn't think of that.

Originally Posted by laserlight

Lambda function syntax from the next version of C++. Considering that Ronix's suggestion should be one of the first things that come to mind, I suspect Elysia was just looking for an excuse to demonstrate the use of a lambda function

Actually, I thought I'd use std::transform for this purpose instead of writing long unnecessary code that does the same thing. Why not use the standard library when you can?
But std::transform requires a function to do the conversion, or transformation, so I stuck in a lambda to do the work.

Originally Posted by whiteflags

Why is lambda so fugly in this language? Couldn't they use lambda or something?

Dunno. They don't look so ugly to me.
The reason they didn't use lambda is probably to avoid introducing a new keyword.