当前位置:   article > 正文

C++函数对象-运算符函数对象-算术运算-实现 x * y 的函数对象(std::multiplies)_c++ multiplies

c++ multiplies

任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。

运算符函数对象

C++ 针对常用的算术和逻辑运算定义了很多函数对象:

算术运算

实现 x * y 的函数对象

std::multiplies

template< class T >
struct multiplies;

(C++14 前)

template< class T = void >
struct multiplies;

(C++14 起)

进行乘法的函数对象。等效地在二个 T 类型实例上调用 operator* 。

特化

标准库提供 std::multiplies 在不指定 T 时的特化,这使得参数类型和返回类型留待推导。

multiplies<void>

实现 x * y 并推导参数和返回类型的函数对象
(类模板特化)
(C++14 起)

成员类型

类型定义
result_type(C++17 中弃用)T
first_argument_type(C++17 中弃用)T
second_argument_type(C++17 中弃用)T
(C++20 前)

成员函数

operator()

返回二个参数的积
(公开成员函数)

std::multiplies::operator()

T operator()( const T& lhs, const T& rhs ) const;

(C++14 前)

constexpr T operator()( const T& lhs, const T& rhs ) const;

(C++14 起)

返回 lhsrhs 的积。

参数

lhs, rhs-要相乘的值

返回值

lhs * rhs 的结果。

异常

(无)

可能的实现

  1. constexpr T operator()(const T &lhs, const T &rhs) const
  2. {
  3. return lhs * rhs;
  4. }

调用示例

  1. #include <iostream>
  2. #include <functional>
  3. struct Cell
  4. {
  5. int x;
  6. int y;
  7. Cell() = default;
  8. Cell(int a, int b): x(a), y(b) {}
  9. Cell(const Cell &cell)
  10. {
  11. x = cell.x;
  12. y = cell.y;
  13. }
  14. bool operator <(const Cell &cell) const
  15. {
  16. if (x == cell.x)
  17. {
  18. return y < cell.y;
  19. }
  20. else
  21. {
  22. return x < cell.x;
  23. }
  24. }
  25. Cell &operator+(const Cell &cell)
  26. {
  27. x += cell.x;
  28. y += cell.y;
  29. return *this;
  30. }
  31. Cell &operator+=(const Cell &cell)
  32. {
  33. x += cell.x;
  34. y += cell.y;
  35. return *this;
  36. }
  37. Cell &operator*=(int n)
  38. {
  39. x *= n;
  40. y *= n;
  41. return *this;
  42. }
  43. Cell &operator++()
  44. {
  45. x += 1;
  46. y += 1;
  47. return *this;
  48. }
  49. friend Cell operator +(const Cell &cell1, const Cell &cell2)
  50. {
  51. Cell cell = cell1;
  52. cell += cell2;
  53. return cell;
  54. }
  55. friend Cell operator *(const Cell &cell1, const Cell &cell2)
  56. {
  57. Cell cell = {cell1.x * cell2.x, cell1.y * cell2.y};
  58. return cell;
  59. }
  60. };
  61. std::ostream &operator<<(std::ostream &os, const Cell &cell)
  62. {
  63. os << "{" << cell.x << "," << cell.y << "}";
  64. return os;
  65. }
  66. int main()
  67. {
  68. std::cout << "std::multiplies<char>()(50, 2): "
  69. << std::multiplies<char>()(50, 2) << std::endl;
  70. std::cout << "std::multiplies<int>()(1023, 1024): "
  71. << std::multiplies<int>()(1023, 1024) << std::endl;
  72. std::cout << "std::multiplies<long>()(1023, 1024): "
  73. << std::multiplies<long>()(1023, 1024) << std::endl;
  74. std::cout << "std::multiplies<long long>()(1023, 1024): "
  75. << std::multiplies<long long>()(1023, 1024) << std::endl;
  76. std::cout << "std::multiplies<uint8_t>()(1023, 1024): "
  77. << std::multiplies<uint8_t>()(8, 32) << std::endl;
  78. std::cout << "std::multiplies<uint16_t>()(123, 456): "
  79. << std::multiplies<uint16_t>()(123, 456) << std::endl;
  80. std::cout << "std::multiplies<uint32_t>()(101, 202): "
  81. << std::multiplies<uint32_t>()(101, 202) << std::endl;
  82. std::cout << "std::multiplies<uint64_t>()(10230, 10240): "
  83. << std::multiplies<uint64_t>()(10230, 10240) << std::endl;
  84. std::cout << "std::multiplies<int8_t>()(1023, 1024): "
  85. << std::multiplies<int8_t>()(8, 32) << std::endl;
  86. std::cout << "std::multiplies<int16_t>()(123, 456): "
  87. << std::multiplies<int16_t>()(123, 456) << std::endl;
  88. std::cout << "std::multiplies<int32_t>()(101, 202): "
  89. << std::multiplies<int32_t>()(101, 202) << std::endl;
  90. std::cout << "std::multiplies<int64_t>()(10230, 10240): "
  91. << std::multiplies<int64_t>()(10230, 10240) << std::endl;
  92. std::cout << "std::multiplies<double>()(3.14, 3.14): "
  93. << std::multiplies<double>()(3.14, 3.14) << std::endl;
  94. std::cout << "std::multiplies<float>()(3.14, 3.14): "
  95. << std::multiplies<float>()(3.14, 3.14) << std::endl;
  96. std::cout << "std::multiplies<float>()(3, 3): "
  97. << std::multiplies<float>()(3, 3) << std::endl;
  98. std::cout << "std::multiplies<int>()(3.14, 3.14): "
  99. << std::multiplies<int>()(3.34, 3.34) << std::endl;
  100. std::cout << "std::multiplies<Cell>()(Cell{101, 101}, Cell{202, 202}): "
  101. << std::multiplies<Cell>()(Cell{101, 101}, Cell{202, 202}) << std::endl;
  102. //编译失败
  103. // std::cout << "std::multiplies<std::string>()(\"I am a \", \"handsome programmer\"):"
  104. // << std::multiplies<std::string>()("I am a ", "handsome programmer") << std::endl;
  105. return 0;
  106. }

输出

  1. std::multiplies<char>()(50, 2): d
  2. std::multiplies<int>()(1023, 1024): 1047552
  3. std::multiplies<long>()(1023, 1024): 1047552
  4. std::multiplies<long long>()(1023, 1024): 1047552
  5. std::multiplies<uint8_t>()(1023, 1024):
  6. std::multiplies<uint16_t>()(123, 456): 56088
  7. std::multiplies<uint32_t>()(101, 202): 20402
  8. std::multiplies<uint64_t>()(10230, 10240): 104755200
  9. std::multiplies<int8_t>()(1023, 1024):
  10. std::multiplies<int16_t>()(123, 456): -9448
  11. std::multiplies<int32_t>()(101, 202): 20402
  12. std::multiplies<int64_t>()(10230, 10240): 104755200
  13. std::multiplies<double>()(3.14, 3.14): 9.8596
  14. std::multiplies<float>()(3.14, 3.14): 9.8596
  15. std::multiplies<float>()(3, 3): 9
  16. std::multiplies<int>()(3.14, 3.14): 9
  17. std::multiplies<Cell>()(Cell{101, 101}, Cell{202, 202}): {20402,20402}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/818492
推荐阅读
相关标签
  

闽ICP备14008679号