इस ट्यूटोरियल में हम C++ STL लिस्ट में एलिमेंट्स डिलीट करने के तरीके पर चर्चा करेंगे। इसके लिए हम pop_back() और pop_front() फ़ंक्शन्स का उपयोग करेंगे जो क्रमशः अंतिम और पहले एलिमेंट को डिलीट करते हैं।
उदाहरण
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> list1 = {10, 15, 20, 25, 30, 35};
cout << "The original list is : ";
for (list<int>::iterator i = list1.begin(); i != list1.end(); i++)
cout << *i << " ";
cout << endl;
// पहला एलिमेंट डिलीट करना
list1.pop_front();
cout << "The list after deleting first element using pop_front() : ";
for (list<int>::iterator i = list1.begin(); i != list1.end(); i++)
cout << *i << " ";
cout << endl;
// अंतिम एलिमेंट डिलीट करना
list1.pop_back();
cout << "The list after deleting last element using pop_back() : ";
for (list<int>::iterator i = list1.begin(); i != list1.end(); i++)
cout << *i << " ";
cout << endl;
return 0;
}
आउटपुट
The original list is : 10 15 20 25 30 35 The list after deleting first element using pop_front() : 15 20 25 30 35 The list after deleting last element using pop_back() : 15 20 25 30