简体   繁体   English

从ints c ++的向量中返回一个unsigned long

[英]Return an unsigned long from vector of ints c++

What's the best method for returning an unsigned long from a vector of ints? 从int向量中返回unsigned long的最佳方法是什么? I'm working on a BigInt class in c++ and I'm storing the large numbers in a vector. 我正在用c ++编写一个BigInt类,我将大数字存储在一个向量中。 I want to write a method that will return this vector as a standard long, provided it isn't larger than unsigned long can hold. 我想编写一个方法,将该向量作为标准长度返回,前提是它不大于unsigned long可以容纳的长度。 Thanks 谢谢

Something along these lines, assuming the ints are stored in the vector with the least significant first: 沿着这些线的某些东西,假设整数存储在具有最低重要性的向量中:

size_t bits_in_int = std::numeric_limits<int>::digits;
size_t bits_in_ulong = std::numeric_limits<unsigned long>::digits;

unsigned long accumulator = 0;
size_t bits_so_far = 0;
for (unsigned long i : the_ints) {
    size_t next_bits = bits_so_far + bits_in_int;
    if (next_bits > bits_in_long) { /* failed, do something about it */}
    accumulator += (i << bits_so_far);
    bits_so_far = next_bits;
}
return accumulator;

Notes: 笔记:

1) In practice you could save some bother because the number of loops is going to be either 1 or 2 on any vaguely normal-looking C++ implementation. 1)在实践中你可以节省一些麻烦,因为在任何看似模糊的C ++实现中,循环的数量将是1或2。 So you could just write a case where you return the_ints[0] and a case where you return the_ints[0] + (the_ints[1] << bits_in_int) . 所以你可以写一个返回the_ints[0]的情况和一个返回the_ints[0] + (the_ints[1] << bits_in_int)

2) I've been lazy. 2)我一直很懒。 Because int is signed and unsigned long is unsigned, you can actually fit at least one int plus the least significant bit of another int into an unsigned long . 因为int是有符号的并且unsigned long是无符号的,所以实际上可以将至少一个int 加上另一个int的最低有效位放入unsigned long For example you might find bits_in_int is 31 but bits_in_long is 32. 例如,您可能会发现bits_in_int为31但bits_in_long为32。

So actually in the "failed" case there is one last hope for peace, which is that (a) there is only one int left to process, and (b) its value fits in the remaining bits of the result. 所以实际上在“失败”的情况下,对和平有一个最后的希望,即(a)只剩下一个int来处理,(b)它的值适合结果的剩余部分。 But like I say, I'm lazy, and I think I've shown the components you need to put together. 但就像我说的那样,我很懒,我想我已经展示了你需要组合的组件。

For this reason if no other, you should probably use a vector of unsigned int for your BigInt. 因此,如果没有其他原因,您应该为BigInt使用unsigned int向量。 It's not required that the width of unsigned long is a multiple of the number of bits in unsigned int , but it might be strange enough that you can ignore it. 不需要unsigned long的宽度是unsigned int中位数的倍数,但是你可以忽略它可能很奇怪。

Update for base 10 digits, stored most significant first: 基数为10位的更新,首先存储最重要:

if (the_ints.size() <= std::numeric_limits<unsigned long>::digits10 + 1) {
    std::stringstream ss;
    for (int i : the_ints) ss << char(i + '0');
    unsigned long result;
    if (ss >> result) return result;
}
/* failed, do something about it */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM