STL list part 2 :
//STL Container list part 2
#include <bits/stdc++.h>
#define p_l_i(l) for(list<int>::iterator it = l.begin();it!=l.end();it++)cout<<*it<<" ";cout<<endl;
using namespace std;
bool r_i_t_i_t(const int& val)
{
if(val>40 && val<90)
return true;
return false;
}
int main()
{
list<int>l1;
list<int>l2;
l1.push_back(34);
l1.push_back(4);
l1.push_back(3);
l1.push_back(41);
l2.push_back(31);
l2.push_back(20);
l2.push_back(91);
l2.push_back(16);
//Merging 2 lists O(n)
//Without sorting
l1.merge(l2);
p_l_i(l1); //Weird ordering
if(l2.empty())
cout<<"l2 is empty!\n";
//merging with sorting
list<int>l3;
list<int>l4;
l3.push_back(34);
l3.push_back(4);
l3.push_back(3);
l3.push_back(41);
l4.push_back(31);
l4.push_back(20);
l4.push_back(91);
l4.push_back(16);
l3.sort(); //this is the min to max sort version
l4.sort(); //this is the min to max sort version
p_l_i(l3);
p_l_i(l4);
l3.merge(l4);
p_l_i(l3);
if(l4.empty())
cout<<"l4 is empty!\n";
//Erasing elements O(1)
//3 4 16 20 31 34 41 91
list<int>::iterator it= l3.begin(); //^
list<int>::iterator it2;
advance(it,4); //advancing iterator // ^ (4 steps ahead from begin)
cout<<*it<<endl;
it2 = l3.erase(it); //3 4 16 20 34 41 91 ....<><><>.... [31] it pointing to 31 still
//but l3 itself doesn't contain 31
p_l_i(l3);
cout<<*it<<endl; //prints 31 invalidated reference
//But it2 prints 34 previous position of 31
cout<<*it2<<endl;
//Can erase a range of elements
list<int>::iterator it3 = l3.begin(); //pointing to 3
list<int>::iterator it4 = l3.end() ; //pointing to next position of 91
it4--; //pointing to 91
it4--; //pointing to 41
advance(it3,3);//pointing to 20
l3.erase(it3,it4); //deleting 20 to 34 (34 is just before 41)
p_l_i(l3);
//removing all elements with a certain value
l3.push_back(3); //3 4 16 41 91 3
p_l_i(l3);
l3.remove(3);
p_l_i(l3); //4 16 41 91
//Can come in handy while pruning
//removing with a condition
l3.remove_if(r_i_t_i_t); //remove if this is true //See the syntax not r_i_t_i_t()
p_l_i(l3);//no elements in (40,90)
//Splicing O(1)
//listX -------transferring-------->>>> listY
//merging whole list
//splice(the iterator of listX where I want to merge listY(or part of it) , name of listY(ref))
//merging just 1 element
//splice( || , || , the iterator of listY pointing to that element)
//merging a range of elements from listY
//splice( || , || , starting iterator , ending iterator )
l4.push_back(-12);
l4.push_back(-98);
l4.push_back(-67);
l4.push_front(445);
l4.push_front(345);
l4.push_front(125);
it4 = l3.begin();
it4++; //pointing to 16
cout<<*it4<<endl; //16
l3.splice(it4,l4); // merging whole list
p_l_i(l3);
if(l4.empty())
cout<<"l4 is now empty\n";
l4.push_back(-12);
l4.push_back(-98);
l4.push_back(-67);
l4.push_front(445);
l4.push_front(345);
l4.push_front(125);
l3.splice(it4,l4,l4.begin());//it4 still pointing to 16 .... just adding 125 in the position of 16
p_l_i(l3);
l4.push_back(999);
l4.push_back(99);
l4.push_back(9);
l3.splice(it4,l4,l4.begin(),l4.end()--); //Still adding 9 post increment ;-)
p_l_i(l3);
l4.push_back(888);
l4.push_back(88);
l4.push_back(8);
l3.splice(it4,l4,l4.begin(),--l4.end()); //Not adding 8 pre increment ;-)
p_l_i(l3);
//Keeping just unique values : Only works on sorted list
l3.sort();
l3.unique();
p_l_i(l3);
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন