functional (STL/CLR)

包括 STL/CLR 标头 <cliext/functional>,用于定义函数类模板和相关模板委托和函数。

语法

#include <functional>

要求

标头:<cliext/functional>

命名空间:cliext

声明

委托 说明
binary_delegate (STL/CLR) 双参数委托。
binary_delegate_noreturn (STL/CLR) 返回 void 的双参数委托。
unary_delegate (STL/CLR) 单参数委托。
unary_delegate_noreturn (STL/CLR) 返回 void 的单参数委托。
说明
binary_negate (STL/CLR) 函子,用于求反双参数函子。
binder1st (STL/CLR) 函子,用于将第一个参数绑定到双参数函子。
binder2nd (STL/CLR) 函子,用于将第二个参数绑定到双参数函子。
divides (STL/CLR) 相除函子。
equal_to (STL/CLR) 等于比较函子。
greater (STL/CLR) 大于比较函子。
greater_equal (STL/CLR) 大于或等于比较函子。
less (STL/CLR) 小于比较函子。
less_equal (STL/CLR) 小于或等于比较函子。
logical_and (STL/CLR) 逻辑“与”函子。
logical_not (STL/CLR) 逻辑“非”函子。
logical_or (STL/CLR) 逻辑“或”函子。
minus (STL/CLR) 相减函子。
modulus (STL/CLR) 取模函子。
multiplies (STL/CLR) 相乘函子。
negate (STL/CLR) 函子,用于返回其参数的求反。
not_equal_to (STL/CLR) 不等于比较函子。
plus (STL/CLR) 相加函子。
unary_negate (STL/CLR) 函子,用于求反单参数函子。
函数 说明
bind1st (STL/CLR) 为参数和函子生成 binder1st。
bind2nd (STL/CLR) 为参数和函子生成 binder2nd。
not1 (STL/CLR) 为函子生成 unary_negate。
not2 (STL/CLR) 为函子生成 binary_negate。

成员

binary_delegate (STL/CLR)

该泛型类描述双参数委托。 可使用它根据其参数和返回类型指定一个委托。

语法

generic<typename Arg1,
    typename Arg2,
    typename Result>
    delegate Result binary_delegate(Arg1, Arg2);

参数

Arg1
第一个参数的类型。

Arg2
第二个参数的类型。

Result
返回类型。

备注

该泛型委托描述双参数函数。

在这些函数模板中:

binary_delegate<int, int, int> Fun1;

binary_delegate<int, int, int> Fun2;

Fun1Fun2 类型是同义词,而对于:

delegate int Fun1(int, int);

delegate int Fun2(int, int);

它们的类型不同。

示例

// cliext_binary_delegate.cpp
// compile with: /clr
#include <cliext/functional>

bool key_compare(wchar_t left, wchar_t right)
    {
    return (left < right);
    }

typedef cliext::binary_delegate<wchar_t, wchar_t, bool> Mydelegate;
int main()
    {
    Mydelegate^ kcomp = gcnew Mydelegate(&key_compare);

    System::Console::WriteLine("compare(L'a', L'a') = {0}",
        kcomp(L'a', L'a'));
    System::Console::WriteLine("compare(L'a', L'b') = {0}",
        kcomp(L'a', L'b'));
    System::Console::WriteLine("compare(L'b', L'a') = {0}",
        kcomp(L'b', L'a'));
    System::Console::WriteLine();
    return (0);
    }
compare(L'a', L'a') = False
compare(L'a', L'b') = True
compare(L'b', L'a') = False

binary_delegate_noreturn (STL/CLR)

该泛型类描述返回 void 的双参数委托。 可使用它根据其参数指定一个委托。

语法

generic<typename Arg1,
    typename Arg2>
    delegate void binary_delegate(Arg1, Arg2);

参数

Arg1
第一个参数的类型。

Arg2
第二个参数的类型。

注解

该泛型委托描述返回 void 的双参数函数。

在这些函数模板中:

binary_delegate_noreturn<int, int> Fun1;

binary_delegate_noreturn<int, int> Fun2;

Fun1Fun2 类型是同义词,而对于:

delegate void Fun1(int, int);

delegate void Fun2(int, int);

它们的类型不同。

示例

// cliext_binary_delegate_noreturn.cpp
// compile with: /clr
#include <cliext/functional>

void key_compare(wchar_t left, wchar_t right)
    {
    System::Console::WriteLine("compare({0}, {1}) = {2}",
        left, right, left < right);
    }

typedef cliext::binary_delegate_noreturn<wchar_t, wchar_t> Mydelegate;
int main()
    {
    Mydelegate^ kcomp = gcnew Mydelegate(&key_compare);

    kcomp(L'a', L'a');
    kcomp(L'a', L'b');
    kcomp(L'b', L'a');
    System::Console::WriteLine();
    return (0);
    }
compare(a, a) = False
compare(a, b) = True
compare(b, a) = False

binary_negate (STL/CLR)

该模板类描述一个函子,调用该函子时,它返回其存储的双参数函子的逻辑“非”。 使用它根据其存储的函子指定一个函数对象。

语法

template<typename Fun>
    ref class binary_negate
    { // wrap operator()
public:
    typedef Fun stored_function_type;
    typedef typename Fun::first_argument_type first_argument_type;
    typedef typename Fun::second_argument_type second_argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    explicit binary_negate(Fun% functor);
    binary_negate(binary_negate<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Fun
存储的函子的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
stored_function_type 函子的类型。
成员 说明
binary_negate 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^() 将函子强制转换为委托。

注解

该模板类描述一个双参数函子,后者存储了另一个双参数函子。 它定义了成员运算符 operator(),以便当对象作为函数调用时,它返回使用两个参数调用的存储函子的逻辑“非”。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_binary_negate.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(4);
    c2.push_back(4);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 4 4"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::less<int> less_op;

    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(),
        cliext::binary_negate<cliext::less<int> >(less_op));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display with function
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::not2(less_op));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
4 4
1 0
1 0

bind1st (STL/CLR)

为参数和函子生成 binder1st

语法

template<typename Fun,
    typename Arg>
    binder1st<Fun> bind1st(Fun% functor,
        Arg left);

模板参数

Arg
自变量类型。

Fun
函子的类型。

函数参数

functor
要包装的函子。

left
要包装的第一个参数。

备注

该函数模板返回 binder1st<Fun>(functor, left)。 可以使用它作为便捷方式,将双参数函子及其第一个参数包装在一个使用第二个参数调用它的单参数函子中的。

示例

// cliext_bind1st.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c3(2, 0);

// display initial contents " 4 3"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::minus<int> sub_op;
    cliext::binder1st<cliext::minus<int> > subfrom3(sub_op, 3);

    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        subfrom3);
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display with function
    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        bind1st(sub_op, 3));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
-1 0
-1 0

bind2nd (STL/CLR)

为参数和函子生成 binder2nd

语法

template<typename Fun,
    typename Arg>
    binder2nd<Fun> bind2nd(Fun% functor,
        Arg right);

模板参数

Arg
自变量类型。

Fun
函子的类型。

函数参数

functor
要包装的函子。

right
要包装的第二个参数。

注解

该函数模板返回 binder2nd<Fun>(functor, right)。 可以使用它作为便捷方式,将双参数函子及其第二个参数包装在一个使用第一个参数调用它的单参数函子中。

示例

// cliext_bind2nd.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c3(2, 0);

// display initial contents " 4 3"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::minus<int> sub_op;
    cliext::binder2nd<cliext::minus<int> > sub4(sub_op, 4);

    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        sub4);
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display with function
    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        bind2nd(sub_op, 4));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
0 -1
0 -1

binder1st (STL/CLR)

该模板类描述一个单参数函子,当调用它时,返回其存储的双参数函子,调用时使用其存储的第一个参数和提供的第二个参数。 使用它根据其存储的函子指定一个函数对象。

语法

template<typename Fun>
    ref class binder1st
    { // wrap operator()
public:
    typedef Fun stored_function_type;
    typedef typename Fun::first_argument_type first_argument_type;
    typedef typename Fun::second_argument_type second_argument_type;
    typedef typename Fun:result_type result_type;
    typedef Microsoft::VisualC::StlClr::UnaryDelegate<
        second_argument_type, result_type>
        delegate_type;

    binder1st(Fun% functor, first_argument_type left);
    binder1st(binder1st<Arg>% right);

    result_type operator()(second_argument_type right);
    operator delegate_type^();
    };

参数

Fun
存储的函子的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
stored_function_type 函子的类型。
成员 说明
binder1st 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^() 将函子强制转换为委托。

备注

该模板类描述一个单参数函子,后者存储了一个双参数函子和第一个参数。 它定义了成员运算符 operator(),以便当对象作为函数调用时,它返回使用存储的第一个参数和提供的第二个参数调用存储的函子的结果。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_binder1st.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c3(2, 0);

// display initial contents " 4 3"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::minus<int> sub_op;
    cliext::binder1st<cliext::minus<int> > subfrom3(sub_op, 3);

    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        subfrom3);
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display with function
    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        bind1st(sub_op, 3));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
-1 0
-1 0

binder2nd (STL/CLR)

该模板类描述一个单参数函子,当调用它时,返回其存储的双参数函子,调用时使用提供的第一个参数和存储的第二个参数。 使用它根据其存储的函子指定一个函数对象。

语法

template<typename Fun>
    ref class binder2nd
    { // wrap operator()
public:
    typedef Fun stored_function_type;
    typedef typename Fun::first_argument_type first_argument_type;
    typedef typename Fun::second_argument_type second_argument_type;
    typedef typename Fun:result_type result_type;
    typedef Microsoft::VisualC::StlClr::UnaryDelegate<
        first_argument_type, result_type>
        delegate_type;

    binder2nd(Fun% functor, second_argument_type left);
    binder2nd(binder2nd<Arg>% right);

    result_type operator()(first_argument_type right);
    operator delegate_type^();
    };

参数

Fun
存储的函子的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
stored_function_type 函子的类型。
成员 说明
binder2nd 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^() 将函子强制转换为委托。

注解

该模板类描述一个单参数函子,后者存储了一个双参数函子和第二个参数。 它定义了成员运算符 operator(),以便当对象作为函数调用时,它返回使用提供的第一个参数和存储的第二个参数调用存储的函子的结果。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_binder2nd.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c3(2, 0);

// display initial contents " 4 3"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::minus<int> sub_op;
    cliext::binder2nd<cliext::minus<int> > sub4(sub_op, 4);

    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        sub4);
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display with function
    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        bind2nd(sub_op, 4));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
0 -1
0 -1

divides (STL/CLR)

