← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 13 of 15

C++ में using namespace std

using namespace std std namespace से हर नाम, string और cout समेत, बिना qualify किए scope में ले आता है, shorter, ज़्यादा readable code के लिए naming conflicts का एक छोटा risk trade करते हुए।
Syntax
cpp
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?

cpp
// 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;
}

using namespace std Directive

एक file के top के पास एक बार using namespace std लिखना std से हर नाम current scope में ला देता है, बाद में cout, string, और दूसरों को std:: prefix के बिना लिखने देते हुए।

उदाहरण: The using namespace std Directive

cpp
// 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;
}

Specifically Strings के लिए Prefix से बचना

पूरा std namespace import करने की बजाय, अकेला using std::string सिर्फ string नाम ले आता है, जो बाकी हर std नाम खींचे बिना code छोटा करने का एक ज़्यादा targeted तरीका है।

उदाहरण: Avoiding the Prefix for Strings Specifically

cpp
// 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;
}

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

cpp
#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;
}

बिना Prefix के Naming Conflicts

पूरा std namespace import करना एक naming conflict का risk रखता है अगर program std में किसी चीज़ के same नाम से अपना खुद का identifier define करे, क्योंकि compiler फिर बता नहीं सकता किसका मतलब है।

उदाहरण: Naming Conflicts Without a Prefix

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. using namespace std; को एक header file में रखना, जो इसे include करने वाली हर file को pollute करता है।
  2. using namespace std; इस्तेमाल करना और अपना खुद का count define करना, जो std::count से clash करता है।
  3. एक header में std:: भूल जाना, ताकि names undefined हों।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.