(C ++)重载运算符<< ,如果我不使用endl

时间:2017-11-26 11:22:25

标签: c++ operator-keyword

我正在为大学做一个C ++项目。我必须制作历史事件的年表。我有一个类来保存事件,名为“FechaHistorica”,其中set<string>int为私人成员。该集包含字符串,每个字符串是一个历史事件。 int是所有事件发生的年份。 另一个类称为“Cronologia”,其中map<int,FechaHistorica>用于存储每个“FechaHistorica”。

问题在于运营商&lt;&lt;我想是FechaHistorica。 executbale使用电影回收两个.txt文件,并且outpout应该是两个文档的并集。这是运营商的代码&lt;&lt; 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输出中的事件。我该怎么办?

我希望你能帮助我,谢谢。

包含数据的文件是“movies.txt”和“films.txt” proyect的完整代码在这里:https://github.com/vicase/Practicas-ED/tree/master/Pr%C3%A1ctica%204%20-%20STL%20e%20Iteradores

1 个答案:

答案 0 :(得分:0)

修正:问题是第一个txt是crlf格式(Windows)而另一个是LF(Ubuntu)。 LF中的两个文件都可以正常工作。谢谢大家