reinterpret_cast应用
reinterpret_cast 的一个实际用途是在哈希函数中,即,通过让两个不同的值几乎不以相同的索引结尾的方式将值映射到索引。
#include <iostream>
using namespace std; // Returns a hash code based on an address
unsigned short Hash( void *p ) {
unsigned int val = reinterpret_cast<unsigned int>( p );
return ( unsigned short )( val ^ (val >> 16));
} using namespace std;
int main() {
int a[20];
for ( int i = 0; i < 20; i++ )
cout << Hash( a + i ) << endl;
} Output:
64641
64645
64889
64893
64881
64885
64873
64877
64865
64869
64857
64861
64849
64853
64841
64845
64833
64837
64825
64829
reinterpret_cast 允许将指针视为整数类型。结果随后将按位移位并与自身进行“异或”运算以生成唯一的索引(具有唯一性的概率非常高)。该索引随后被标准 C 样式强制转换截断为函数的返回类型。
reinterpret_cast应用的更多相关文章
- c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast
c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast [版权声明]转载请注明出处 http://www.cnblogs.c ...
- static_cast、dynamic_cast、reinterpret_cast、const_cast以及C强制类型转换的区别
static_cast 1. 基础类型之间互转.如:float转成int.int转成unsigned int等 2. 指针与void*之间互转.如:float*转成void*.CBase*转成void ...
- C++标准转换运算符reinterpret_cast
C++标准转换运算符reinterpret_cast reinterpret_cast <new_type> (expression) reinterpret_cast运算符是用来处理无关 ...
- c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast
c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构 dynamic_ca ...
- static_cast dynamic_cast const_cast reinterpret_cast总结对比
[本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...
- C++-const_cast, reinterpret_cast, static_cast的用法
/////////////////////////////////////////////////////////////////////////////// // // FileName : cas ...
- c++强制类型转换(static_cast,const_cast,dynamic_cast,reinterpret_cast)
static_cast <typeid>(exdlvssion) static_cast 很像 C 语言中的旧式类型转换.它能进行基础类型之间的转换,也能将带有可被单参调用的构造函数或用户 ...
- static_cast和reinterpret_cast
static_cast和reinterpret_cast 相同点:都是暴力转换,从一个类型转换为另一个类型,对于类指针不会保证安全性 static_cast和reinterpret_cast的区别 ...
- dynamic_cast,const_cast,static_cast,reinterpret_cast 详解
如果直接指针直接强转,将只能访问虚函数的内容,而不能访问特定类中的特定成员或方法!!!! 强制类型转换运算符:C++有四种强制类型转换符,分别是dynamic_cast,const_cast,stat ...
- c++中的强制转换static_cast、dynamic_cast、reinterpret_cast的不同用法儿
c++中的强制转换static_cast.dynamic_cast.reinterpret_cast的不同用法儿 虽然const_cast是用来去除变量的const限定,但是static_cast ...
随机推荐
- 安装Theano
参考文档 http://deeplearning.net/software/theano/install_centos6.html#install-centos6 安装依赖库 sudo yum ins ...
- 需要我们了解的SQL Server阻塞原因与解决方法
需要我们了解的SQL Server阻塞原因与解决方法 上篇说SQL Server应用模式之OLTP系统性能分析.五种角度分析sql性能问题.本章依然是SQL性能 五种角度其一“阻塞与死锁” 这里通过连 ...
- window.setTimeout()函数的使用
<script type="text/javascript"> //此程序主要完成页面定时关闭功能 function closeMyWindow() { window. ...
- Python爬虫小白入门(五)PhatomJS+Selenium第二篇
一.前言 前文介绍了PhatomJS 和Selenium 的用法,工具准备完毕,我们来看看如何使用它们来改造我们之前写的小爬虫. 我们的目的是模拟页面下拉到底部,然后页面会刷出新的内容,每次会加载10 ...
- 错误: 找不到或无法加载主类 tomcat-juli.jar
错误: 找不到或无法加载主类 tomcat-juli.jar 然后就可以用了.
- Java入门第二季第一章类和对象知识点
Java 中的 static 使用之静态方法 1. 静态方法中可以直接调用同类中的静态成员,但不能直接调用非静态成员.如: 如果希望在静态方法中调用非静态变量,可以通过创建类的对象,然后通过对象来访问 ...
- 为什么说2017全球云计算大会中国站 (Cloud Connect China 2017)不得不参加?
2017全球云计算大会中国站将继续关注全球云计算产业现状及最热门的领域,设立主题论坛.2017全球云计算大会中国站将于2017年9月5-7日在上海世博展览馆.正如去年9月活动现场所预告的,2017年除 ...
- ng1笔记
AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 AngularJS 应用程序. ng-model 指令把元素值(比如输入域的值)绑定到应用程序. ...
- Aspose系列实现docx转PDF,PPT转PDF,EXCEL转PDF
没有什么营养,就是调用一下这个组件.其实一开始用的是Microsoft.Office.Interop.Excel;Microsoft.Office.Interop.Word 但是在服务器要注意,服务器 ...
- netty高级篇(3)-HTTP协议开发
一.HTTP协议简介 应用层协议http,发展至今已经是http2.0了,拥有以下特点: (1) CS模式的协议 (2) 简单 - 只需要服务URL,携带必要的请求参数或者消息体 (3) 灵活 - 任 ...