इस लेख में हम C++ में list::end() फंक्शन के कार्य, सिंटैक्स और उदाहरणों पर चर्चा करेंगे।
list::end() क्या है?
list::end() C++ STL में एक इनबिल्ट फंक्शन है जो <list> हेडर फाइल में घोषित है। end() एक इटरेटर लौटाता है जो लिस्ट कंटेनर में अंतिम स्थिति के अगले एलिमेंट को इंगित करता है। यह फंक्शन कंटेनर में किसी एलिमेंट को इंगित नहीं करता।
यह फंक्शन ज्यादातर list::begin() के साथ उपयोग किया जाता है ताकि एक विशेष लिस्ट कंटेनर की रेंज दी जा सके।
सिंटैक्स
list_container.end();
यह फंक्शन कोई पैरामीटर स्वीकार नहीं करता।
रिटर्न मान
यह फंक्शन लिस्ट कंटेनर के अंत से आगे के एलिमेंट का इटरेटर लौटाता है।
उदाहरण 1: इटरेशन के लिए end() उपयोग करना
#include <bits/stdc++.h>
using namespace std;
int main() {
list<int> myList;
myList.push_back(67);
myList.push_back(12);
myList.push_back(32);
myList.push_back(780);
myList.push_back(78);
cout << "elements in the list are:\n";
for (auto j = myList.begin(); j != myList.end(); j++) {
cout << *j << " ";
}
return 0;
}
आउटपुट
elements in the list are: 67 12 32 780 78
उदाहरण 2: हेडर फाइल के साथ सरणी से लिस्ट बनाना
#include <iostream>
#include <list>
int main() {
int values[] = {67, 12, 32, 780, 78};
std::list<int> myList(values, values + 5);
std::cout << "elements in the list are:\n";
for (std::list<int>::iterator i = myList.begin(); i != myList.end(); ++i)
std::cout << ' ' << *i;
return 0;
}
आउटपुट
elements in the list are: 67 12 32 780 78
व्याख्या
end()"पास्ट-द-एंड" इटरेटर लौटाता है - यह किसी वैध एलिमेंट को इंगित नहीं करता।- रेंज-बेस्ड फॉर लूप
for (auto& x : myList)आंतरिक रूप सेbegin()औरend()का उपयोग करता है। cbegin()/cend()कॉन्स्ट इटरेटर्स लौटाते हैं (रीड-ओनली एक्सेस)।