简体   繁体   中英

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?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM