C++函数对象-运算符函数对象 - 位运算 - 实现 x y 的函数对象 (std::bit_and)

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

运算符函数对象

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

位运算

实现 x & y 的函数对象

std::bit_and

template< class T >
struct bit_and;

(C++14 前)

template< class T = void >
struct bit_and;

(C++14 起)

进行逐位与的函数对象。等效地调用类型 T 上的 operator& 。

特化

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

bit_and<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::bit_and::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 的结果。

异常

(无)

可能的实现

constexpr T operator()(const T &lhs, const T &rhs) const 
{return lhs & rhs;
}

 std::bit_and<void>

std::bit_and<void>

template<>
class bit_and<void>;

(C++14 起)

std::bit_and<> 是 std::bit_and 的特化,拥有推导出的参数和返回类型。

成员类型

成员类型定义
is_transparent/* 未指定 */

成员函数

operator()

应用 operator& 到 lhs 和 rhs
(公开成员函数)

 

std::bit_and<>::operator()

template< class T, class U>

constexpr auto operator()( T&& lhs, U&& rhs ) const

  -> decltype(std::forward<T>(lhs) & std::forward<U>(rhs));

返回 lhs & rhs 的结果。

参数

lhs, rhs-为之计算逐位与的值

返回值

lhs & rhs 的结果。

调用示例

#include <iostream>
#include <functional>struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell(const Cell &cell){x = cell.x;y = cell.y;}Cell &operator+(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator+=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator*=(int n){x *= n;y *= n;return *this;}Cell &operator++(){x += 1;y += 1;return *this;}friend Cell operator +(const Cell &cell1, const Cell &cell2){Cell cell = cell1;cell += cell2;return cell;}friend Cell operator *(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x * cell2.x, cell1.y * cell2.y};return cell;}friend Cell operator /(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x / cell2.x, cell1.y / cell2.y};return cell;}friend Cell operator %(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x % cell2.x, cell1.y % cell2.y};return cell;}friend bool operator ==(const Cell &cell1, const Cell &cell2){return cell1.x == cell2.x && cell1.y == cell2.y;}friend bool operator !=(const Cell &cell1, const Cell &cell2){return cell1.x != cell2.x && cell1.y != cell2.y;}friend bool operator <(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y < cell2.y;}else{return cell1.x < cell2.x;}}friend bool operator >(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y > cell2.y;}else{return cell1.x > cell2.x;}}friend bool operator &&(const Cell &cell1, const Cell &cell2){return cell1.x && cell2.x && cell1.y && cell2.y;}friend bool operator ||(const Cell &cell1, const Cell &cell2){return cell1.x || cell2.x || cell1.y || cell2.y;}friend bool operator !(const Cell &cell){return !(cell.x && cell.x);}friend Cell operator &(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x & cell2.x, cell1.y & cell2.y};return cell;}friend Cell operator |(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x | cell2.x, cell1.y | cell2.y};return cell;}friend Cell operator ^(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x ^ cell2.x, cell1.y ^ cell2.y};return cell;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}int main()
{std::cout << std::boolalpha;int *ptr = nullptr;
//    std::cout << "std::bit_and<int*>()(ptr, nullptr):      "
//              << std::bit_and<int*>()(ptr, nullptr) << std::endl;std::cout << "std::bit_and<char>()(50, 2):             "<< std::bit_and<char>()(50, 2) << std::endl;std::cout << "std::bit_and<char>()('a', 97):           "<< std::bit_and<char>()('a', 97) << std::endl;std::cout << "std::bit_and<int>()(1023, 1024):         "<< std::bit_and<int>()(1023, 1024) << std::endl;std::cout << "std::bit_and<long>()(1023, 1024):        "<< std::bit_and<long>()(1023, 1024) << std::endl;std::cout << "std::bit_and<long long>()(1023, 1024):   "<< std::bit_and<long long>()(1023, 1024) << std::endl;std::cout << "std::bit_and<uint8_t>()(1023, 1024):     "<< std::bit_and<uint8_t>()(8, 32) << std::endl;std::cout << "std::bit_and<uint16_t>()(123, 456):      "<< std::bit_and<uint16_t>()(123, 456) << std::endl;std::cout << "std::bit_and<uint32_t>()(101, 202):      "<< std::bit_and<uint32_t>()(101, 202) << std::endl;std::cout << "std::bit_and<uint64_t>()(10230, 10240):  "<< std::bit_and<uint64_t>()(10230, 10240) << std::endl;std::cout << "std::bit_and<int8_t>()(1023, 1024):      "<< std::bit_and<int8_t>()(8, 32) << std::endl;std::cout << "std::bit_and<int16_t>()(123, 456):       "<< std::bit_and<int16_t>()(123, 456) << std::endl;std::cout << "std::bit_and<int32_t>()(101, 202):       "<< std::bit_and<int32_t>()(101, 202) << std::endl;std::cout << "std::bit_and<int64_t>()(10230, 10240):   "<< std::bit_and<int64_t>()(10230, 10240) << std::endl;//    std::cout << "std::bit_and<double>()(3.14, 3.14):      "
//              << std::bit_and<double>()(3.14, 3.14) << std::endl;
//    std::cout << "std::bit_and<float>()(3.14, 3.14):       "
//              << std::bit_and<float>()(3.14, 3.14) << std::endl;
//    std::cout << "std::bit_and<float>()(3, 3):             "
//              << std::bit_and<float>()(3, 3) << std::endl;
//    std::cout << "std::bit_and<float>()(3.56, 3.14):       "
//              << std::bit_and<float>()(3.56, 3.14) << std::endl;
//    std::cout << "std::bit_and<int>()(3.14, 3.14):         "
//              << std::bit_and<int>()(3.34, 3.34) << std::endl;std::cout << "std::bit_and<Cell>()(Cell{101, 101}, Cell{202, 202}):       "<< std::bit_and<Cell>()(Cell{101, 101}, Cell{202, 202}) << std::endl;//编译失败
//    std::cout << "std::bit_and<std::string>()(\"I am a \", \"handsome programmer\"):"
//              << std::bit_and<std::string>()("I am a ", "handsome programmer") << std::endl;return 0;
}

输出

std::bit_and<char>()(50, 2):
std::bit_and<char>()('a', 97):           a
std::bit_and<int>()(1023, 1024):         0
std::bit_and<long>()(1023, 1024):        0
std::bit_and<long long>()(1023, 1024):   0
std::bit_and<uint8_t>()(1023, 1024):
std::bit_and<uint16_t>()(123, 456):      72
std::bit_and<uint32_t>()(101, 202):      64
std::bit_and<uint64_t>()(10230, 10240):  8192
std::bit_and<int8_t>()(1023, 1024):
std::bit_and<int16_t>()(123, 456):       72
std::bit_and<int32_t>()(101, 202):       64
std::bit_and<int64_t>()(10230, 10240):   8192
std::bit_and<Cell>()(Cell{101, 101}, Cell{202, 202}):       {64,64}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/681116.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《Java 简易速速上手小册》第7章:Java 网络编程(2024 最新版)

文章目录 7.1 网络基础和 Java 中的网络 - 揭开神秘的面纱7.1.1 基础知识7.1.2 重点案例&#xff1a;实现一个简单的聊天程序7.1.3 拓展案例 1&#xff1a;使用 UDP 进行消息广播7.1.4 拓展案例 2&#xff1a;建立一个简单的 Web 服务器 7.2 创建客户端和服务器 - 构建沟通的桥…

Netty源码系列 之 FastThreadLocal源码

目录 Netty优化方案之 FastThreadLocal 前言 ThreadLocal ThreadLocal是干什么的&#xff1f; 为什么要使用ThreadLocal工具类去操控存取目标数据到Thread线程 &#xff1f; ThreadLocal的使用场景 目标数据存储到Thread线程对象的哪里&#xff1f; 怎么样把一个目标数据…

学习Android的第六天

目录 Android TextView 文本框 TextView 基础属性 范例 带阴影的TextView 范例 带边框的TextView 范例 带图片(drawableXxx)的TextView 范例1 范例2 使用autoLink属性识别链接类型 范例 TextView 显示简单的 HTML 范例1 范例2 SpannableString & Spannable…

政安晨:演绎在KerasCV中使用Stable Diffusion进行高性能图像生成

小伙伴们好&#xff0c;咱们今天演绎一个使用KerasCV的StableDiffusion模型生成新的图像的示例。 考虑计算机性能的因素&#xff0c;这次咱们在Colab上进行&#xff0c;Colab您可以理解为在线版的Jupyter Notebook&#xff0c;还不熟悉Jupyter的的小伙伴可以去看一下我以前的文…

python+ flask+MySQL旅游数据可视化81319-计算机毕业设计项目选题推荐(免费领源码)

摘要 信息化社会内需要与之针对性的信息获取途径&#xff0c;但是途径的扩展基本上为人们所努力的方向&#xff0c;由于站在的角度存在偏差&#xff0c;人们经常能够获得不同类型信息&#xff0c;这也是技术最为难以攻克的课题。针对旅游数据可视化等问题&#xff0c;对旅游数据…

[英语学习][28][Word Power Made Easy]的精读与翻译优化

[序言] 译者的这次翻译, 如果按照一一对应的单词翻译&#xff0c;正确率是100%, 但可惜的是, 从整体的翻译上, 正确率下降到30%. 也就是说没有正确表达出作者的逻辑. [英文学习的目标] 提升自身的英语水平, 对日后编程技能的提升有很大帮助. 希望大家这次能学到东西, 同时加入…

端口映射原理及实验概要

端口映射是一种网络技术&#xff0c;用于将外部网络请求定向到内部网络中的特定服务或设备。其原理可以分为两个部分&#xff1a;NAT&#xff08;Network Address Translation&#xff09;和端口转发。 NAT是一种将网络数据包的目标IP地址和端口从一个网络地址转换为另一个网络…

Git仓库

1、安装 sudo yum install -y git 2、什么叫版本控制Git&&Gitee&&Github 对源代码的历史修改进行维护&#xff0c;保留历史的所有的修改痕迹。 在Linux中&#xff0c;版本控制是指对软件、文档等项目的不同版本进行管理和追踪的过程。通常使用版本控制系统&…

图表自动化开篇

目录 前言&#xff1a; 使用 Canvas 或者 SVG 渲染 选择哪种渲染器 代码触发 ECharts 中组件的行为 前言&#xff1a; 图表自动化一直以来是自动化测试中的痛点&#xff0c;也是难点&#xff0c;痛点在于目前越来越多公司开始构建自己的BI报表平台但是没有合适的自动化测试…

BLE蓝牙安全机制

1. 蓝牙配对 蓝牙配对是建立安全连接的关键过程。对于传统蓝牙应采用SSP配对方式&#xff0c;而低功耗蓝牙4.0和4.1的版本应使用legacy pairing配对方式&#xff0c;4.2以后的版本应采用Secure connection的配对方式。 传统的蓝牙采用的配对方式如下&#xff1a; 蓝牙2.1版本…

1012: 【C1】【循环】【入门】均值

题目描述 给出一组样本数据&#xff0c;计算其均值。 输入 输入有两行&#xff0c;第一行包含一个整数n&#xff08;1&#xff1c;n&#xff1c;100&#xff09;&#xff0c;代表样本容量&#xff1b;第二行包含n个绝对值不超过1000的浮点数&#xff0c;代表各个样本数据。 …

《Linux 简易速速上手小册》第4章: 包管理与软件安装(2024 最新版)

文章目录 4.1 包管理基础4.1.1 重点基础知识4.1.2 重点案例&#xff1a;在 Ubuntu 上安装和管理软件4.1.3 拓展案例 1&#xff1a;添加软件仓库4.1.4 拓展案例 2&#xff1a;回滚软件到旧版本 4.2 使用 APT 与 YUM4.2.1 重点基础知识4.2.2 重点案例&#xff1a;在 Ubuntu 上配置…

排序刷题11

题目来源&#xff1a;[NOIP1998 提高组] 拼数 - 洛谷 解题思路&#xff1a;这道题重点在于怎么把数字拼接&#xff0c;得到最大的值。这里可以用to_string&#xff08;&#xff09;函数&#xff0c;将数字先转换为字符再拼接&#xff0c;最后得到拼接的最大值。ps&#xff1a;…

Kong 负载均衡

负载均衡是一种将API请求流量分发到多个上游服务的方法。负载均衡可以提高整个系统的响应速度&#xff0c;通过防止单个资源过载而减少故障。 在以下示例中&#xff0c;您将使用部署在两台不同服务器或上游目标上的应用程序。Kong网关需要在这两台服务器之间进行负载均衡&…

2024年华为OD机试真题-求幸存数之和-Java-OD统一考试(C卷)

题目描述: 给一个正整数列 nums,一个跳数 jump,及幸存数量 left。运算过程为:从索引为0的位置开始向后跳,中间跳过 J 个数字,命中索引为J+1的数字,该数被敲出,并从该点起跳,以此类推,直到幸存left个数为止。然后返回幸存数之和。 约束: 1)0是第一个起跳点。 2)起…

Java中,List、Map和Set的区别是什么?

在Java中&#xff0c;List、Map和Set是三种常用的集合类型&#xff0c;它们之间的主要区别如下&#xff1a; 1、List List是有序集合&#xff0c;它可以包含重复元素。 List中的元素是按照插入顺序排列的&#xff0c;可以通过索引访问每个元素。 Java中常见的List实现类有A…

简单易懂:一篇文章讲透所有Python知识

Python语法简单&#xff0c;应用领域广&#xff0c;本文给出 Python八大核心知识点&#xff0c;供大家迅速上手&#xff0c;建议收藏。 第一部分&#xff1a;基础语法 # 6种数据类型# 整数、浮点数、字符串 x 2 y 3.1415926 name "You jump, I jump"# 列表、元组…

嵌入式Qt 计算器界面设计

一.计算器界面设计 计算机界面程序分析&#xff1a; 需要用到的组件&#xff1a; 界面设计&#xff1a; 界面设计实现&#xff1a; 实验1&#xff1a;计算器界面设计 #include <QtGui/QApplication> #include <QWidget> //主窗口 #include <QLineEdit> //文…

2024年2月5日-2月11日周报

论文阅读 1. 本周计划2. 完成情况2.1 论文摘要2.2 网络结构2.3 损失函数2.4 优化器2.5 代码2.5.1 代码结果2.5.2 代码大致流程 4. 总结及收获4. 下周计划 1. 本周计划 阅读论文《Data-Driven Seismic Waveform Inversion: A Study on the Robustness and Generalization》并实…

桥接模式:连接抽象与实现的设计艺术

桥接模式&#xff1a;连接抽象与实现的设计艺术 在软件开发中&#xff0c;设计模式是帮助我们以优雅的方式解决问题的模板。桥接模式&#xff08;Bridge Pattern&#xff09;是一种结构型设计模式&#xff0c;它的主要目标是将抽象部分与实现部分分离&#xff0c;这样两者可以…