Is it better to use std::cout and std::cin in my programs or can I jsut use cout.
Is this dependant on using namespace std;?
Is it better to use std::cout and std::cin in my programs or can I jsut use cout.
Is this dependant on using namespace std;?
... and ...Code:#include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; }Are essentially the same thing.Code:#include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; }
As BMJ said it's more of a personal preference since they will behave ver similar. Sometimes i prefer to use for instance
The only reason i sometimes prefer to use this scheme is because i can later on change all logging calls simply by changing the namespace for some other namespace as long as it has clog also defined. so by changing:Code:#include <iostream> using std::clog; using std::endl; int main() { clog << "Logging feature" << endl; return 0; }
toCode:using std::clog;
I can override all calls to clog and change the logging features without interefering with std::endl or any other std:: definitions. This allows a per-file basis changes without having to go through the whole file changing the clog calls. This is obviously useful only sometimes and always as a personal preference.Code:using my::clog;
Hope this helps a tad, cheers.
seems to be easier, because you type less. But in the end, I think it's whatever you're comfortable with.Originally Posted by BMJ
I prefer to put std:: on everything and never use a using directive. The reason the namespace was created was to avoid naming conflicts. If you are always putting in the using directive, then you lose the benefits of the namespace. In some cases it does matter, so I prefer to be consistent and use std:: everywhere. It also makes it clear that you are using a standard library name versus a hand implementation or something from another library.
For beginner programs it rarely matters, but if you intend on becoming more than a beginning programmer consider avoiding the using directive and making the effort to type std::.
Using just cout will only work if you have the 'using namespace std;' line before main(). It has to do with a topic called 'namespaces' which you will learn about later. It requires much less typing if you include 'using namespace std;' and just use 'cout', 'cin', and 'endl', so for now just continue doing that.Is it better to use std::cout and std::cin in my programs or can I jsut use cout.
Is this dependant on using namespace std;?
Last edited by 7stud; 04-02-2006 at 09:48 PM.
I would go with the process LinuxCoder suggested - it's flexible and there's probably less of a chance that unwanted object code (from header files) will end up in the final result.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
There are times when you don't want to have a using directive in force (whether a using namespace std; or "using std::cout;" or similar). For example, one way to tie yourself (and others using your code) into future knots of ambiguity [code that won't compile due to there being multiple candidates for a name like cout] is to place a "using namespace std;" directive into a header file. So, if you want to use cout within a header file (eg within inline member functions of a class), then avoid the using directive and call the object std::cout.
I rarely use the 'using namespace <>' method. When looking at my code I can certainly tell that vector, list, map, etc, etc. are in namespace std. Without using the form std::vector, std::list, etc, etc, it can get confusing later. Just a personal preference.
How about "/n" and endl?
Which is best?
Do you mean '\n' and std::endl? Again, it doesn't really matter. Use \n when you don't need to explicitly flush the buffer (most of the time). Use endl when you do want to add a newline and flush the buffer. Since the buffer should be automatically flushed if you use cin to wait for input, or when the program closes, you don't need the endl explicitly in those cases.
I break this rule by using endl a lot when '\n' would work. I'm more used to it and I think it is clearer, and like I said, it doesn't really matter.
I think my book called it Simple Polymorphism when you use the same function name with different parameters...do you need to write a different definition and code?
Yes.