STL में List क्या है
List कंटेनर हैं जो अनुक्रम में कहीं भी निरंतर समय सम्मिलन और विलोपन की अनुमति देते हैं। List को डबल लिंक्ड सूचियों के रूप में कार्यान्वित किया जाता है। List गैर-सन्निकट मेमोरी आवंटन की अनुमति देते हैं। List सरणी, वेक्टर और deque की तुलना में किसी भी स्थिति में तत्व के सम्मिलन, निष्कर्षण और स्थानांतरण में बेहतर प्रदर्शन करते हैं। List में तत्व तक सीधी पहुंच धीमी होती है और list forward_list के समान होती है, लेकिन forward list ऑब्जेक्ट एकल लिंक्ड सूचियाँ होती हैं और उन्हें केवल आगे की ओर पुनरावृत्त किया जा सकता है।
unique() क्या है
list unique() का उपयोग सूची में सभी डुप्लिकेट तत्वों को हटाने के लिए किया जाता है।
सिं्टैक्स
list_name.unique(binarypredicate name)
बाइनरी प्रेडिकेट के लिए सिं्टैक्स
Bool name(data_type a, data_type b)
पैरामीटर
यह फ़ंक्शन एक पैरामीटर स्वीकार करता है जो एक बाइनरी प्रेडिकेट है जो true लौटाता है यदि तत्वों को समान माना जाना चाहिए।
उदाहरण
इनपुट List - 2 2 6 7 9 9 9 10 5 5
आउटपुट नया List - 2 5 6 7 9 10
इनपुट List - 3.14 5.56 7.62 9.00 0.45 7.62 9.00 7.62 0.45 3.00
आउटपुट नया List - 0.45 3.00 3.14 5.56 7.62 9.00
अनुसरण किया जाने वाला दृष्टिकोण
- पहले हम बाइनरी प्रेडिकेट फ़ंक्शन बनाते हैं।
- फिर हम सूची प्रारंभ करते हैं।
- फिर हम unique() फ़ंक्शन परिभाषित करते हैं।
- फिर हम unique ऑपरेशन के बाद सूची प्रिंट करते हैं।
उपरोक्त दृष्टिकोण का उपयोग करके हम सूची से डुप्लिकेट तत्व हटा सकते हैं।
उदाहरण
/ / C++ code to demonstrate the working of list unique( ) function in STL #include <iostream.h> #include<list.h> Using namespace std; / / function for binary predicate Bool cmp(int a, int b){ Return (abs(a) == abs(b)) } int main ( ){ List<int> list = { 13, 14, 13, 19, 20, 19, 15, 19, 20, 15, 15 }; / / print the list cout<< " Elements in List: "; for( auto x = List.begin( ); x != List.end( ); ++x) cout<< *x << " "; / / declaring unique( ) function list.unique(cmp); / / printing new list after unique operation cout<< "List after unique operation: "; for( x=list.begin( ); x != list.end( ); ++x) cout<< " " <<*x; return 0; }आउटपुट
यदि हम उपरोक्त कोड चलाते हैं तो यह निम्न आउटपुट उत्पन्न करेगा
Input - Element in List : 13 14 13 19 20 19 15 19 20 15 Output - List after unique operation : 13 14 15 19 20
उदाहरण
/ / C++ code to demonstrate the working of list unique( ) function in STL #include<iostream.h> #include<list.h> Using namespace std; / / function for binary predicate Bool cmp(float a, float b){ Return (abs(a) == abs(b)) } int main ( ){ List <float>t; list = { 3.14, 5.56, 7.62, 9.00, 0.45, 7.62, 9.00, 7.62, 0.45, 3.00 }; / / print the list cout<< " Elements in List: "; for( auto x = List.begin( ); x != List.end( ); ++x) cout<< *x << " "; / / declaring unique( ) function list.unique(cmp); / / printing new list after unique operation cout<< "List after unique operation: "; for( x=list.begin( ); x != list.end( ); ++x) cout<< " " <<*x; return 0; }आउटपुट
यदि हम उपरोक्त कोड चलाते हैं तो यह निम्न आउटपुट उत्पन्न करेगा
Input - Element in List: 3.14 5.56 7.62 9.00 0.45 7.62 9.00 7.62 0.45 3.00 Output - List after unique operation: 0.45 3.00 3.14 5.56 7.62 9.00