c++ - std::bind2nd 和 std::bind 与二维数组和结构数组

标签 c++ arrays struct bind bind2nd

我知道 C++ 有 lambda 表达式,std::bind1st、std::bind2nd 和 std::bind 已弃用。

但是,从 C++ 的基础开始,我们可以更好地理解新特性。

所以,我从这个非常简单的代码开始,使用一个 int 数组:

第一个例子:使用std::bind2nd

int array1[] = { 10, 20, 30, 40, 50, 60, 40 };
int c1, c2, c3;

c1 = count_if(array1, array1 + 7, bind2nd(greater<int>(), 40));
c2 = count_if(array1, array1 + 7, bind2nd(less<int>(), 40));
c3 = count_if(array1, array1 + 7, bind2nd(equal_to<int>(), 40));
cout << "There are " << c1 << " elements that are greater than 40." << endl;
cout << "There are " << c2 << " elements that are lesser than 40." << endl;
cout << "There are " << c3 << " elements that are equal to 40." << endl;

第二个例子:使用std::bind

greater<int> big;
less<int> small;
equal_to<int> equal;

c1 = count_if(array1, array1 + 7, bind(big, _1, 40));
c2 = count_if(array1, array1 + 7, bind(small, _1, 40));
c3 = count_if(array1, array1 + 7, bind(equal, _1, 40));
cout << "There are " << c1 << " elements that are greater than 40." << endl;
cout << "There are " << c2 << " elements that are lesser than 40." << endl;
cout << "There are " << c3 << " elements that are equal to 40." << endl;

两种情况下的输出都是:

There are 2 elements that are greater than 40.
There are 3 elements that are lesser than 40.
There are 2 elements that are equal to 40.

我怎样才能像下面这样对双向数组做同样的事情:
(我想对第二个坐标做同样的操作)

int array2[7][2] = { { 1, 10 }, { 2, 20 }, { 3, 30 }, 
                     { 4, 40 }, { 5, 50 }, { 6, 60 }, { 4, 40 } };

还有这样的结构数组:

struct st
{
    char c;
    int i;
};

st array3[] = { { 'a', 10 }, { 'b', 20 }, { 'c', 30 }, 
                { 'd', 40 }, { 'e', 50 }, { 'f', 60 }, { 'd', 40 } };

在这种情况下,我想对结构数组中的字段“int”执行相同的操作。

谁能帮帮我?

谢谢

最佳答案

bind1stbind2nd 及其兄弟在 C++11 中已弃用,并在 C++17 中被彻底删除。以防万一你不知道这一点。

使用 bind,解决方案相当简单,您可以利用 bind 表达式是可组合的这一事实,并且您可以使用 bind 来提取数据成员(为简洁起见省略了占位符):

auto gr = count_if(array3, array3 + 7, bind(greater<>{}, bind(&st::i, _1), 40));
auto ls = count_if(array3, array3 + 7, bind(less<>{}, bind(&st::i, _1), 40));
auto eq = count_if(array3, array3 + 7, bind(equal_to<>{}, bind(&st::i, _1), 40));

使用 bind2nd 就没那么容易了。您需要声明一个具有多个 typedef 的函数对象(不能使用函数)。您可以使用 binary_function 来简化此操作:

struct my_greater : binary_function<st, int, bool>
{
    bool operator()(st const& l, int r) const {
        return greater<>{}(l.i, r);
    }
};

然后就可以调用了

auto old = count_if(array3, array3 + 7, bind2nd(my_greater{}, 40));

在 C++11 中,您可以使用 lambda:

auto XI = count_if(array3, array3 + 7, [](st const& l){ return l.i > 40});

demo of all


如果您有可用的 C++11 或更新版本,使用 lambda 几乎总是更好的选择。这不仅仅是一个“好的默认值”,您必须真正扭曲 bind 的情况才能成为更好的解决方案。

关于c++ - std::bind2nd 和 std::bind 与二维数组和结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40524697/

相关文章:

c++ - 如何通过参数在构造函数中初始化数组?

c++ - 在 C++ 中,调用 delete 运算符时会发生什么?

c - 如何在结构中使用结构

c++ - 传递用于结构定义的参数

c++ - 在 C++ 中使用多线程进行快速排序

c++ - VC++/C++ 高性能多线程GUI交易注意事项

javascript - 使用 JavaScript 在眨眼时显示不同的文本?

c - 使用数组作为结构的元素初始化结构数组

c++ - 内存中的数组存储

arrays - 如何处理 Bash 中元素包含空格的数组?