取得std :: vector的总和是使用引用而不是值?(Taking sum of std::vector is using references instead of values?)

编程入门 行业动态 更新时间:2024-10-24 14:16:14
取得std :: vector的总和是使用引用而不是值?(Taking sum of std::vector is using references instead of values?)

我有一个包含整数值的向量,我希望总结。 然而,它总结了错误 - 如果向量在-10和10之间保持10个整数,我的总数不应该是18446744073709551602 。

我声明矢量,并用值填充它:

std::vector<int> X; for ( int i = 0; i < readings.size() ; ++i ) { X.push_back(readings[i].x);

然后我总结并展示:

unsigned long temptotal = 0; for ( int i = 0; i < readings.size() ; ++i ) { std::cout << "Value in X: " << X[i] << std::endl; temptotal += X[i]; } std::cout << "Temp total: " << temptotal << std::endl;

它产生以下输出: Temp total: 18446744073709551603 。

当我遍历GDB中的求和循环时(在temptotal += X[i]; ),我可以print X[i] :

(gdb) p X[i] $1 = (reference) @0x100100c80: -1

在添加之前和之后打印temptotal :

(gdb) p temptotal $2 = 0 (gdb) p temptotal $3 = 18446744073709551615

我相当肯定,我添加内存位置而不是内存位置的值 - 我可能是错误的 - 但我不知道为什么我不在X[i]添加值。 当我用readings[i].x的值填充X时,它们是double readings[i].x值,然后我将其输入到int 。 这是我的错误吗? 在GDB中,我对类型的readings[i].x值应该是一个整数,或者至少是某种类型的数值:

(gdb) p (int)readings[i].x $1 = -1

我很难过; 如果有人能够说明为什么我不添加我认为我所添加的内容,我会很感激。

编辑 这是从int到unsigned long的投射。 我翻遍了它,按“提交问题”并发现我的错误。 虽然,我仍然不明白为什么它正在做它正在做的事情 - 任何光照都会很好 - 为什么,如果我将一个int添加到unsigned long ,它是否不从int取值?

I have a vector containing integer values, which I wish to sum. However, it's summing the total wrong - if the vector holds 10 ints between -10 and 10, my total should not be 18446744073709551602.

I declare the vector, and populate it with values:

std::vector<int> X; for ( int i = 0; i < readings.size() ; ++i ) { X.push_back(readings[i].x);

I then sum and display:

unsigned long temptotal = 0; for ( int i = 0; i < readings.size() ; ++i ) { std::cout << "Value in X: " << X[i] << std::endl; temptotal += X[i]; } std::cout << "Temp total: " << temptotal << std::endl;

which produces the following output: Temp total: 18446744073709551603.

When I step through the summation loop in GDB, (at temptotal += X[i];), I can print X[i]:

(gdb) p X[i] $1 = (reference) @0x100100c80: -1

Printing temptotal before and after the addition is performed:

(gdb) p temptotal $2 = 0 (gdb) p temptotal $3 = 18446744073709551615

I'm fairly sure that I'm adding the memory locations rather than the values in the memory locations - I could be wrong on that - but I'm not sure why I'm not adding the values at X[i]. When I populate X with the values from readings[i].x, they're doubles, which I then type-cast to ints. Is this where my error is? Checking in GDB, my type-cast of the readings[i].x value should be an int, or, at least, a number of some kind:

(gdb) p (int)readings[i].x $1 = -1

I'm stumped; if somebody could shed some light on why I'm not adding what I think I'm adding, I'd be grateful.

EDIT It's the casting from int to unsigned long. I went over it and over it, pressed "submit question" and spotted my error. Although, I still don't understand why it's doing what it's doing - any light shed on that would be wonderful - why, if I'm adding an int to an unsigned long, does it not take the value from the int?

最满意答案

这是因为你的temptotal是没有签名的。 所有添加到它的数字都被解释为无符号的,因此小的负数变成二进制补码表示中的大正数。

This is because your temptotal is unsigned. All numbers added to it are interpreted as unsigned, hence small negative numbers become large positive numbers in two's complement representation.

更多推荐

本文发布于:2023-08-01 00:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350536.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   而不是   vector   std   references

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!