Jerome Baldridge
Jerome Baldridge

Reputation: 528

with std::transform, better to use begin() or back_inserter()?

This page uses begin() while this one and many others suggest using std::back_inserter. I have been reading about this for an hour now and can't find any solid reason to use one over the other, or what the difference is. Can anyone point me in the right direction?

Upvotes: 0

Views: 528

Answers (1)

T.C.
T.C.

Reputation: 137315

Those do completely different things:

std::string a = "12345", b = "67890", c = b;

std::transform(a.begin(), a.end(), b.begin(), [](char ch) { return ch; });
// b is now "12345"

std::transform(a.begin(), a.end(), std::back_inserter(c), [](char ch) { return ch; });
// c is now "6789012345"

Upvotes: 4

Related Questions