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

C++ में set और multiset

एक set unique items का एक bag है जो sorted order में रखा गया है, अलग-अलग stickers की एक line की तरह। एक multiset repeats भी allow करता है।
Syntax
cpp
#include <set>

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

set क्या है?

std::set unique elements को sorted order में store करता है, एक balanced binary search tree की तरह implemented। यह तब useful है जब आपको बिना duplicates और fast ordered lookups वाला एक collection चाहिए, O(log n) time में।

उदाहरण: What is a set?

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

// Program execution starts in main()
int main() {
	std::set<int> nums = {3, 1, 2};
	for (int n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Insert और Erase करना

insert() एक element add करता है और एक pair return करता है जो indicate करता है कि यह newly insert हुआ या नहीं। erase() value या iterator से एक element remove करता है।

क्योंकि एक set automatically uniqueness enforce करता है, एक duplicate value insert करना बस कुछ नहीं करता और set unchanged छोड़ता है।

उदाहरण: Inserting and Erasing

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

// Program execution starts in main()
int main() {
	std::set<int> nums;
	nums.insert(5);
	nums.insert(5);
	nums.erase(5);
	// Print to the console with cout
	std::cout << nums.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

set बनाम multiset

std::set duplicate inserts चुपचाप ignore करता है। std::multiset कई equal elements allow करता है, सबको sorted order में store करते हुए, repeats वाला (जैसे sorted scores) एक sorted collection रखने के लिए useful।

उदाहरण: set vs multiset

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

// Program execution starts in main()
int main() {
	std::set<int> s;
	s.insert(5);
	s.insert(5);
	std::multiset<int> ms;
	ms.insert(5);
	ms.insert(5);
	// Print to the console with cout
	std::cout << s.size() << " " << ms.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक set पर Iterate करना

एक set iterate करना automatically elements को ascending sorted order में visit करता है, बिना किसी explicit sort step की ज़रूरत के। यह ordering guarantee ही std::set को std::unordered_set से अलग करता है, जो ऐसी कोई guarantee नहीं offer करता।

उदाहरण: Iterating over a set

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

// Program execution starts in main()
int main() {
	std::set<int> nums = {5, 1, 3};
	for (int n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक set Search करना

find() element का एक iterator return करता है या अगर missing हो तो end()। count() एक set के लिए 0 या 1 return करता है (या एक multiset के लिए matches की संख्या)। दोनों O(log n) में चलते हैं।

क्योंकि count() एक plain set के लिए कभी सिर्फ 0 या 1 ही return कर सकता है, कई developers clearer, non-boolean-looking intent के लिए find() पसंद करते हैं।

उदाहरण: Searching a set

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

// Program execution starts in main()
int main() {
	std::set<int> nums = {1, 2, 3};
	// Declare it and set it to nums.find(2)
	auto it = nums.find(2);
	// Print to the console with cout
	std::cout << (it != nums.end()) << " " << nums.count(2) << 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. यह उम्मीद करना कि set duplicates रखेगा, जब duplicates ignore होते हैं (इसके लिए multiset इस्तेमाल करें)।
  2. in place एक element modify करने की कोशिश करना, जब set elements const हैं।
  3. यह भूल जाना कि एक set sorted order में store होता है, insertion order में नहीं।

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.