← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 5 of 15

C++ में pair और tuple

एक pair दो values को साथ रखता है और एक tuple कई रखता है, कुछ चीज़ों से labeled एक छोटे bag की तरह। ये एक से ज़्यादा answer return करने के लिए handy हैं।
Syntax
cpp
std::pair<type1, type2> pair_name = {value1, value2};
pair_name.first;
pair_name.second;

std::tuple<type1, type2, type3> tuple_name = {v1, v2, v3};
std::get<index>(tuple_name);

std::pair के साथ काम करना

std::pair एक simple container है जो exactly दो values को एक single unit की तरह साथ store करता है, और उन दो values को पूरी तरह अलग data types का होने की permission है — आमतौर पर एक key को इसकी associated value के साथ pair करने जैसी चीज़ों के लिए इस्तेमाल होता है।

उदाहरण: Working with std::pair

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

// Program execution starts in main()
int main() {
	std::pair<std::string, int> entry("age", 30);
	// Print to the console with cout
	std::cout << entry.first << " " << entry.second << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

std::tuple के साथ काम करना

std::tuple std::pair को किसी भी fixed संख्या की values रखने के लिए generalize करता है, हर एक potentially एक अलग type की, आपको एक one-off case के लिए एक dedicated struct define किए बिना दो से ज़्यादा related pieces of data साथ group करने देते हुए।

उदाहरण: Working with std::tuple

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

// Program execution starts in main()
int main() {
	std::tuple<int, std::string, double> record(1, "Alex", 3.5);
	// Print to the console with cout
	std::cout << std::get<0>(record) << " " << std::get<1>(record) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

std::tie से Unpacking

आप std::tie इस्तेमाल करके एक tuple के individual elements को सीधे separate named variables में unpack कर सकते हैं, जो कई values return करने वाले एक function के साथ काम करना ordinary multiple return values के साथ काम करने जितना almost naturally महसूस कराता है।

उदाहरण: Unpacking with std::tie

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

// Program execution starts in main()
int main() {
	std::tuple<int, int> point(3, 4);
	int x, y;
	std::tie(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;
}

Pairs और Tuples Compare करना

C++ relational operators से pairs और tuples को सीधे compare करना support करता है; comparison lexicographically काम करता है, पहले first elements check करते हुए और सिर्फ तभी अगला element compare करने के लिए आगे बढ़ते हुए जब पहले वाले equal हों।

उदाहरण: Comparing Pairs and Tuples

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

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

Lists के अंदर Chaining

Related records को compactly साथ group करने के लिए pairs अक्सर एक vector के अंदर store किए जाते हैं — उदाहरण के लिए, (name, score) pairs का एक vector — आपको एक custom struct के बिना ordinary STL algorithms इस्तेमाल करके पूरा collection sort या search करने देते हुए।

उदाहरण: Chaining within Lists

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

// Program execution starts in main()
int main() {
	std::vector<std::pair<std::string, int>> scores = {{"Bob", 80}, {"Alice", 95}};
	std::sort(scores.begin(), scores.end());
	// Print to the console with cout
	std::cout << scores[0].first << 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. एक std::tuple पर .first और .second इस्तेमाल करना, जो इसकी बजाय std::get<0>(t) इस्तेमाल करता है।
  2. std::tuple इस्तेमाल करने से पहले #include <tuple> भूल जाना।
  3. यह मान लेना कि std::tie values copy करता है, जब यह references बनाता है।

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.