我将展示代码块。
void function(int n) {}
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
std::vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
std::for_each(v.begin(), v.end(), boost::bind(&function, arr[_1]))
有没有办法在上面的代码中使用占位符作为数组的索引?
我仅限于 gcc 4.4.7,所以我不能使用 C++11 及更高版本。
最佳答案
如果您坚持使用 boost::bind
,您可以添加 getValue
函数,该函数获取索引并返回数组值并进行绑定(bind)组合:
int getValue(int* arr, int index)
{
return arr[index];
}
std::for_each(v.begin(), v.end(),
boost::bind(&function, boost::bind(getValue, arr, boost::placeholders::_1) ));
关于c++ - 我可以使用占位符作为 C++ 中数组的索引吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57687076/