using namespace std

What Does using namespace std Do?

Find out what using namespace std does in C++, how it lets you skip the std:: prefix, what it does not change, and when it is safe to use in real code.

On this page
  1. The short answer
  2. A before and after example
  3. What the directive changes
  4. What the directive does not change
  5. Using a single name instead
  6. The c using keyword is a different feature
  7. Where the debate matters

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. std still 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.

The complete guide to using namespace std

Using Namespace std in C++ Explained
Read the guide

Keep exploring

Guides from other topics

All topics →

The Latest C++ Standard

C++23 is the most recent C++ standard at the time of writing, with C++26 next. Here is what changed, how versions are named, and how to compile for each.

C++ Standards 3 min read