C++ में pair और tuple
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Pairs और Tuples Compare करना
C++ relational operators से pairs और tuples को सीधे compare करना support करता है; comparison lexicographically काम करता है, पहले first elements check करते हुए और सिर्फ तभी अगला element compare करने के लिए आगे बढ़ते हुए जब पहले वाले equal हों।
उदाहरण: Comparing Pairs and Tuples
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Lists के अंदर Chaining
Related records को compactly साथ group करने के लिए pairs अक्सर एक vector के अंदर store किए जाते हैं — उदाहरण के लिए, (name, score) pairs का एक vector — आपको एक custom struct के बिना ordinary STL algorithms इस्तेमाल करके पूरा collection sort या search करने देते हुए।
उदाहरण: Chaining within Lists
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक
std::tupleपर.firstऔर.secondइस्तेमाल करना, जो इसकी बजायstd::get<0>(t)इस्तेमाल करता है। std::tupleइस्तेमाल करने से पहले#include <tuple>भूल जाना।- यह मान लेना कि
std::tievalues copy करता है, जब यह references बनाता है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: