← Back to C++ Course | Chapter 16: Modern C++ | Lesson 1 of 9

C++ का auto Keyword

auto compiler को आपके डाले गए data से एक variable का type खुद पता लगाने देता है, एक label की तरह जो खुद भर जाता है। आप कम लिखते हैं, लेकिन type फिर भी fixed रहता है।
Syntax
cpp
auto variable_name = value;  // type is deduced from value
auto *pointer_name = &variable;
auto &reference_name = variable;

Type Inference Basics

auto keyword compiler को बताता है कि एक variable का type इसके initializer से deduce करे, आपके इसे explicitly spell out करने की बजाय।

auto x = 5; x को एक int बनाता है exactly वैसे ही जैसे आपने int x = 5; लिखा हो -- type dynamic या looser नहीं है, यह compile time पर एक बार determine होता है और तब से fixed रहता है।

उदाहरण: Type Inference Basics

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	auto x = 5;
	// Print to the console with cout
	std::cout << x << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Complex Types के साथ auto

auto को exactly control करने के लिए * और & के साथ combine किया जा सकता है कि क्या deduce हो -- auto* p को initializer एक pointer होने की ज़रूरत है, और auto& एक reference type deduce करता है, जो matter करता है जब एक copy और एक reference के बीच का difference program behavior बदल दे।

उदाहरण: auto with Complex Types

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 10
	int value = 10;
	auto *p = &value;
	auto &ref = value;
	// Print to the console with cout
	std::cout << *p << " " << ref << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Loops के साथ auto

Range-based for loops उन सबसे common जगहों में से हैं जहाँ auto दिखता है, क्योंकि एक container का पूरा iterator या element type spell out करना (जैसे std::vector<std::pair<std::string,int>>::iterator) अक्सर लंबा और गलत होना आसान है -- for (auto& item : container) इसे पूरी तरह sidestep करता है।

उदाहरण: auto with Loops

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {1, 2, 3};
	// Range-based for loop: visit every element of nums
	for (auto n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Return Types के लिए auto

एक function के return type को auto declare करना compiler को function body के अंदर return statement से इसे infer करने देता है, template functions के लिए useful जहाँ exact return type otherwise लिखना मुश्किल होता या template parameters पर depend करता।

उदाहरण: auto for Return Types

cpp
// Include std::cout and std::cin
#include <iostream>

auto add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << add(2, 3) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Lambdas के साथ auto

Lambda expressions आमतौर पर auto-declared variables में store होती हैं क्योंकि एक lambda का actual type एक compiler-generated, unnameable closure type है -- आप literally इसका type हाथ से नहीं लिख सकते। auto generic lambda parameters ([](auto x, auto y)) के लिए भी इस्तेमाल होता है, एक lambda को कई argument types accept करने देते हुए।

उदाहरण: auto with Lambdas

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Define a lambda (anonymous function) stored in square
	auto square = [](int x) { return x * x; };
	// Print to the console with cout
	std::cout << square(5) << std::endl;
	// Return 0 to signal that the program finished successfully
	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. बिना initializer के auto x; declare करना, जो एक type deduce नहीं कर सकता।
  2. यह उम्मीद करना कि auto references या const रखेगा, जब यह इन्हें drop करता है जब तक आप auto& न लिखें।
  3. auto को overuse करना ताकि type readers को unclear हो।

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.