C++ का auto Keyword
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- बिना initializer के
auto x;declare करना, जो एक type deduce नहीं कर सकता। - यह उम्मीद करना कि
autoreferences या const रखेगा, जब यह इन्हें drop करता है जब तक आपauto&न लिखें। autoको overuse करना ताकि type readers को unclear हो।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: