0
This page使用begin()
而this one and many others建议使用std::back_inserter
。我现在一直在读这篇文章一个小时,并且找不到任何坚实的理由来使用它,或者有什么不同。任何人都可以将我指向正确的方向吗?与std :: transform,最好使用begin()或back_inserter()?
This page使用begin()
而this one and many others建议使用std::back_inserter
。我现在一直在读这篇文章一个小时,并且找不到任何坚实的理由来使用它,或者有什么不同。任何人都可以将我指向正确的方向吗?与std :: transform,最好使用begin()或back_inserter()?
那些做完全不同的事情:
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 "6789"
可能值得一提的是,如果b小于a,第一个很容易导致UB,而第二个是安全的。 –
他们这样做完全不同的事情。 –