(C++)重载运算符<<,如果我不使用endl,则覆盖输出

(C++) Overloading operator << , overwrite the output if I do not use endl

本文关键字:lt endl 输出 覆盖 重载 C++ 如果 运算符      更新时间:2023-10-16

我正在为大学做C 项目。我必须对历史事件进行年表。我有一个可以保存事件的课程,称为" Fechahistorica",set<string>int作为私人成员。该集合包含字符串,每个字符串是一个历史事件。INT是所有事件发生的那一年。另一个类称为" cronologia",用map<int,FechaHistorica>存储每个" Fechahistorica"。

问题是运营商&lt;&lt;我猜是Fechahistorica。执行键带有电影恢复两个.txt文件,而Outpout应该是两个文档的结合。这是运营商的代码OS Fechahistorica:

const char SEP = '#';
ostream& operator<< (ostream& os, const FechaHistorica& e) {
    os << e.getYear();
    for(const_iterator p = e.eventos.begin(); p != e.eventos.end(); ++p)
        os << SEP << *p;
    return os;
}

输出为:

victor@victor-GL552VW:~/Documentos/Universidad/1º Cuatrimestre/Estructura de dat
os/Practicas/Practicas-ED/Práctica 4 - STL e Iteradores$ ./bin/prueba movies.txt
 films.txt
Cronología leida del archivo: 
0
1900#Sherlock Holmes Baffled#The Enchanted Drawing
1901#Scrooge, or, Marley's Ghost#Star Theatre
1902#A Trip to the Moon
#The Great Train Robbery Fireman
1904#The Impossible Voyage
1905#Adventures of Sherlock Holmes; or, Held for Ransom
#Humorous Phases of Funny Faces#The Story of the Kelly Gang
1907#Ben Hur#L'Enfant prodigue
#Fantasmagorie#The Assassination of the Duke of Guise#The Taming of the Shrew#The Thieving Hand
#The Country Doctoreat#Les Misérables#Princess Nicotine; or, The Smoke Fairy
1910#In Old California'
1911#Defence of Sevastopol#L'Inferno
1912#Independenţa României#The Musketeers of Pig Alley
1913#Barney Oldfield's Race for a Life#Fantômas#Raja Harishchandra#The Bangville Police
1914#Judith of Bethulia#The Perils of Pauline#Tillie's Punctured Romance
1915#A Fool There Was#Birth of a Nation#Les Vampires#The Tramp

您可以看到,几年的印刷不正确,输出应为:

1900#Sherlock Holmes Baffled#The Enchanted Drawing
1901#Scrooge, or, Marley's Ghost#Star Theatre
1902#A Trip to the Moon
1903#The Great Train Robbery#Life of an American Fireman
1904#The Impossible Voyage
1905#Adventures of Sherlock Holmes; or, Held for Ransom
1906#The Story of the Kelly Gang#Humorous Phases of Funny Faces#Dream of a Rarebit Fiend
1907#Ben Hur#L'Enfant prodigue
1908#Fantasmagorie#The Taming of the Shrew#The Thieving Hand#The Assassination of the Duke of Guise#A Visit to the Seaside
1909#The Country Doctor#A Corner in Wheat#Les Misérables#Princess Nicotine; or, The Smoke Fairy
1910#In Old California'
1911#L'Inferno#Defence of Sevastopol
1912#Independenţa României#The Musketeers of Pig Alley
1913#The Bangville Police#Barney Oldfield's Race for a Life#Fantômas#Raja Harishchandra
1914#The Perils of Pauline#Judith of Bethulia#Tillie's Punctured Romance
1915#Birth of a Nation#Les Vampires#The Tramp#A Fool There Was

好吧,如果我在每个事件发生后添加endl,则输出是正确的,我的意思是,os << SEP << *p << endl;而不是os << SEP << *p;,但是我不想在输出中的每个历史事件后都具有ENDL。我该怎么办?

我希望你能帮我,谢谢。

带有数据的文件是" tover.txt"answers" films.txt"Proyect的完整代码在这里:https://github.com/vicase/practicas-ed/tree/master/master/práCTICA 4 4 4 -20STL STL E ETIREDITERADORESIDETEME>

修复:问题是第一个TXT是以CRLF格式(Windows),另一个是在LF(Ubuntu)中。LF中的两个文档都可以正常工作。谢谢大家