The short answer
using namespace std; lets you write names from the C++ standard library without the std:: prefix. After the line, cout, string and vector work the same way as std::cout, std::string and std::vector.
The line changes how the compiler looks up names. It does not add code to your program, and it does not change what the program prints.
A before and after example
Here is a program that uses the full names:
#include <iostream>
#include <string>
int main() {
std::string name = "Sam";
std::cout << "Hello, " << name << "\n";
}
Here is the same program with the directive:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Sam";
cout << "Hello, " << name << "\n";
}
Both print Hello, Sam followed by a newline.
What the directive changes
Without the directive, a bare cout is an error, because the compiler only looks in the global scope for it. With the directive in place, the compiler also searches std, and it finds std::cout.
The directive applies from the line onward, to the rest of the file. If you place it inside a function, the effect stays inside that function.
What the directive does not change
- Program speed. The lookup happens at compile time, so the directive has no effect on how fast the program runs.
- Program output. The program prints the same text as it would with the full names.
- The namespace itself.
stdstill holds the names. The directive only makes them easier to reach.
Using a single name instead
A using declaration brings in one name and nothing else:
#include <iostream>
#include <string>
using std::cout;
int main() {
cout << "Hello, " << std::string("Sam") << "\n";
}
Here cout is brought in by name, but std::string still needs its prefix.
The c using keyword is a different feature
The word using has a second job. The c using keyword defines type aliases, such as using Name = std::string;, which gives an existing type a new name. It looks similar to the directive, but it does a different job.
Where the debate matters
For a short practice program, the directive is a reasonable choice. The debate is mostly about shared code and headers. Name clashes and leaked directives cause real trouble there, because other people’s code is affected. That is where most developers draw the line.