简体   繁体   English

我应该在使用std :: back_inserter时保留内存

[英]Should I reserve memory when using std::back_inserter

Do I need to reserve memory when using a back:inserter? 使用背部时是否需要保留内存:插入器?

d.reserve(s.size())
std::copy (s.begin(),s.end(),back_inserter(d));

You do not need to reserve memory for the container when using std::back_inserter . 使用std::back_inserter时,不需要为容器保留内存。 However, if you know beforehand about the number of elements you are going to insert and want to prevent repeated allocations you may reserve memory. 但是,如果您事先知道要插入的元素数量并希望防止重复分配,则可以保留内存。

You don't have to. 你不必。 However, if this code snippet is run very frequently, you may consider reserving memory to improve performance. 但是,如果此代码段运行频繁,您可以考虑保留内存以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将`std :: copy()`与`std :: back_inserter()`一起使用 - Using `std::copy()` with `std::back_inserter()` std::back_inserter 用于 std::set? - std::back_inserter for a std::set? std :: back_inserter比std :: inserter有什么好处? - What's the benefit of std::back_inserter over std::inserter? 我可以一直使用std :: inserter(container,container.end())而不是std :: back_inserter(容器)吗? - Can I always use std::inserter(container, container.end()) instead of std::back_inserter(container)? 使用back_inserter()或inserter()提高std :: copy()的效率 - Improving efficiency of std::copy() with back_inserter() or inserter() 尝试使用 std::copy() 和 std::back_inserter 从 std::cin 读取时的不同结果 - Different results when trying to use std::copy() with std::back_inserter to read from std::cin 使用front_inserter代替back_inserter - Using front_inserter instead of back_inserter 将 std::transform 与 std::back_inserter 一起使用是否有效? - Is it valid to use std::transform with std::back_inserter? std :: transform包装器,带有指向成员函数和back_inserter的指针 - std::transform wrapper with pointer to member functionand back_inserter 如果我使用vector :: begin()而不是std :: back_inserter(vector)来输出set_intersection会发生什么? - What happens if I use vector::begin() instead of std::back_inserter(vector) for output of set_intersection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM