c++ - 我可以使用占位符作为 C++ 中数组的索引吗?

标签 c++ vector placeholder c++03

我将展示代码块。

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/

相关文章:

c++ - 捕获无效的操作码 rip rsp

c++ - 我们可以创建一个特征矩阵 vector 吗?

c++ - 比较 vector 中元素的最有效和最快的方法?

php - Zend_Form 占位符翻译

wordpress - Woocommerce 结帐 : Add placeholder in country dropdown

c++ - 如果类具有特定的成员函数,则启用模板函数

c++ - 从视频 Opencv Haar Cascade 中获取前一帧

c# - 监控线程创建

arrays - 将字符串添加到空数组给出 `NAN`

html - &lt;textarea&gt; 中可以包含多行 HTML5 占位符文本吗?