Strings/Nesting Comments

Started by
13 comments, last by Serapth 17 years, 7 months ago
Here's one that has nothing to do with the topic. :P

Which should I use in coding, \n or end1; ?
Advertisement
doesn't really matter. endl i find to be easier to read but \n can be useful to prevent very long lines of code.
Both std::endl and '\n' accomplish the same thing but in a slightly different way. std::endl causes the output buffer to flush, while '\n' does not. So, for performance reasons it's better to use '\n' but for debugging purposes and code readability, std::endl is better. The performance penalty of std::endl will only matter in cases where you have TONS of stream I/O going on, so for most cases you shouldn't even worry about it.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Well, I like to use endl, as that is the C++ way, but I do know some C++ coders that use \n. I think endl is a lot more easier to read. I mean...take this for instance:
cout<<"/n Hola!";


cout<<"Hola!"<<endl;


A lot easier to read I believe. The average reader might look at that real fast and think \n will be outputted when it woudn't be.


Also, to get code to be put in the white box you do this: [ source] [/ source] (without the spaces). Here's an example.


#include <iostream>int main(){     std::cout<<"Hello World!";     return 0;}



I hoped this helped!


Chad!
Alot of times when you see \n, its because the person doing the programming has a C background. Before C++ had the standard library ( and for all of the time C existed, which C++ is based off ) \n has the only option. Old habits die hard.

Another big advantage to \n is it can be embedded in a string, while << endl; has to be part of a stream.

For example, I could do:

const char * myConstString = "Mary had a little lamb\nLittle lamb\nMary had a little lamb\nWhich she did bad things to\nAnd went to prison";

Where as with streams, you would have to do something like
cout << "Mary had a little lamb" << endl << "Little lamb" << endl << "Little lamb" << endl << "Mary had a little lamb" << endl "which she did bad things to" << "And went to prison";

for the same effect using endl. There are cases where \n is really your only option.

This topic is closed to new replies.

Advertisement