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

C++ के Structured Bindings

Structured bindings आपको एक group के हिस्सों को एक साथ अलग-अलग named variables में unpack करने देते हैं, एक gift set खोलकर हर item को नाम देने की तरह। यह pairs, structs और arrays के लिए neat है।
Syntax
cpp
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?

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

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

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

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

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

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

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

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

cpp
// 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;
}
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. members के लिए गलत संख्या names इस्तेमाल करना।
  2. जब आप original modify करना चाहें तो & भूल जाना।
  3. एक ऐसे type से bind करने की कोशिश करना जो इसे support न करे।

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.