इस लेख में हम C++ STL में set::upper_bound() पर चर्चा करेंगे, उनका सिंटैक्स, कार्य और उनके रिटर्न मान।
C++ STL में Set क्या है?
C++ STL में Sets ऐसे कंटेनर हैं जिनमें सामान्य क्रम में अद्वितीय तत्व होने चाहिए। Sets में अद्वितीय तत्व होने चाहिए क्योंकि तत्व का मान तत्व की पहचान करता है। एक बार सेट कंटेनर में मान जोड़ने के बाद बाद में संशोधित नहीं किया जा सकता, हालांकि हम अभी भी सेट में मानों को हटा या जोड़ सकते हैं। Sets का उपयोग बाइनरी सर्च ट्री के रूप में किया जाता है।
set::upper_bound() क्या है?
upper_bound() C++ STL में एक अंतर्निहित फ़ंक्शन है जिसे <set> हेडर फ़ाइल में घोषित किया गया है। upper_bound() उस मान की ऊपरी सीमा पर एक इटरेटर लौटाता है जिसकी ऊपरी सीमा हम खोजना चाहते हैं। फ़ंक्शन उस मान के तत्काल अगले तत्व की ओर इंगित करने वाला इटरेटर लौटाता है जिसकी ऊपरी सीमा हम खोजना चाहते हैं।
सिंटैक्स
name_of_set.upper_bound(const type_t& value);
पैरामीटर
यह फ़ंक्शन एक पैरामीटर स्वीकार करता है, यानी वह मान जिसकी ऊपरी सीमा खोजी जानी है।
रिटर्न वैल्यू
यह फ़ंक्शन उस तत्व की ओर इंगित करने वाला एक इटरेटर लौटाता है जो मान से तत्काल बड़ा है।
उदाहरण
Input: set<int> myset = {1, 2, 3, 4, 5}; Myset.upper_bound(3);
Output: Upper bound = 4
उदाहरण
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> Set;
Set.insert(9);
Set.insert(7);
Set.insert(5);
Set.insert(3);
Set.insert(1);
cout << "Elements are : ";
for (auto i = Set.begin(); i != Set.end(); i++)
cout << *i << " ";
auto i = Set.upper_bound(5);
cout << "\nupper bound of 5 in the set is: ";
cout << (*i) << endl;
i = Set.upper_bound(1);
cout << "upper bound of 1 in the set is: ";
cout << (*i) << endl;
return 0;
}
आउटपुट
यदि हम उपरोक्त कोड चलाते हैं तो यह निम्नलिखित आउटपुट उत्पन्न करेगा:
upper bound of 5 in the set is: 7 upper bound of 1 in the set is: 3
उदाहरण
#include <iostream>
#include <set>
int main () {
std::set<int> Set;
std::set<int>::iterator one, end;
for (int i = 1; i < 10; i++)
Set.insert(i * 10);
one = Set.lower_bound (20);
end = Set.upper_bound (40);
Set.erase(one, end); // 10 20 70 80 90
std::cout << "Elements are: ";
for (std::set<int>::iterator i = Set.begin(); i != Set.end(); ++i)
std::cout << ' ' << *i;
std::cout << '\n';
return 0;
}
आउटपुट
यदि हम उपरोक्त कोड चलाते हैं तो यह निम्नलिखित आउटपुट उत्पन्न करेगा:
Elements are : 10 50 60 70 80 90