C++ के Structured Bindings
In this page:
auto [name1, name2] = pair_or_struct;
Structured Binding क्या है?
Structured bindings, जो C++17 में introduce हुई, आपको एक struct, pair, tuple, या array से कई values को एक single declaration में individually named variables में unpack करने देती हैं, dot या index syntax से हर member एक बार में access करने की बजाय।
उदाहरण: What is Structured Binding?
// Include std::cout and std::cin
#include <iostream>
#include <utility>
// Program execution starts in main()
int main() {
std::pair<int, int> point(3, 4);
auto [x, y] = point;
// Print to the console with cout
std::cout << x << "," << y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Struct Members से Bind करना
एक struct के लिए, syntax auto [a, b] = myStruct; इसके public members को declaration order में variables a और b में unpack करता है। brackets के अंदर names की संख्या exactly unpack हो रहे members की संख्या से match होनी चाहिए, नहीं तो compiler इसे reject कर देता है।
उदाहरण: Binding to Struct Members
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point { int x; int y; };
// Program execution starts in main()
int main() {
Point p{3, 4};
auto [a, b] = p;
// Print to the console with cout
std::cout << a << "," << b << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Arrays से Bind करना
same bracket syntax एक fixed-size C-style array पर काम करता है -- auto [x, y, z] = myArray; इसके तीनों elements को separate variables में unpack करता है, जो अक्सर number से बार-बार array index करने से ज़्यादा readable होता है।
उदाहरण: Binding to Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[3] = {1, 2, 3};
auto [x, y, z] = arr;
// Print to the console with cout
std::cout << x << " " << y << " " << z << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bindings में References Use करना
Default से, structured bindings हर unpacked value को इसके नए variable में copy करती हैं, इसलिए bound variable में changes का original struct या array पर कोई असर नहीं होता।
Binding को एक reference declare करना -- auto& [a, b] = myStruct; -- इसकी बजाय सीधे original members से bind करता है, इसलिए modifications वापस propagate होते हैं।
उदाहरण: Using References in Bindings
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point { int x; int y; };
// Program execution starts in main()
int main() {
Point p{1, 2};
auto &[x, y] = p;
x = 100;
// Print to the console with cout
std::cout << p.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
const References Use करना
जब आप बिना copy किए values unpack करना चाहें लेकिन यह भी guarantee करना चाहें कि इन्हें accidentally modify नहीं किया जा सकता, const auto& [a, b] = myStruct; आपको एक read-only reference binding देता है -- जब आप सिर्फ एक बड़े struct के fields inspect कर रहे हों तब best choice।
उदाहरण: Using const References
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point { int x; int y; };
// Program execution starts in main()
int main() {
Point p{5, 6};
const auto &[x, y] = p;
// Print to the console with cout
std::cout << x << " " << y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- members के लिए गलत संख्या names इस्तेमाल करना।
- जब आप original modify करना चाहें तो
&भूल जाना। - एक ऐसे type से bind करने की कोशिश करना जो इसे support न करे।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: