为什么iostream会用某些词语切掉第一个字母?

JAu*_*tin 6 c++ iostream

注意:请在回答之前阅读评论.问题似乎是编译器特定的.

我有一个简单的程序,它从文件或控制台读取名称和一些等级到结构Student_info,然后通过重载<<和>>运算符打印出一些数据.但是,该程序正在切断部分甚至整个单词并移动数据.例如,输入

Eunice 29 87 42 33 18 13 
Mary 71 24 3 96 70 14 
Carl 61 12 10 44 82 36 
Debbie 25 42 53 63 34 95 
Run Code Online (Sandbox Code Playgroud)

回报

Eunice: 42 33 18 13 
Mary: 3 96 70 14 
rl: 10 44 82 36 
25: 63 34 95 
Run Code Online (Sandbox Code Playgroud)

暗示不知何故,流已经忽略了Carl的前两个字母,然后将整个流转移了1个字.我一直试图在一小时的大部分时间内调试这个,但它似乎是随意的.对于不同的名称,不同的单词被切断,没有明显的模式.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

struct Student_info {
    friend std::ostream &operator<<( std::ostream &output,
                                    const Student_info &S ) { // overloads operator to print name and grades
        output << S.name << ": ";
        for (auto it = S.homework.begin(); it != S.homework.end(); ++it)
            std::cout << *it << ' ';
        return output;
    }

    friend std::istream &operator>>( std::istream &input, Student_info &S ) { // overloads operator to read into Student_info object
        input >> S.name >> S.midterm >> S.final;
        double x;
        if (input) {
            S.homework.clear();
            while (input >> x) {
                S.homework.push_back(x);
            }
            input.clear();
        }
        return input;
    }

    std::string name; // student name
    double midterm, final; // student exam scores
    std::vector<double> homework; // student homework total score (mean or median)

};


int main() {
    //std::ifstream myfile ("/Users/.../Documents/C++/example_short.txt");
    Student_info student; // temp object for receiving data from istream
    std::vector<Student_info> student_list; // list of students and their test grades
    while (std::cin >> student) { // or myfile >> student
        student_list.push_back(student);
        student.homework.clear();
    }
    for (auto it = student_list.begin(); it != student_list.end(); ++it) {
        std::cout << *it << '\n';
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:添加换行符.

正如你所看到的,它不适用于clang,但它适用于GCC

n. *_* m. 8

当涉及失败的浮点格式化输入时,它们如何实现标准的实现之间存在不一致.

clang(或者更准确地说,libc++)读取并丢弃有效浮点表示可能包含的所有字符,即使它不能在此位置包含它们并且转换必然会失败.这些字符包括CA(大写和小写,因为它们是十六进制数字,标准实际上允许使用十六进制浮点数表示法).因此,当尝试读取a double和输入包含时Carl,字符C并被A读取和丢弃,即使没有浮点数可以从这些字符中的任何一个开始.

另一方面,一旦变得清楚转换失败,gcc(libstdc++)就会停止读取.因此,如果输入包含Carl,则转换在第一个字符处停止(并且它保留在流中),因为十六进制浮点数不能以C(它必须以0X)开头.

我不会自愿提出关于哪种实施方式正确无误的意见.无论它是什么,正常的代码应该避开语言的微妙和神秘的角落.如果学生记录是一行,那么代码应该读取行.如果学生记录被定义为"一个名称和一系列数字,直到下一个名称",那么请停止阅读本文.

  • @bolov有[这个问题](/sf/ask/1728256491/)探讨了这个问题,并且有一些有趣的外部链接. (2认同)