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

STL C++ algorithm


 //STL ALGORITHM APPLICATION ON SEQUENCE CONTAIERS  
 #include <bits/stdc++.h>  
 using namespace std;  
 int main()  
 {  
  vector<char>vc;  
  vc.push_back('a');  
  vc.push_back('b');  
  vc.push_back('c');  
  vc.push_back('d');  
  //first occurrence and iterator  
  vector<char>::iterator it = find(vc.begin(),vc.end(),'c');  
  cout<<"c is at position "<<it - vc.begin() + 1<<endl;  
  vc.push_back('c');  
  vc.push_back('c');  
  vc.push_back('c');  
  //Counting number of appearances  
  int cnt = count(vc.begin(),vc.end(),'c');  
  cout<<"c appeared "<<cnt<<" times\n";  
  //Searching a subsequence  
  vector<int>v_prime;  
  v_prime.push_back(3);  
  v_prime.push_back(4);  
  v_prime.push_back(5);  
  v_prime.push_back(6);  
  v_prime.push_back(7);  
  v_prime.push_back(8);  
  v_prime.push_back(9);  
  vector<int>v_sec_1;  
  v_sec_1.push_back(7);  
  v_sec_1.push_back(8);  
  v_sec_1.push_back(9);  
  vector<int>v_sec_2;  
  v_sec_2.push_back(3);  
  v_sec_2.push_back(5);  
  v_sec_2.push_back(6);  
  //An iterator to the first element of the first occurrence of [first2,last2) in [first1,last1).  
  //If the sequence is not found, the function returns last1.  
  //SEARCH   S FOR SUBSEQUENCE  
  if(search(v_prime.begin()+2,v_prime.end(),v_sec_1.begin(),v_sec_1.end())!=v_prime.end())  
   cout<<"Found sequence\n";  
   else  
   cout<<"Not Found sequence\n";  
  if(search(v_prime.begin()+2,v_prime.end(),v_sec_2.begin(),v_sec_2.end())!=v_prime.end())  
   cout<<"Found sequence\n";  
  else  
   cout<<"Not Found sequence\n";  
   cout<<*min_element(v_prime.begin(),v_prime.end())<<endl;  
   return 0;  
 }  

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

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