该模板类描述一个函子,调用该函子时,将返回第一个参数除以第二个参数。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class divides
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef Arg result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    divides();
    divides(divides<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型和返回值。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
divides 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^() 将函子强制转换为委托。

注解

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便当对象作为函数调用时,它返回第一个参数除以第二个参数。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_divides.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(2);
    c2.push_back(1);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 2 1"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::divides<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
2 1
2 3

equal_to (STL/CLR)

该模板类描述一个函子,当调用该函子时,仅当第一个参数等于第二个参数时才返回 true。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class equal_to
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    equal_to();
    equal_to(equal_to<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
equal_to 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^() 将函子强制转换为委托。

备注

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便在将对象作为函数调用时,仅当第一个参数等于第二个参数时它才返回 true。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_equal_to.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(4);
    c2.push_back(4);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 4 4"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::equal_to<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
4 4
1 0

greater (STL/CLR)

该模板类描述一个函子,当调用该函子时,仅当第一个参数大于第二个参数时才返回 true。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class greater
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    greater();
    greater(greater<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
greater 构造函子。
操作员 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便在将对象作为函数调用时,仅当第一个参数大于第二个参数时它才返回 true。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_greater.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(3);
    c2.push_back(3);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 3 3"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::greater<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
3 3
1 0

greater_equal (STL/CLR)

该模板类描述一个函子,当调用该函子时,仅当第一个参数大于或等于第二个参数时才返回 true。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class greater_equal
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    greater_equal();
    greater_equal(greater_equal<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
greater_equal 构造函子。
操作员 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便在将对象作为函数调用时,仅当第一个参数大于或等于第二个参数时它才返回 true。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_greater_equal.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(4);
    c2.push_back(4);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 4 4"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::greater_equal<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
4 4
1 0

less (STL/CLR)

该模板类描述一个函子,当调用该函子时,仅当第一个参数小于第二个参数时才返回 true。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class less
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    less();
    less(less<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
less 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便在将对象作为函数调用时,仅当第一个参数小于第二个参数时它才返回 true。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_less.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(4);
    c2.push_back(4);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 4 4"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::less<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
4 4
0 1

less_equal (STL/CLR)

该模板类描述一个函子,当调用该函子时,仅当第一个参数小于或等于第二个参数时才返回 true。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class less_equal
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    less_equal();
    less_equal(less_equal<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
less_equal 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便在将对象作为函数调用时,仅当第一个参数小于或等于第二个参数时它才返回 true。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_less_equal.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(3);
    c2.push_back(3);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 3 3"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::less_equal<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
3 3
0 1

logical_and (STL/CLR)

该模板类描述一个函子,调用该函子时,只有当第一个参数和第二个测试都为 true 时才返回 true。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class logical_and
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    logical_and();
    logical_and(logical_and<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
logical_and 构造函子。
操作员 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便在将对象作为函数调用时,仅当第一个参数和第二个测试都为 true 时才返回 true。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_logical_and.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(2);
    c1.push_back(0);
    Myvector c2;
    c2.push_back(3);
    c2.push_back(0);
    Myvector c3(2, 0);

// display initial contents " 1 0" and " 1 0"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::logical_and<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
2 0
3 0
1 0

logical_not (STL/CLR)

该模板类描述一个函子,调用该函子时,只有当它的任何一个参数测试为 false 时才返回 true。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class logical_not
    { // wrap operator()
public:
    typedef Arg argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::UnaryDelegate<
        argument_type, result_type>
        delegate_type;

    logical_not();
    logical_not(logical_not<Arg> %right);

    result_type operator()(argument_type left);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
argument_type 函子参数的类型。
delegate_type 泛型委托的类型。
result_type 函子结果的类型。
成员 说明
logical_not 构造函子。
操作员 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个单参数函子。 它定义了成员运算符 operator(),以便在将对象作为函数调用时,仅当其参数测试为 false 时才返回 true。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_logical_not.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(0);
    Myvector c3(2, 0);

// display initial contents " 4 0"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c3.begin(), cliext::logical_not<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 0
0 1

logical_or (STL/CLR)

该模板类描述一个函子,调用该函子时,只有当第一个参数或第二个参数测试为 true 时才返回 true。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class logical_or
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    logical_or();
    logical_or(logical_or<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
logical_or 构造函子。
操作员 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

注解

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便当对象作为函数调用时,仅当第一个参数或第二个参数测试为 true 时它才返回 true。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_logical_or.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(2);
    c1.push_back(0);
    Myvector c2;
    c2.push_back(0);
    c2.push_back(0);
    Myvector c3(2, 0);

// display initial contents " 2 0" and " 0 0"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::logical_or<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
2 0
0 0
1 0

minus (STL/CLR)

该模板类描述一个函子,调用该函子时,将返回第一个参数减去第二个参数。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class minus
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef Arg result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    minus();
    minus(minus<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型和返回值。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
minus 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便当对象作为函数调用时,它返回第一个参数减去第二个参数。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_minus.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(2);
    c2.push_back(1);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 2 1"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::minus<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
2 1
2 2

modulus (STL/CLR)

该模板类描述一个函子,调用该函子时,将返回第一个参数除以第二个参数的余数。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class modulus
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef Arg result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    modulus();
    modulus(modulus<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型和返回值。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
modulus 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

注解

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便当对象作为函数调用时,它返回第一个参数除以第二个参数的余数。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_modulus.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(2);
    Myvector c2;
    c2.push_back(3);
    c2.push_back(1);
    Myvector c3(2, 0);

// display initial contents " 4 2" and " 3 1"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::modulus<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 2
3 1
1 0

multiplies (STL/CLR)

该模板类描述一个函子,调用该函子时,将返回第一个参数乘以第二个参数。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class multiplies
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef Arg result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    multiplies();
    multiplies(multiplies<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型和返回值。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
multiplies 构造函子。
操作员 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便当对象作为函数调用时,它返回第一个参数乘以第二个参数。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_multiplies.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(2);
    c2.push_back(1);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 2 1"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::multiplies<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
2 1
8 3

negate (STL/CLR)

该模板类描述一个函子,调用该函子时,返回参数的求反。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class negate
    { // wrap operator()
public:
    typedef Arg argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::UnaryDelegate<
        argument_type, result_type>
        delegate_type;

    negate();
    negate(negate<Arg>% right);

    result_type operator()(argument_type left);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
argument_type 函子参数的类型。
delegate_type 泛型委托的类型。
result_type 函子结果的类型。
成员 说明
negate 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个单参数函子。 它定义了成员运算符 operator(),以便当对象作为函数被调用时,它返回参数的求反。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_negate.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(-3);
    Myvector c3(2, 0);

// display initial contents " 4 -3"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c3.begin(), cliext::negate<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 -3
-4 3

not_equal_to (STL/CLR)

该模板类描述一个函子,当调用该函子时,仅当第一个参数不等于第二个参数时才返回 true。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class not_equal_to
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    not_equal_to();
    not_equal_to(not_equal_to<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
not_equal_to 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),使得在将对象作为函数调用时,仅当第一个参数不等于第二个参数时它才返回 true。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_not_equal_to.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(4);
    c2.push_back(4);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 4 4"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::not_equal_to<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
4 4
0 1

not1 (STL/CLR)

为函子生成 unary_negate

语法

template<typename Fun>
    unary_negate<Fun> not1(Fun% functor);

模板参数

Fun
函子的类型。

函数参数

functor
要包装的函子。

备注

函数模板返回 unary_negate<Fun>(functor)。 可以使用它作为便捷方式,将单参数函子包装在传递其逻辑“非”的函子中。

示例

// cliext_not1.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(0);
    Myvector c3(2, 0);

// display initial contents " 4 0"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::logical_not<int> not_op;

    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        cliext::unary_negate<cliext::logical_not<int> >(not_op));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display with function
    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        cliext::not1(not_op));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 0
1 0
1 0

not2 (STL/CLR)

为函子生成 binary_negate

语法

template<typename Fun>
    binary_negate<Fun> not2(Fun% functor);

模板参数

Fun
函子的类型。

函数参数

functor
要包装的函子。

备注

函数模板返回 binary_negate<Fun>(functor)。 可以使用它作为便捷方式,将双参数函子包装在传递其逻辑“非”的函子中。

示例

// cliext_not2.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(4);
    c2.push_back(4);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 4 4"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::less<int> less_op;

    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(),
        cliext::binary_negate<cliext::less<int> >(less_op));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display with function
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::not2(less_op));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
4 4
1 0
1 0

plus (STL/CLR)

该模板类描述一个函子,调用该函子时,将返回第一个参数加上第二个参数。 使用它根据其参数类型指定一个函数对象。

语法

template<typename Arg>
    ref class plus
    { // wrap operator()
public:
    typedef Arg first_argument_type;
    typedef Arg second_argument_type;
    typedef Arg result_type;
    typedef Microsoft::VisualC::StlClr::BinaryDelegate<
        first_argument_type, second_argument_type, result_type>
        delegate_type;

    plus();
    plus(plus<Arg>% right);

    result_type operator()(first_argument_type left,
        second_argument_type right);
    operator delegate_type^();
    };

参数

Arg
参数的类型和返回值。

成员函数

类型定义 说明
delegate_type 泛型委托的类型。
first_argument_type 函子第一个参数的类型。
result_type 函子结果的类型。
second_argument_type 函子第二个参数的类型。
成员 说明
plus 构造函子。
运算符 说明
operator() 计算所需的函数。
operator delegate_type^ 将函子强制转换为委托。

注解

该模板类描述一个双参数函子。 它定义了成员运算符 operator(),以便当对象作为函数调用时,它返回第一个参数加上第二个参数。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_plus.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(3);
    Myvector c2;
    c2.push_back(2);
    c2.push_back(1);
    Myvector c3(2, 0);

// display initial contents " 4 3" and " 2 1"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

    for each (int elem in c2)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::transform(c1.begin(), c1.begin() + 2,
        c2.begin(), c3.begin(), cliext::plus<int>());
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 3
2 1
6 4

unary_delegate (STL/CLR)

该泛型类描述单参数委托。 可使用它根据其参数和返回类型指定一个委托。

语法

generic<typename Arg,
    typename Result>
    delegate Result unary_delegate(Arg);

参数

Arg
自变量类型。

Result
返回类型。

备注

该泛型委托描述单参数函数。

在这些函数模板中:

unary_delegate<int, int> Fun1;

unary_delegate<int, int> Fun2;

Fun1Fun2 类型是同义词,而对于:

delegate int Fun1(int);

delegate int Fun2(int);

它们的类型不同。

示例

// cliext_unary_delegate.cpp
// compile with: /clr
#include <cliext/functional>

int hash_val(wchar_t val)
    {
    return ((val * 17 + 31) % 67);
    }

typedef cliext::unary_delegate<wchar_t, int> Mydelegate;
int main()
    {
    Mydelegate^ myhash = gcnew Mydelegate(&hash_val);

    System::Console::WriteLine("hash(L'a') = {0}", myhash(L'a'));
    System::Console::WriteLine("hash(L'b') = {0}", myhash(L'b'));
    return (0);
    }
hash(L'a') = 5
hash(L'b') = 22

unary_delegate_noreturn (STL/CLR)

该泛型类描述返回 void 的单参数委托。 可使用它根据其参数类型指定一个委托。

语法

generic<typename Arg>
    delegate void unary_delegate_noreturn(Arg);

参数

Arg
自变量类型。

备注

该泛型委托描述返回 void 的单参数函数。

在这些函数模板中:

unary_delegate_noreturn<int> Fun1;

unary_delegate_noreturn<int> Fun2;

Fun1Fun2 类型是同义词,而对于:

delegate void Fun1(int);

delegate void Fun2(int);

它们的类型不同。

示例

// cliext_unary_delegate_noreturn.cpp
// compile with: /clr
#include <cliext/functional>

void hash_val(wchar_t val)
    {
    System::Console::WriteLine("hash({0}) = {1}",
       val, (val * 17 + 31) % 67);
    }

typedef cliext::unary_delegate_noreturn<wchar_t> Mydelegate;
int main()
    {
    Mydelegate^ myhash = gcnew Mydelegate(&hash_val);

    myhash(L'a');
    myhash(L'b');
    return (0);
    }
hash(a) = 5
hash(b) = 22

unary_negate (STL/CLR)

该模板类描述一个函子,调用该函子时,它返回其存储的单参数函子的逻辑“非”。 使用它根据其存储的函子指定一个函数对象。

语法

template<typename Fun>
    ref class unary_negate
    { // wrap operator()
public:
    typedef Fun stored_function_type;
    typedef typename Fun::argument_type argument_type;
    typedef bool result_type;
    typedef Microsoft::VisualC::StlClr::UnaryDelegate<
        argument_type, result_type>
        delegate_type;

    unary_negate(Fun% functor);
    unary_negate(unary_negate<Fun>% right);

    result_type operator()(argument_type left);
    operator delegate_type^();
    };

参数

Fun
存储的函子的类型。

成员函数

类型定义 说明
argument_type 函子参数的类型。
delegate_type 泛型委托的类型。
result_type 函子结果的类型。
成员 说明
unary_negate 构造函子。
操作员 说明
operator() 计算所需的函数。
delegate_type^ 将函子强制转换为委托。

备注

该模板类描述一个单参数函子,后者存储了另一个单参数函子。 它定义了成员运算符 operator(),以便当对象作为函数调用时,它返回使用参数调用的存储函子的逻辑“非”。

还可以将对象作为类型为 delegate_type^ 的函数参数传递,它将相应地转换。

示例

// cliext_unary_negate.cpp
// compile with: /clr
#include <cliext/algorithm>
#include <cliext/functional>
#include <cliext/vector>

typedef cliext::vector<int> Myvector;
int main()
    {
    Myvector c1;
    c1.push_back(4);
    c1.push_back(0);
    Myvector c3(2, 0);

// display initial contents " 4 0"
    for each (int elem in c1)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display
    cliext::logical_not<int> not_op;

    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        cliext::unary_negate<cliext::logical_not<int> >(not_op));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();

// transform and display with function
    cliext::transform(c1.begin(), c1.begin() + 2, c3.begin(),
        cliext::not1(not_op));
    for each (int elem in c3)
        System::Console::Write(" {0}", elem);
    System::Console::WriteLine();
    return (0);
    }
4 0
1 0
1 0