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,而第二個是安全的。 –
他們這樣做完全不同的事情。 –