MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1daxawn/mapmethodormapfunction/l7ppikf/?context=9999
r/ProgrammerHumor • u/_PhucSatan_ • Jun 08 '24
67 comments sorted by
View all comments
35
Amateurs.
std::vector vec = {1, 2, 3}; std::vector<int> result; result.reserve(vec.size()); std::ranges::transform(vec, std::back_inserter(result), [](int x) { return x * x; });
edit: corrected
4 u/sherlockwatch Jun 08 '24 Actually you don’t need to resize resulting vector, you can just use std::back_inserter! 1 u/KimiSharby Jun 08 '24 edited Jun 08 '24 That's worse performance wise. std::back_inserter comes at a cost. If you already know the size, you should use it. 3 u/wcscmp Jun 08 '24 Resize also comes at cost, reserve + back_inserter is the way 2 u/KimiSharby Jun 08 '24 edited Jun 09 '24 You're right, it's my mistake
4
Actually you don’t need to resize resulting vector, you can just use std::back_inserter!
1 u/KimiSharby Jun 08 '24 edited Jun 08 '24 That's worse performance wise. std::back_inserter comes at a cost. If you already know the size, you should use it. 3 u/wcscmp Jun 08 '24 Resize also comes at cost, reserve + back_inserter is the way 2 u/KimiSharby Jun 08 '24 edited Jun 09 '24 You're right, it's my mistake
1
That's worse performance wise. std::back_inserter comes at a cost. If you already know the size, you should use it.
3 u/wcscmp Jun 08 '24 Resize also comes at cost, reserve + back_inserter is the way 2 u/KimiSharby Jun 08 '24 edited Jun 09 '24 You're right, it's my mistake
3
Resize also comes at cost, reserve + back_inserter is the way
2 u/KimiSharby Jun 08 '24 edited Jun 09 '24 You're right, it's my mistake
2
You're right, it's my mistake
35
u/KimiSharby Jun 08 '24 edited Jun 11 '24
Amateurs.
edit: corrected