C++ में using namespace std
In this page:
using namespace std; // then write cout instead of std::cout
std::name; // without the directive
std Namespace क्या है?
Standard library की classes और functions, जैसे string, cout, और vector, सब std नाम के एक namespace के अंदर रहते हैं, जो उन्हें साथ group करता है और कहीं और defined names से clash करने से बचाता है।
उदाहरण: What is the std Namespace?
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << "cout lives inside the std namespace" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
using namespace std Directive
एक file के top के पास एक बार using namespace std लिखना std से हर नाम current scope में ला देता है, बाद में cout, string, और दूसरों को std:: prefix के बिना लिखने देते हुए।
उदाहरण: The using namespace std Directive
// Include std::cout and std::cin
#include <iostream>
// Lets us write cout instead of std::cout
using namespace std;
// Program execution starts in main()
int main() {
// Print to the console with cout
cout << "No std:: prefix needed now" << endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Specifically Strings के लिए Prefix से बचना
पूरा std namespace import करने की बजाय, अकेला using std::string सिर्फ string नाम ले आता है, जो बाकी हर std नाम खींचे बिना code छोटा करने का एक ज़्यादा targeted तरीका है।
उदाहरण: Avoiding the Prefix for Strings Specifically
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
using std::string;
// Program execution starts in main()
int main() {
string name = "Alex";
// Print to the console with cout
std::cout << name << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Headers में using namespace std से क्यों बचें
एक header file के अंदर using namespace std रखना उस header को include करने वाली हर file पर वह namespace import force करता है, जो directive लिखे जाने की जगह से दूर unexpected naming conflicts cause कर सकता है।
उदाहरण: Why Avoid using namespace std in Headers
#include <iostream>
// A header should never contain "using namespace std;" -- every file that
// includes it would be forced to import the whole namespace too.
int main() {
std::cout << "Prefer std:: explicitly in headers" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
बिना Prefix के Naming Conflicts
पूरा std namespace import करना एक naming conflict का risk रखता है अगर program std में किसी चीज़ के same नाम से अपना खुद का identifier define करे, क्योंकि compiler फिर बता नहीं सकता किसका मतलब है।
उदाहरण: Naming Conflicts Without a Prefix
#include <iostream>
using namespace std;
int count = 5; // fine here, but risks colliding with any "count" from std
int main() {
cout << count << endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
using namespace std;को एक header file में रखना, जो इसे include करने वाली हर file को pollute करता है।using namespace std;इस्तेमाल करना और अपना खुद काcountdefine करना, जोstd::countसे clash करता है।- एक header में
std::भूल जाना, ताकि names undefined हों।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings