C++ allows us to use standard library features without declaring using namespace std;. We can access these features by qualifying their names with the std:: prefix.
- std:: specifies that a name belongs to the standard namespace.
- It helps avoid naming conflicts with other identifiers.
Example:
Instead of writing: cout << "GeeksforGeeks";
we can write: std::cout << "GeeksforGeeks";
Approach
The approach is to remove using namespace std; and explicitly qualify standard library names with std::.
- Include the required standard library header.
- Remove the using namespace std; statement.
- Add std:: before standard library names such as cout and endl.
Example: The following program prints a message without using using namespace std;.
#include <iostream>
#include <string>
int main()
{
std::cout << "geeksforgeeks";
return 0;
}
Output
geeksforgeeks
Example: The std:: prefix can also be used with standard library functions and objects when needed.
#include <bits/stdc++.h>
int main()
{
int a = 5;
int b = 10;
int c = a + b;
std::cout << c << std::endl;
return 0;
}
Output
15