std::bind2nd 和 std::bind 与二维数组和结构数组
std::bind2nd and std::bind with bidimensional arrays and arrays of structs
我知道 C++ 有 lambda 表达式,std::bind1st、std::bind2nd 和 std::bind 已弃用。
但是,从C++的基础开始,我们可以更好地理解新特性。
所以,我从这个非常简单的代码开始,使用 ints 数组:
第一个例子: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' 执行相同的操作。
谁能帮帮我?
谢谢
bind1st
、bind2nd
及其兄弟在 C++11 中已弃用,并在 C++17 中被彻底删除。以防万一你不知道这个。
使用 bind
,解决方案相当简单,您可以使用 bind
表达式可组合的事实,并且可以使用 bind
提取数据成员(placeholders
为简洁起见省略):
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});
如果您有可用的 C++11 或更新版本,使用 lambda 几乎总是更好的选择。这不仅仅是 "good default",你必须真正扭曲 bind
的情况才能成为更好的解决方案。
我知道 C++ 有 lambda 表达式,std::bind1st、std::bind2nd 和 std::bind 已弃用。
但是,从C++的基础开始,我们可以更好地理解新特性。
所以,我从这个非常简单的代码开始,使用 ints 数组:
第一个例子: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' 执行相同的操作。
谁能帮帮我?
谢谢
bind1st
、bind2nd
及其兄弟在 C++11 中已弃用,并在 C++17 中被彻底删除。以防万一你不知道这个。
使用 bind
,解决方案相当简单,您可以使用 bind
表达式可组合的事实,并且可以使用 bind
提取数据成员(placeholders
为简洁起见省略):
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});
如果您有可用的 C++11 或更新版本,使用 lambda 几乎总是更好的选择。这不仅仅是 "good default",你必须真正扭曲 bind
的情况才能成为更好的解决方案。