C++ में set और multiset
In this page:
#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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
set बनाम multiset
std::set duplicate inserts चुपचाप ignore करता है। std::multiset कई equal elements allow करता है, सबको sorted order में store करते हुए, repeats वाला (जैसे sorted scores) एक sorted collection रखने के लिए useful।
उदाहरण: set vs multiset
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
setduplicates रखेगा, जब duplicates ignore होते हैं (इसके लिएmultisetइस्तेमाल करें)। - in place एक element modify करने की कोशिश करना, जब set elements const हैं।
- यह भूल जाना कि एक set sorted order में store होता है, insertion order में नहीं।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: