dhayden wrote: |
---|
1 2 3
|
//Use '\n' instead of endl unless you positively MUST flush the
// stream. Otherwise you can kill performance
cerr << err << '\n';
|
|
Sort of true, but wrong at the same time. Yes, only use
endl when you need to flush the stream. No, it won't kill performance; most console programs of any significance are not I/O bound performance-wise, particularly when you only have one output statement that needs to be flushed anyway; in addition, when you're printing an error, usually you'd care more that the output gets flushed than for performance.
In addition, the way you've written it you get no performance gains whatsoever.
std::cerr is unbuffered, and tied to
std::cout, which means it gets flushed
every time you write to it.
Moreover, unless you explicitly specify otherwise the C++ streams are synced with the C output streams, which means even if you used the (nominally) buffered variant
std::clog the streams are unbuffered regardless, so you still end up flushing whenever the C streams feel like it (which IIRC is at every newline).
Long-wided diatribe aside; this doesn't actually make a difference performance-wise. Your best hope for any performance gains would be to use
std::cout or
std::clog instead, but in almost all cases such performance gains would be nigh unnoticeable.
TL;DR:
std::endl is fine.
dhayden wrote: |
---|
1 2 3
|
// When passing read-only data that is big, pass by const reference
// to avoid an expensive copy operation
Room(int w, int h, const vector<string> &d) : width(w), height(h), data(d) {}
|
|
... except when you proceed to immediately copy said data regardless, in which case (from C++11, at least) passing by value is either equally good or better. The OP's code is fine.
dhayden wrote: |
---|
1 2
|
// Warn the caller that you can throw an exception
vector<Room> getRoomLayouts(const string filename) throw (const char *);
|
|
https://stackoverflow.com/questions/88573/should-i-use-an-exception-specifier-in-c?noredirect=1&lq=1