As everybody would say, using std:: is usually better than usingnamespace std;.
I got the point on how it works but can you give me a code(using namespace std) that if compiled and run, will produce errors or some ambiguities?
#include <algorithm>
usingnamespace std;
int count;
The real problems don't come up until you've dumped several namespaces into global scope and your compiler is threatening to murder you with errors.
EDIT:
code [...] that if compiled and run, will produce errors or some ambiguities?
Code that is ambiguous will never compile. The defining characteristic of programming languages is that messages written in them have to be unambiguous to be correct.
If there are ambiguities in the code, it will just not compile. It won't give incorrect results because it will just never get that far.
The defining characteristic of programming languages is that messages written in them have to be unambiguous to be correct.
Thanks for correcting. I used to think that those code will continue to compile and run, and the error will just show in the actual process.
Although this help me to acquire new concept, I still find it hard to understand.
#include <iostream>
#include <algorithm>
#include <cstring>
// Count the number of characters before c
int count(constchar* f, constchar* t, char c)
{
size_t i;
for(i = 0; f < t and *f != c; ++i, ++f);
return i;
}
int main()
{
char* line = "Counter Test";
size_t len;
len = count(line, line + strlen(line), 'T');
std::cout << "len = " << len << std::endl;
usingnamespace std; // Poison the global namespace
len = count(line, line + strlen(line), 'T');
std::cout << "len = " << len << std::endl;
return 0;
}