Why you shouldn't write `using namespace std;` in C++ and what are the alternatives?
Hello! I started learning C++ and in all tutorials and code examples I constantly see the line `using namespace std;`. I got used to always writing it at the beginning of the program, because without it I have to add `std::` everywhere, which seems inconvenient. For example:
```cpp
#include
#include
#include
using namespace std;
int main() {
vector numbers = {5, 2, 8, 1, 9};
sort(numbers.begin(), numbers.end());
cout << "Sorted: ";
for (int n : numbers) {
cout << n << " ";
}
cout << endl;
return 0;
}
```
But recently I read that experienced programmers advise never to use `using namespace std;` in real projects. Supposedly this can lead to hard-to-find errors. I tried to write code without this directive
No answers yet.
Log in to leave an answer
Blogs