简体   繁体   English

在基于范围的for循环中使用双/整数和缩小数据

[英]Double/Integer use and data narrowing within a Range-Based for loop

I am working my way through Bjarne Stroustrup's Programming Principles and Practice and am having difficulty. 我正在通过Bjarne Stroustrup的编程原则和实践工作,我遇到了困难。 I am currently reading on Vectors and have been introduced to Range-based for loops. 我目前正在阅读Vector,并已介绍到基于范围的for循环。 Below I have some code, that from my eyes, appears to be reading a double into an INT; 下面我有一些代码,从我的眼睛看,似乎正在阅读一个双重进入INT; which I assume causes narrowing. 我认为这会导致缩小范围。

int main()
{
    vector<double> temps;               // temperatures
    for (double temp; cin >> temp; )    // read into temp
        temps.push_back(temp);          // put temp into vector
                                        // compute mean temperature:
    double sum = 0;
    for (int x : temps) sum += x;
        cout << "Average temperature: " << sum / temps.size() << '\n'; 
                                        // compute median temperature:
    sort(temps);                        // sort temperatures
    cout << "Median temperature: " << temps[temps.size() / 2] << '\n';
    keep_window_open();
    return 0;
}

Upon trying this a few times with different inputs I have arrived at the conclusion this is indeed narrowing. 在尝试了几次不同的输入后,我得出结论,这确实在缩小。 I am creating a vector of doubles and than in the for(int x : temps) loop, taking the first element within temp, putting it within x, processing it than incrementing to the next element and repeating. 我正在创建一个双精度矢量,而不是for(int x:temps)循环,将第一个元素放在temp中,将它放在x中,处理它而不是递增到下一个元素并重复。 Because the element (a double) is being read into x (an integer) causing the narrowing. 因为元素(双精度)被读入x(整数),导致变窄。

My main question is if I am indeed correct it is narrowing the elements within the vector (maybe it doesn't actually read into x and that int x is describing something else), can I simply replace for(int x : temps) to for(double x : temps) to avoid this narrowing, or is the use of an integer in the range-based for loop parameters mandatory (maybe designed that way). 我的主要问题是,如果我确实是正确的,它正在缩小向量中的元素(可能它实际上没有读入x并且int x正在描述其他内容),我可以简单地将(int x:temps)替换为for (double x:temps)以避免这种缩小,或者是在基于范围的for循环参数中强制使用整数(可能是这样设计的)。 Any thoughts, thanks. 有任何想法,谢谢。

It is a narrowing conversion, yes (Section 11.6.4 aka [dcl.init.list] of the C++ standard , item 7.1). 这是一个缩小的转换,是( C ++标准的第11.6.4节[dcl.init.list], 7.1项)。 I would guess it's a mis-type. 我猜这是一种错误的类型。 Pretend it said double x (or auto x if you already know about the auto keyword, which makes the compiler deduce the type if possible). 假装它说double x (如果你已经知道auto关键字,则auto x ,这使得编译器在可能的情况下推断出类型)。

I can't find this in the 2nd edition errata though. 我在第2版勘误表中找不到这个。 I'd actually drop Prof. Stroustrup a line and ask him (don't be shy - as you can see, the book has a bunch of errors, he should be appreciative of the question). 我实际上放弃了Stroustrup教授的一句话并问他(不要害羞 - 你可以看到,这本书有一堆错误,他应该感谢这个问题)。

  1. Yes, you can use any type to initialize your for loop variable (any version of it). 是的,您可以使用任何类型来初始化for循环变量(它的任何版本)。
  2. Yes, any conversion from floating point type to integer type in C/C++, always return the truncated value of this number ( floor operation). 是的,在C / C ++中从浮点类型到整数类型的任何转换 ,总是返回此数字的截断值( floor操作)。

Please, take a look at C++ casting rules: 请看一下 C ++的转换规则:

If the conversion is from a floating-point type to an integer type, the value is truncated (the decimal part is removed). 如果转换是从浮点类型转换为整数类型,则会截断该值(删除小数部分)。 If the result lies outside the range of representable values by the type, the conversion causes undefined behavior. 如果结果位于类型的可表示值范围之外,则转换会导致未定义的行为。

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

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