Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use constructor as predicate

Tags:

c++

Can we use a constructor as predicate? So far what I manage to be able to do is this:

std::vector<const char*> v1 = {
    "Hello", "from", "GCC", __VERSION__, "!"
};
std::vector<std::string> v2(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(),
    [] (const char* s) { return std::string(s); });

But I want some way to do std::tranform( ..., std::string). I've tried std::string::string, std::string::basic_string and std::string::basic_string<char>.

like image 807
user4394074 Avatar asked Dec 05 '25 10:12

user4394074


1 Answers

I would just do:

std::vector<std::string> v2(v1.begin(), v1.end());
like image 82
Chris Drew Avatar answered Dec 06 '25 23:12

Chris Drew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!