我可以使自己的自定义数据类型比 C++ 中的数据类型大吗? [英] Can I Make my Own Custom Data Type Larger than the Ones in C++?

查看:25
本文介绍了我可以使自己的自定义数据类型比 C++ 中的数据类型大吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在 C++ 中实现 big int

例如,假设我有一个long"类型的变量.long 可以存储从 -?9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 的值.我是否可以创建自己的 C++ 数据类型来创建无限大小的整数?我知道从记忆的角度来看这有点鲁莽,但我只是好奇.

For example, say I have a variable of the "long" type. A long can store values from -⁠9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Is it possible for me to create my own C++ data type that allows me to create integers of unlimited size? I know it's kind of reckless from a memory standpoint, but I'm just curious.

推荐答案

哈!这是一个鲁莽的答案!

Ha! Here is a reckless answer!

只需创建一个动态创建和添加位集的类.要实现该类,您应该创建一个存储 unsigned long 的向量(或您想要/需要的任何适当的容器)(每个 unsigned long 表示一个比它前面的维度更高的位集;您也可以使用任何您想要/需要的 POD 类型)想要).

Just create a class that dynamically creates and adds bit sets. To implement the class, you should create a vector (or any appropriate container you want/need) that store unsigned longs (each unsigned long represents a bit set of higher dimension than that of what precedes it; you can also use any POD types you want).

对于具有几乎无限可能值的类,您应该为值上溢和下溢实现自动检测(最好在运算符上).对于溢出,您应该在最后一个位集之后添加一个更高维度的位集.应通过设置包含零值的参考位集来处理下溢.如果该值低于零(或最低值),则应在参考(或包含最低值的)位集之前添加一个位集.

For the class to have virtually unlimited possible values, you should implement automatic detection (preferably on operators) for value overflows and underflows. For overflows you should then add a bit set of higher dimension after the last bit set. Underflows should be handled by setting a reference bit set that contains the value zero. If the value becomes lower than zero (or the lowest value), you should then add a bit set before the reference (or that which contains the lowest value) bit set.

请阅读有关二进制算术和基数的信息.他们应该给你思路(而且确实非常有用!).

Please read about binary arithmetic and base numbers. They should give you the idea (and are very useful indeed!).

这篇关于我可以使自己的自定义数据类型比 C++ 中的数据类型大吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