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

C++ का unordered_set

एक unordered_set बिना किसी particular order के unique items का एक bag है, एक box में फेंके stickers की तरह। यह items बहुत जल्दी ढूंढता है।
Syntax
cpp
#include <unordered_set>

std::unordered_set<data_type> set_name = {value1, value2};
set_name.insert(value);
set_name.erase(value);

unordered_set क्या है?

std::unordered_set बिना किसी guaranteed order के unique elements store करता है, internally एक hash table इस्तेमाल करते हुए। यह average O(1) insertion, deletion, और lookup offer करता है, sorted order की ज़रूरत न होने पर set से तेज़।

उदाहरण: What is unordered_set?

cpp
#include <iostream>
#include <unordered_set>

int main() {
	std::unordered_set<int> s = {3, 1, 2}; // hash table, no guaranteed order
	std::cout << s.size() << std::endl;
	return 0;
}

Insert और Erase करना

insert() और erase() std::set जैसे behave करते हैं लेकिन एक tree की बजाय hash buckets पर operate करते हैं, इसलिए insertion order और iteration order के बीच कोई relationship नहीं है।

यह trade-off average constant-time insertion और lookup के बदले ordering guarantees छोड़ देता है।

उदाहरण: Inserting and Erasing

cpp
#include <iostream>
#include <unordered_set>

int main() {
	std::unordered_set<int> s;
	s.insert(5);
	s.erase(5); // operates on hash buckets, not a tree
	std::cout << s.size() << std::endl;
	return 0;
}

Hashing और Performance

unordered_map की तरह, unordered_set elements को buckets में hash करता है। Average-case operations O(1) हैं, लेकिन एक poor hash या कई collisions worst case O(n) तक degrade हो सकते हैं।

अपने key type के लिए एक अच्छा hash function चुनना या customize करना actually वह average O(1) performance achieve करने के लिए essential है।

उदाहरण: Hashing & Performance

cpp
#include <iostream>
#include <unordered_set>

int main() {
	std::unordered_set<int> s;
	s.insert(42); // hashed into a bucket -- average O(1)
	std::cout << (s.count(42) > 0) << std::endl;
	return 0;
}

Iterating (Unordered)

एक unordered_set iterate करना elements को hash table layout से determined एक unspecified order में visit करता है। यह इसके insertion order से match करने पर depend न करें।

अगर आपको elements एक predictable order में चाहिए, std::set (या manually एक copy sort करना) इसकी बजाय बेहतर choice है।

उदाहरण: Iterating (Unordered)

cpp
#include <iostream>
#include <unordered_set>

int main() {
	std::unordered_set<int> s = {3, 1, 2};
	for (int n : s) { // order determined by hash layout, not insertion or value
		std::cout << n << " ";
	}
	std::cout << std::endl;
	return 0;
}

एक unordered_set Search करना

find() और count() hashing के जरिए average O(1) time में elements locate करते हैं, यही वजह है कि unordered_set बड़े datasets पर fast membership tests के लिए एक common choice है।

यह unordered_set को natural choice बनाता है जब भी goal बस order की परवाह किए बिना 'क्या यह value यहाँ exist करती है' यह देखना हो।

उदाहरण: Searching an unordered_set

cpp
#include <iostream>
#include <unordered_set>

int main() {
	std::unordered_set<int> s = {1, 2, 3};
	if (s.find(2) != s.end()) { // average O(1) via hashing
		std::cout << "Found" << std::endl;
	}
	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. यह उम्मीद करना कि एक unordered_set के elements sorted या insertion order में आएंगे, जब order hashing पर depend करता है।
  2. यह उम्मीद करना कि duplicates store होंगे, जब एक existing value insert करने का कोई असर नहीं होता।
  3. एक hash function और equality operator दिए बिना element की तरह एक custom type इस्तेमाल करना।

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.