A namespace is a declarative region that provides a scope to the
identifiers (the names of types, functions, variables, etc) inside
it. Namespaces are used to organize code into logical groups and to
prevent name collisions that can occur especially when your code base
includes multiple libraries
//using namespaces
using namespace std;
//creating namespaces
namespace custom{
class example{
public:
static int method(){
return 0;
}
};
};
//using custom namespaces
using namespace custom;
#include <iostream>
using namespace std;
int main() {
cout<< "Hello World" ;
}
//If you are a web developer, please give https://code.ionicbyte.com/ a try
//Namespaces provide a method for preventing name conflicts in large projects.
//Symbols declared inside a namespace block are placed in a named scope that
//prevents them from being mistaken for identically-named symbols in other
//scopes. Multiple namespace blocks with the same name are allowed.
//All declarations within those blocks are declared in the named scope.
namespace yourName{
//any code you want to put inside
}
#include <iostream>
using namespace std;
namespace square{
int x;
int y;
}
int main(){
using namespace square;
x = 10;
y = 0;
cout << x << y << endl;
}
Namespaces avoids name collisions bacause of large libraray in c++.
This feature was not supported in C
Namespace std::cout or cout <<
Code Example |
---|
Cpp :: unordered_set to vector |
Cpp :: Find minimum maximum element CPP |
Cpp :: sort array c++ |
Cpp :: how to empty string c++ |
Cpp :: stack overflow c++ |
Cpp :: c++ contains |
Cpp :: c pre-processor instructions |
Cpp :: what is g++ and gcc |
Cpp :: print pattern and space in cpp |
Cpp :: pure virtual function in c++ |
Cpp :: ascii cpp |
Cpp :: c++ random generator |
Cpp :: after login redirect to dashboard in nuxt |
Cpp :: vector c++ |
Cpp :: c++ convert const char* to int |
Cpp :: c++ auto |
Cpp :: factorial of large number |
Cpp :: C++ String Compare Example |
Cpp :: grep xargs sed |
Cpp :: c++ array pointer |
Cpp :: How to split a string by Specific Delimiter in C/C++ |
Cpp :: sfml keyboard events cpp |
Cpp :: disallowcopy c++ |
Cpp :: cpp oop |
Cpp :: c++ regex count number of matches |
Cpp :: c++ formatting |
Cpp :: cout stack in c++ |
Cpp :: assignment operator with pointers c++ |
Cpp :: int to string C++ Using stringstream class |
Cpp :: minimum characters to make string palindrome |