C++ STL में चार सेट कंटेनर्स हैं, जिनमें ऑर्डरिंग, डुप्लिकेट हैंडलिंग, और आंतरिक कार्यान्वयन के आधार पर अंतर है।
तुलना तालिका
| विशेषता | set | multiset | unordered_set | unordered_multiset |
|---|---|---|---|---|
| ऑर्डर | सॉर्टेड (Ascending) | सॉर्टेड (Ascending) | कोई ऑर्डर नहीं (हैश-आधारित) | कोई ऑर्डर नहीं |
| डुप्लिकेट्स | नहीं | हां | नहीं | हां |
| कार्यान्वयन | बैलेंस्ड BST (Red-Black Tree) | बैलेंस्ड BST | हैश टेबल | हैश टेबल |
| सर्च/इन्सर्ट/डिलीट | O(log n) | O(log n) | O(1) औसत, O(n) सबसे खराब | O(1) औसत, O(n) सबसे खराब |
| इटरेटर इनवैलिडेशन | केवल डिलीट किए गए | केवल डिलीट किए गए | रीहैश पर सभी | रीहैश पर सभी |
| मेमोरी ओवरहेड | उच्च (पॉइंटर्स) | उच्च | मध्यम | मध्यम |
1. std::set - यूनिक, सॉर्टेड
#include <iostream>
#include <set>
using namespace std;
int main() {
int data[] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41};
set<int> s;
for (int x : data) s.insert(x);
cout << "set (sorted, unique):\n";
for (int x : s) cout << x << " ";
cout << "\nSize: " << s.size() << " (duplicates removed)\n";
// रेंज क्वेरीज
auto it = s.lower_bound(30); // पहला >= 30
cout << "Lower bound of 30: " << *it << endl;
return 0;
}
आउटपुट
set (sorted, unique): 11 22 23 33 41 44 55 66 77 88 99 Size: 11 (duplicates removed) Lower bound of 30: 33
2. std::multiset - डुप्लिकेट्स अनुमत, सॉर्टेड
#include <iostream>
#include <set>
using namespace std;
int main() {
int data[] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41};
multiset<int> ms;
for (int x : data) ms.insert(x);
cout << "multiset (sorted, duplicates allowed):\n";
for (int x : ms) cout << x << " ";
cout << "\nSize: " << ms.size() << endl;
cout << "Count of 66: " << ms.count(66) << endl;
// equal_range: सभी 66 के इटरेटर्स
auto range = ms.equal_range(66);
cout << "All 66s: ";
for (auto it = range.first; it != range.second; ++it)
cout << *it << " ";
cout << endl;
return 0;
}
आउटपुट
multiset (sorted, duplicates allowed): 11 11 22 22 23 33 41 44 55 66 66 66 77 88 99 Size: 15 Count of 66: 3 All 66s: 66 66 66
3. std::unordered_set - यूनिक, अनऑर्डर्ड (हैश)
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
int data[] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41};
unordered_set<int> us;
for (int x : data) us.insert(x);
cout << "unordered_set (no order, unique):\n";
for (int x : us) cout << x << " ";
cout << "\nSize: " << us.size() << endl;
cout << "Bucket count: " << us.bucket_count() << endl;
cout << "Load factor: " << us.load_factor() << endl;
// कस्टम हैश के लिए
struct PairHash {
size_t operator()(const pair<int,int>& p) const {
return hash<int>()(p.first) ^ hash<int>()(p.second);
}
};
unordered_set<pair<int,int>, PairHash> pairSet;
return 0;
}
आउटपुट
unordered_set (no order, unique): 11 55 22 66 33 44 77 88 99 23 41 Size: 11 Bucket count: 13 Load factor: 0.846154
4. std::unordered_multiset - डुप्लिकेट्स अनुमत, अनऑर्डर्ड
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
int data[] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41};
unordered_multiset<int> ums;
for (int x : data) ums.insert(x);
cout << "unordered_multiset (no order, duplicates):\n";
for (int x : ums) cout << x << " ";
cout << "\nSize: " << ums.size() << endl;
cout << "Count of 11: " << ums.count(11) << endl;
return 0;
}
आउटपुट
unordered_multiset (no order, duplicates): 11 55 22 66 33 22 11 44 77 88 66 99 66 23 41 Size: 15 Count of 11: 2
कौन सा चुनें?
- set: यूनिक एलिमेंट्स, सॉर्टेड ऑर्डर चाहिए, रेंज क्वेरीज (
lower_bound) - multiset: डुप्लिकेट्स चाहिए, सॉर्टेड ऑर्डर चाहिए
- unordered_set: यूनिक एलिमेंट्स, तेज़ लुकअप (O(1)), ऑर्डर नहीं चाहिए
- unordered_multiset: डुप्लिकेट्स चाहिए, तेज़ लुकअप, ऑर्डर नहीं चाहिए
C++20: heterogeneous lookup
set<string, less<>> s; // transparent comparator
s.find("key"); // string ऑब्जेक्ट बनाए बिना खोजता है