শনিবার, ২৬ সেপ্টেম্বর, ২০১৫

C++ STL Vector

Vector template :



 
 //TEMPLATE PROGRAM FOR STL SEQUENCE CONTAINER Vector  
 #include <bits/stdc++.h>  
 using namespace std;  
 ///vector : p_v(vector) print_vector(vector)  
 #define p_v_i(v) for(vector<int>::iterator it = v.begin();it!=v.end();it++)cout<<*it<<" ";cout<<endl;  
 #define p_v_b(v) for(vector<bool>::iterator it = v.begin();it!=v.end();it++)cout<<*it<<" ";cout<<endl;  
 ///vector : saa_v(vector_to_be_searched,value); search_all_appearances_vector(vector_to_be_searched,value)  
 // searches all appearances of value in the vector and returns them in a vector  
 /*  
 template<typename T>  
 vector<int> sas_v(vector<T>vT,T val)  
 {  
   vector<int>il; //index_list  
   for(int a=0; a<vT.size(); a++)  
   {  
     if(vT[a]==val)  
       il.push_back(a); //0 based indexing  
   }  
   return il;  
 }  
 */  
 template<typename T>  
 vector<int> sas_v(vector<T>vT,T val)  
 {  
   vector<int>il; //index_list  
   typename vector<T>::iterator it; //See how used typename infront of it declaration  
   for(it = vT.begin(); it!=vT.end(); it++)  
   {  
     if(*it==val)  
       il.push_back(it-vT.begin()); //0 based indexing  
   }  
   return il;  
 }  
 int main()  
 {  
   vector<int> v1; //Don't use v1() empty constructors . They are evil!!!  
   vector<int> v2(10,-5); //10 elements with value -5  
   vector<int> v3(v2.begin(),v2.end());  
   vector<int>::iterator it = v2.begin()+2;  
   vector<int> v4(it , v2.end()); //Even though I added 2 elements to v2 later But those were not added to v4  
   vector<int> v5(v4); //copy if v4  
   v2.push_back(3);  
   v2.push_back(7);  
 //v1 should be empty  
   if(v1.empty())  
     cout<<"v1 is empty!\n";  
 //traversing a vector sequentially using iterators  
   for(vector<int>::iterator it = v2.begin(); it!=v2.end(); it++)  
     cout<<*it<<" ";  
   cout<<endl;  
   for(vector<int>::iterator it = v3.begin(); it!=v3.end(); it++)  
     cout<<*it<<" ";  
   cout<<endl;  
 //traversing a vector sequentially using index [array]  
   for(int a=0; a<v4.size(); a++)  
     cout<<v4[a]<<" ";  
   cout<<endl;  
   for(int a=0; a<v5.size(); a++)  
     cout<<v5[a]<<" ";  
   cout<<endl;  
 //Searching 1 : binary search is effective if only the sureness of existence of a value is needed not the position  
 //or the number of appearance of the value  
 ///WORKS ONLY ON SORTED SEQUENCE  
   sort(v2.begin(),v2.end()); //funny the vector was already in the sorted form  
   bool bs_6_v2 = binary_search(v2.begin(),v2.end(),6);  
   if(bs_6_v2)  
     cout<<"6 found in v2\n";  
   else  
     cout<<"6 not found in v2\n";  
   bool bs_3_v2 = binary_search(v2.begin(),v2.end(),3);  
   if(bs_3_v2)  
     cout<<"3 found in v2\n";  
   else  
     cout<<"3 not found in v2\n";  
   bool bs_3_v2_er = binary_search(v2.begin(),v2.end() - 2,3); //Edited Range : See how range works here with iterators  
   if(bs_3_v2_er)  
     cout<<"3 found in edited range of v2 \n";  
   else  
     cout<<"3 not found in edited range of v2\n";  
   //So from this edited range we get that begin points to first element begin + 1 points to second element  
   //end points to last element . end - 1 points to the second last element  
   //Searching 2 : If I also need the position of the first appearance then can use find  
   vector<int>::iterator it2 = find(v2.begin(),v2.end()-1 , 3);  
   if(it2!=v2.end()-1)  
   {  
     cout<<"3 found at position "<<it2 - v2.begin() + 1<<endl;  
   }  
   else  
     cout<<"3 not found."<<endl;  
 //Searching 3 : If I need all the position of the appearance  
   int val = 3;  
   vector<int>aa = sas_v(v2,val); //all_appearance  
   cout<<"All the appearance Of "<<val<<" :";  
   for(vector<int>::iterator it = aa.begin(); it!=aa.end(); it++)  
     cout<<*it<<" ";  
   cout<<endl;  
   v2.push_back(-2);  
   v2.push_back(999);  
   //sorting min to max  
   sort(v2.begin(),v2.end());  
   p_v_i(v2);  
   //sorting max to min  
   sort(v2.rbegin(),v2.rend());  
   p_v_i(v2);  
 ///Reversing elements of vector  
 ///Comment : You don't need to reverse just when traversing go from rbegin to rend  
   for(vector<int>::iterator it = v2.end()-1; it>=v2.begin(); it--)//See the ranges  
     cout<<*it<<" ";  
   cout<<endl;  
 ///Inserting an element in the middle O(n)  
   vector<int>::iterator pos = v2.begin() + 5 ; //inserting element at pos 6 [if first element has pos 1]  
   v2.insert(pos,456);  
   p_v_i(v2);  
   cout<<endl;  
 ///Erasing an element from the middle O(n)  
   vector<int>::iterator n_pos = v2.begin() + 5 ; //inserting element at pos 6 [if first element has pos 1]  
   v2.erase(n_pos); // 1 deleted  
   p_v_i(v2);  
   cout<<endl;  
   v2.erase(n_pos,v2.end()-3); //all deleted from pos 6 to last_pos - 3  
   p_v_i(v2);  
   cout<<endl;  
   v2.clear(); //O(n) Generally not used ! Use if same vector is used more than once in different test cases for data storage  
   ///Vector of bools  
   vector<bool>vb;  
   vb.push_back(true);  
   vb.push_back(false);  
   vb.push_back(false);  
   vb.push_back(true);  
   vb.push_back(false);  
   p_v_b(vb);  
   vb.flip(); //flipping all bits;  
   p_v_b(vb);  
   return 0;  
 }  

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন