【发布时间】:2016-03-14 14:30:07
【问题描述】:
我有一个问题,哪种方式最好将向量转发到绑定函数?
下面是两种方法的代码。在生产代码向量中将包含大量数据,我希望尽可能避免复制它。
#include <iostream>
#include <vector>
#include <functional>
void foo(const std::vector<uint16_t>& v)
{
for(const auto& c : v)
{
std::cout << c;
}
std::cout << std::endl;
}
int main()
{
std::vector<uint16_t> vv{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
auto f1 = std::bind(&foo, vv); // 1)
auto f2 = std::bind(&foo, std::move(vv)); // 2)
f1();
f2();
}
【问题讨论】:
-
您向我们展示的代码有什么问题?您是否只想对应该使用两个
std::bind调用中的哪一个提出意见? -
是的,我想了解一下您的意见,哪种方法是绑定大量数据(例如 STL 容器)的最佳方法(从性能 POV 来看)。
标签: performance c++11 vector stl bind