How to omit "using namespace std" in C++?

A namespace in C++ is used to declare a region, which provides scope to identifiers (the names of functions, variables, types, and so on) inside it.

It is common to see the using namespace std line in almost every C++ program. However, there are programs that run without it. We can omit the using namespace std line in our C++ program by using only the std keyword, followed by the scope resolution operator, that is, ::.

Example

The code given below shows us how to create a program that omits the using namespace std line:

#include <iostream>
#include <string>
int main() {
std::string greeting = "Hello World";
std::cout << greeting;
return 0;
}

Code explanation

From the code given above, we can see that we are able to write a code without using the using namespace std line. We can do this by replacing the using namespace std line with the std keyword, followed by the scope resolution operator, that is, ::.

Free Resources