Apache C++ Standard Library Reference Guide
bind1st(), bind2nd(), binder1st, binder2nd
Library:
General utilities
Function
Local Index
No Entries
Summary
Templatized utilities to bind values to function objects
Synopsis
#include <functional>
namespace std {
template <class Operation>
class binder1st;
template <class Operation, class T>
binder1st<Operation> bind1st(const Operation&, const T&);
template <class Operation>
class binder2nd ;
template <class Operation, class T>
binder2nd<Operation> bind2nd(const Operation&, const T&);
}
Description
Because so many functions included in the C++ Standard Library take
other functions as arguments, the library includes classes that let you
build new function objects out of old ones. Both bind1st()
and bind2nd()
are functions that take as arguments a binary function object f
and a value x,
and return, respectively, classes binder1st
and binder2nd
. The underlying function object must be a subclass of binary_function
.
Class binder1st
binds the value to the first argument of the binary function, and binder2nd
does the same thing for the second argument of the function. The
resulting classes can be used in place of a unary predicate in other
function calls.
For example, you could use the count_if()
algorithm to count all elements in a vector
that are less than or equal to 7, using the following:
vector<int> v;
int littleNums;
count_if(v.begin, v.end, bind1st(greater<int>(),7),
littleNums)
This function adds one to littleNums
each time the element is greater than 7.
Interface
namespace std {
// Class binder1st
template <class Operation>
class binder1st
: public unary_function<typename
Operation::second_argument_type,
typename Operation::result_type>
{
public:
binder1st(const Operation&,
const typename Operation::first_argument_type&);
typename Operation::result_type
operator() (const typename
Operation::second_argument_type&) const;
};
// Class binder2nd
template <class Operation>
class binder2nd
: public unary_function<typename
Operation::first_argument_type,
typename Operation::result_type>
{
public:
binder2nd(const Operation&,
const typename Operation::second_argument_type&);
typename Operation::result_type
operator()(const typename
Operation::first_argument_type&) const;
};
// Creator bind1st
template <class Operation, class T>
binder1st<Operation> bind1st(const Operation&, const T&);
// Creator bind2nd
template<class Operation, class T>
binder2nd <Operation> bind2nd(const Operation&, const T&);
}
Example
//
// binders.cpp
//
#include <algorithm> // for find_if
#include <functional> // for equal_to, bind1st, bind2nd
#include <iostream> // for cout
#include <iterator> // for ostream_iterator
#include <vector> // for vector
int main ()
{
typedef std::vector<int> Vector;
typedef std::equal_to<Vector::value_type> EqualTo;
const Vector::value_type arr [] = { 1, 2, 3, 4, 5 };
// Initialize a vector with the array elements.
const Vector v1 (arr, arr + sizeof arr / sizeof *arr);
// Value to look for.
const Vector::value_type x (3);
// Create an 'equal to 3' unary predicate by binding the value
// 3 to the EqualTo binary predicate.
const std::binder1st<EqualTo> equal_to_3 =
std::bind1st (EqualTo (), x);
// Now use this new predicate in a call to find_if.
const Vector::const_iterator it1 =
std::find_if (v1.begin (), v1.end (), equal_to_3);
// Even better, construct the new predicate on the fly.
const Vector::const_iterator it2 =
std::find_if (v1.begin (), v1.end (), std::bind1st (EqualTo (), x));
// And now the same thing using bind2nd.
// Same result since EqualTo is commutative.
const Vector::const_iterator it3 =
std::find_if (v1.begin (), v1.end (), std::bind2nd (EqualTo (), x));
// Use the same predicate to count the number of elements
// equal to 3.
const Vector::size_type n =
std::count_if (v1.begin (), v1.end (), std::bind2nd (EqualTo (), x));
// Output results.
std::ostream_iterator<Vector::value_type> out (std::cout, " ");
std::cout << "The vector { ";
std::copy (v1.begin (), v1.end (), out);
std::cout << "} contains " << n << " element equal to "
<< x << " at offset " <<
it1 - v1.begin () << ".\n";
// Exit with status of 0 on success, 1 on failure.
const bool success = 1 == n && it1 == it2 && it1 == it2 && *it1 == x;
return success ? 0 : 1;
}
Program Output:
The vector { 1 2 3 4 5 } contains 1 element equal to 3 at offset 2.
See Also
Function Objects
Standards Conformance
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 20.3.6
分享到:
相关推荐
bind bind bind bind
bind9官方源码,未改动。 bind9官方源码,未改动。 bind9官方源码,未改动。 bind9官方源码,未改动。
BIND9中文手册 BIND9中文手册 BIND9中文手册 BIND9中文手册 BIND9中文手册
赠送jar包:jakarta.xml.bind-api-2.3.3.jar; 赠送原API文档:jakarta.xml.bind-api-2.3.3-javadoc.jar; 赠送源代码:jakarta.xml.bind-api-2.3.3-sources.jar; 赠送Maven依赖信息文件:jakarta.xml.bind-api-...
bind-9.3.6-4.P1.el5.i386.rpm bind-chroot-9.3.6-4.P1.el5.i386.rpm bind-devel-9.3.6-4.P1.SEL5_4.2.i386.rpm bind-libs-9.3.6-4.P1.el5.i386.rpm bind-utils-9.3.6-4.P1.el5_5.3.i386.rpm caching-nameserver-7.3...
赠送jar包:jakarta.xml.bind-api-2.3.3.jar; 赠送原API文档:jakarta.xml.bind-api-2.3.3-javadoc.jar; 赠送源代码:jakarta.xml.bind-api-2.3.3-sources.jar; 赠送Maven依赖信息文件:jakarta.xml.bind-api-...
DNS服务器搭建的bind安装包,该安装包为源码压缩包,适用于linux操作系统 DNS服务器搭建的bind安装包,该安装包为源码压缩包,适用于linux操作系统DNS服务器搭建的bind安装包,该安装包为源码压缩包,适用于linux...
bind-9.10.4-P1.tar.gz
bind-9.6.0-P1.tar.gzbind-9.6.0-P1.tar.gzbind-9.6.0-P1.tar.gz
bind-libs-9.8.2-0.10.rc1.el6.i686.rpm是centos工具包。
linux补丁包bind-9.3.6-20.P1.el5_8.6.x86_64.rpm
bind-9.3.6-20一套RPM格式安装包和安装配置说明档,包含必须的:bind-9.3.6-4.P1.el5.i386,bind-chroot-9.3.6-4.P1.el5.i386,caching-nameserver-9.3.6-20.P1.el5_8.6.i386,bind-devel-9.7.3-8.P3.el6.i686,bind-...
BIND9 IPV6操作指南 BIND9 IPV6操作指南
ES5的bind手写实现
BIND9配置手册 BIND9.9管理员手册 DNS配置手册
#tar xfz bind-9.3.1rc1.tar.gz 进如刚解压出来的目录 # cd bind-9.3.1rc1 编译配置 #./configure --prefix=/usr/local/named --enable-threads #--enable-threads开启多线程处理能力 #make #make install
linux RedHat5-4 DNS bind-chroot-9.3.6-4.P1.el5.i386.rpm DNs服务器配置安装包
javax.xml.bind.jar
bind-utils-9.8.2-0.10.rc1.el6.i686.rpm是centos工具包。