1 / 20

stl programming part_2 sujan prodhan Rajshahi university CSE

stl programming part_2 sujan prodhan Rajshahi university CSEstl programming part_2 sujan prodhan Rajshahi university CSEstl programming part_2 sujan prodhan Rajshahi university CSE

Sujan3
Download Presentation

stl programming part_2 sujan prodhan Rajshahi university CSE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sujan prodhan 1 M Contents 1.vector declare: ................................................ 2 2.vector element access.cpp ............................. 2 3.vector access with iterator.cpp: ..................... 3 4.vector user input.cpp ...................................... 3 5.vector input.cpp ............................................. 3 6. pop_back, insert, erase, size, empty.cpp ...... 4 7.vector size define declare.cpp ........................ 5 8. vector Resize.cpp ........................................... 6 10.unique vector.cpp ......................................... 7 11. list.cpp .......................................................... 8 12. list sort, unique, remove .cpp ...................... 9 13. slpice.cpp ................................................... 10 PR0GRAMMING STL 14. string.cpp ................................................... 11 15.string input.cpp ........................................... 12 16. String operation.cpp .................................. 12 WRITTEN BY SUJAN PRODHAN 17.stack.cpp ..................................................... 13 Continue any part of the book under your own name. Offenses punishable by law.Various websites such as GitHub have been used to develop the book.You can visit prodhan2.blogspot.com to know more details about us. 18. queue.cpp .................................................. 15 19.set.cpp ........................................................ 16 20. map.cpp ..................................................... 17 21.map key value search technique 1.cpp ...... 18 22.map key value search technique 2.cpp ...... 19 23. map search fault.cpp ................................. 19 upper bound & lower bound: .......................... 20 COMPUTER SCIENCE AND ENGINEERING In the FIRST PARTI have added examples with explanation of examples. That will help you understand the examples.. 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  2. Sujan prodhan 2 M ফভক্টব্ররর সাইে োোর েেয বযবহার িরা হয়, size() public member function. এটি ফভক্টব্ররর সাইে করটােত িব্রর। complexity O(1) C++ STL & Tricks-1 ফভক্টর empty কিো, তা োোর েেয বযহহার িরা হয় empty() public member function. এটি বুকলয়াে ভযালু করট ত াে িব্রর। ফভক্টর empty হব্রল true otherwise false করট ত াে িব্রর। complexity O(1) C++ এ কিছু build in লাইব্রেরী আব্রছ, যাব্রেরব্রি বলা হয় standard template library. এইসব STL বযবহার িব্রর অব্রেি প্রবব্রলম অব্রেি সহব্রে িব্রর ফেলা যায়। ফযমে ফিাে প্রবব্রলব্রম যকে stack,queue এইগুলা বযবহার িরা লাব্রে, এইগুলার েেয েতুে িব্রর ফিাড ফলখার প্রব্রয়ােে পব্রর ো। stack,queue stl বযবহার িব্ররই প্রবব্রলম সল্ভ িরা যায়। ফভক্টব্ররর element েরমাল এযাব্ররর মত [] অপাব্ররটর কেব্রয় access িরা যায়। তাছাড়া at() public member function কেব্রয়ও ফযব্রিাে পকেশব্রের ভযালু এযািব্রসস ও পকরবততে িরা যায়। vector: ফভক্টর েরমাল এযাব্ররর মতই। এর সুকবধা হল যখে ইব্রে সাইে পকরবততে িরা যায়। 1.vector declare: vector বযবহার িরার েেয header file vector include িরব্রত হব্রব। 2.vector element access.cpp for(int i=0;i<v.size();i++) vector < type > Name; { #include<stdio.h> cout<<v[i]<<" "; //print every element of vector #include<vector> } using namespace std; int main() for(int i=0;i<v.size();i++) { { vector<int>v1; v[i]+=10; // add 10 to each element of vector vecotr<char>v2; vecotr<long long>v3; } return 0; STL container ফযগুব্রলাব্রত begin() & end() public member function আব্রছ, ফসইগুলা iterator বযবহার িব্ররও element access িরা যায়। } Vector capacity, element access, modifiers: 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  3. Sujan prodhan 3 M v.push_back(x); এর মাব্রে হল ‘x’ ফভক্টব্ররর ফশব্রে ফযাে হব্রয় যাব্রব। 3.vector access with iterator.cpp: vector< Type >::iterator Name; #include<iostream> 4.vector user input.cpp #include<vector> using namespace std; #include<iostream> int main() #include<vector> { using namespace std; vector<int>v; int main() for(int i=0;i<10;i++)v.push_back(rand()%100); { cout<<"Initial vector: "; vector<int>v; for(int i=0;i<v.size();i++)cout<<v[i]<<" "; int N; cout<<endl; cin>>N; for(int i=0;i<N;i++) vector<int>::iterator it; { cout<<"Access with iterator: "; int x; for(it=v.begin();it!=v.end();it++) cin>>x; { v.push_back(x); cout<<(*it)<<" "; } } return 0; cout<<endl; } return 0; vector এ ইেপুট ফেয়ার আব্ররিটি উপায় হল, কেকেতষ্ট সাইব্রের ফভক্টর declare িব্রর তাাঁব্রত, scanf/cin use িব্রর ইেপুট ফেয়া। } ফভক্টব্রর সরাসকর ইেপুট ফেয়ার ফিাে উপায় ফেই। এর েেয আলাোভাব্রব ভযালু ইেপুট কেব্রয় ফভক্টর modifier কেব্রয় ফভক্টব্রর ভযালু assign িরব্রত হয়। সাধারণত push_back use িব্রর এযাসাইে িরা হয়। এর িাে হল ফভক্টর কলব্রের ফশব্রে এিটি একলব্রমন্ট ফযাে িব্রর ফেয়া। 5.vector input.cpp 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  4. Sujan prodhan 4 M #include<stdio.h> { #include<vector> vector<int>v; using namespace std; v.push_back(199); int main() v.push_back(34); { v.push_back(1325); vector<int>v(10); v.push_back(45); for(int i=0;i<v.size();i++) v.push_back(8989); { scanf("%d",&v[i]); cout<<"Initial vector size "<<v.size()<<endl; } cout<<"Initial vector: "; return 0; for(int i=0;i<v.size();i++) } { pop_back use িব্রর ফভক্টব্ররর ফশব্রের element করমুভ িব্রর ফেয়া হয়। cout<<v[i]<<" "; } insert ফভক্টব্ররর এিটি কেকেতষ্ট পকেশব্রে ভযালু insert িরা। cout<<endl; erase ফভক্টর ফেব্রি কেকেতষ্ট পকেশব্রের ভযালু মুব্রছ ফেয়া বা এিটা কেকেতষ্ট পকেশব্রের ফরব্রের ভযালু মুব্রছ ফেয়ার েেয বযবহার িরা হয়। Complexity Linear. v.pop_back(); cout<<"After pop_back: "; for(int i=0;i<v.size();i++)cout<<v[i]<<" "; clear পুব্ররা ফভক্টর empty িব্রর কেব্রত বযবহার িরা হয়। cout<<endl; 6. pop_back, insert, erase, size, empty.cpp v.insert(v.begin(),34); cout<<"After Insert 1st position: "; for(int i=0;i<v.size();i++)cout<<v[i]<<" "; #include<iostream> cout<<endl; #include<vector> using namespace std; v.insert(v.begin()+2,345); int main() cout<<"After Insert 3rd position: "; 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  5. Sujan prodhan 5 M for(int i=0;i<v.size();i++) cout<<v[i]<<" "; Is vector empty cout<<endl; এখাব্রে v.begin() vector এর শুরুর iterator. এইটা অব্রেিটা পব্রয়ন্টাব্ররর মত। v.erase(v.begin()+1); Line 27 এ v.begin() মাব্রে ফভক্টব্ররর প্রেব্রম ভযালু insert িরা হব্রে। cout<<"After erase one element from 2nd position: "; Line 32 এ v.begin() মাব্রে হব্রে ফভক্টব্ররর 3rd position এ ভযালু insert িরা হব্রে। for(int i=0;i<v.size();i++)cout<<v[i]<<" "; cout<<endl; Line 42 এ v.erase(v.begin()+2,v.begin()+4) মাব্রে হব্রে ফভক্টব্ররর 3rd to 4th পকেশে পযতন্ত সব একলব্রমন্ট করমুভ িব্রর ফেয়া। এখাব্রে ফরে [2 , 4) এইভাব্রব িাে িরব্রছ। including first limit, excluding last limit. (x<=i<y) v.erase(v.begin()+2,v.begin()+4); cout<<"After erase 3rd to 4th position: "; for(int i=0;i<v.size();i++) cout<<v[i]<<" "; Some advance operation in vector: cout<<endl; Size define িব্রর ফভক্টর declare িরা ও initial value set িব্রর ফভক্টর declare িরা যায়। v.clear(); vector< type >Name( Size ); cout<<"Is vector empty "<<v.empty()<<endl; vector< type >Name( Size , InitialValue ); return 0; 7.vector size define declare.cpp } #include<iostream> #include<vector> using namespace std; Initial vector size 5 int main() Initial vector: 199 34 1325 45 8989 { After pop_back: 199 34 1325 45 vector<int>v1(10); After Insert 1st position: 34 199 34 1325 45 cout<<"vector v1: "; After Insert 3rd position: 34 199 345 34 1325 45 for(int i=0;i<v1.size();i++)cout<<v1[i]<<" "; After erase one element from 2nd position: 34 345 34 1325 45 cout<<endl; After erase 3rd to 4th position: 34 345 45 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  6. Sujan prodhan 6 M vector<int>v2(10,-1); cout<<endl; cout<<"vector v2: "; for(int i=0;i<v2.size();i++)cout<<v2[i]<<" "; v.resize(5); cout<<endl; cout<<"After resize(5): "; for(int i=0;i<v.size();i++)cout<<v[i]<<" "; return 0; cout<<endl; } v.resize(8,100); vector v1: 0 0 0 0 0 0 0 0 0 0 cout<<"After resize(8,100): "; vector v2: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 for(int i=0;i<v.size();i++)cout<<v[i]<<" "; cout<<endl; size define িব্রর initial value ছাড়া ফভক্টর declare িরব্রল initially vector এর সব একলব্রমন্ট zero োব্রি। v.resize(15); cout<<"After resize(15): "; for(int i=0;i<v.size();i++)cout<<v[i]<<" "; vector এর সাইে পকরবততে িরার েেয resize() member function use িরা হয়। cout<<endl; 8. vector Resize.cpp return 0; } #include<iostream> #include<vector> Initial vector: 1 2 3 4 5 6 7 8 9 10 using namespace std; After resize(5): 1 2 3 4 5 int main() After resize(8,100): 1 2 3 4 5 100 100 100 { After resize(15): 1 2 3 4 5 100 100 100 0 0 0 0 0 0 0 vector<int>v; for(int i=1;i<=10;i++)v.push_back(i); accumulate(), min_element(), max_element() কেব্রয় ফভক্টর ফেব্রি সহব্রে ফযােেল, কমকেমাম ও মযাকিমাম োম্বার ফবর িব্রর ফেলা যায় এবং cout<<"Initial vector: "; for(int i=0;i<v.size();i++)cout<<v[i]<<" "; 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  7. Sujan prodhan 7 M sort() function কেব্রয় সট ত িরা যায়। এইগুলা েেয algorithm header file include িরব্রত হব্রব। sort(v.begin(),v.end()); cout<<"After sort: "; for(int i=0;i<v.size();i++)cout<<v[i]<<" "; cout<<endl; return 0; } 9. Sum Min Max sort.cpp Initial vector: 83 86 77 15 93 35 86 92 49 21 #include<iostream> sum of all element: 637 #include<vector> Min element of vector: 15 #include<stdlib.h> Max element of vector: 93 #include<algorithm> After sort: 15 21 35 49 77 83 86 86 92 93 #include <numeric> using namespace std; Make vector element unique: int main() { 10.unique vector.cpp vector<int>v; for(int i=0;i<10;i++)v.push_back(rand()%100); #include<iostream> cout<<"Initial vector: "; #include<vector> #include<stdlib.h> for(int i=0;i<v.size();i++)cout<<v[i]<<" "; #include<algorithm> cout<<endl; using namespace std; int main() cout<<"sum of all element: "<<accumulate(v.begin(),v.end(),0)<<endl; { vector<int>v; cout<<"Min element of vector: "<<*min_element(v.begin(),v.end())<<endl; for(int i=0;i<10;i++)v.push_back(rand()%100); cout<<"Initial vector: "; cout<<"Max element of vector: "<<*max_element(v.begin(),v.end())<<endl; for(int i=0;i<v.size();i++)cout<<v[i]<<" "; 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  8. Sujan prodhan 8 M element add িরা যায় ও প্রেম ফেব্রি element remove িরা যায়। cout<<endl; sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); 11. list.cpp #include<iostream> cout<<"Unique vector: "; #include<list> for(int i=0;i<v.size();i++)cout<<v[i]<<" "; #include<stdlib.h> cout<<endl; using namespace std; return 0; int main() } { Initial vector: 83 86 77 15 93 35 86 92 49 21 list<int>List; Unique vector: 15 21 35 49 77 83 86 92 93 list<int>::iterator it; List: for(int i=0;i<10;i++)List.push_back(rand()%100); ফভক্টব্ররর মত কলে ও এি ধরব্রের ডাইোকমি িেব্রটইোর। cout<<"List size: "<<List.size()<<endl; List declare: List container এর েেয list header file include িরব্রত হব্রব। cout<<"Initial list: "; for(it=List.begin();it!=List.end();it++)cout<<(*it)< <" "; list< Type >Name; list< int >List; cout<<endl; List capacity, element access, modifiers: cout<<"First element: "<<List.front()<<" Last element: "<<List.back()<<endl; vector এর মত list প্রায় সিল অপাব্ররশে এিই রিব্রমর। list এর element vector মত [] অপাব্ররটর কেব্রয় access িরা যায় ো। list এর সিল element access এর েেয iterator বযবহার িরব্রত হয়। list resize িরার েেয কিছু ফেই। তব্রব push_front(), pop_front() েুইটি public member function আব্রছ, যা কেব্রয় list এর প্রেব্রম List.push_front(10000); cout<<"After push element in front: "; 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  9. Sujan prodhan 9 M for(it=List.begin();it!=List.end();it++)cout<<(*it)< <" "; 12. list sort, unique, remove .cpp #include<iostream> cout<<endl; #include<list> #include<stdlib.h> List.pop_back(); using namespace std; List.pop_front(); int main() { cout<<"After poped element from front & back: "; list<int>List; for(it=List.begin();it!=List.end();it++)cout<<(*it)< <" "; list<int>::iterator it; for(int i=0;i<10;i++)List.push_back(rand()%100); cout<<endl; List.push_front(100); List.push_back(100); return 0; cout<<"Initial list: "; } for(it=List.begin();it!=List.end();it++)cout<<(*it)< <" "; Initial list: 83 86 77 15 93 35 86 92 49 21 cout<<endl; First element: 83 Last element: 21 After push element in front: 10000 83 86 77 15 93 35 86 92 49 21 List.sort(); After poped element from front & back: 83 86 77 15 93 35 86 92 49 cout<<"After sort: "; for(it=List.begin();it!=List.end();it++)cout<<(*it)< <" "; Some advance operation in list: list এর কিছু public member function হল sort(), remove(), unique(). sort এর time complexity O(nlogn), remove(), unique() এর time complexity linear O(n). cout<<endl; List.unique(); 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  10. Sujan prodhan 10 M িরা। এর েেয list এর splice() public member function বযবহার িরা হয়। cout<<"After quniue: "; for(it=List.begin();it!=List.end();it++)cout<<(*it)< <" "; 13. slpice.cpp cout<<endl; #include<iostream> List.remove(100); #include<list> List.remove(73); #include<stdlib.h> using namespace std; cout<<"After remove: "; int main() for(it=List.begin();it!=List.end();it++)cout<<(*it)< <" "; { list<int>List1,List2; cout<<endl; list<int>::iterator it; for(int i=0;i<10;i++)List1.push_back(rand()%100); return 0; } for(int i=0;i<5;i++)List2.push_back(rand()%100); Initial list: 100 7 49 73 58 30 72 44 78 23 9 100 cout<<"Initial list1: "; After sort: 7 9 23 30 44 49 58 72 73 78 100 100 for(it=List1.begin();it!=List1.end();it++)cout<<(*i t)<<" "; After quniue: 7 9 23 30 44 49 58 72 73 78 100 After remove: 7 9 23 30 44 49 58 72 78 cout<<endl; কবদ্রঃ list/vector এর সিল element unique িরার েেয আব্রে sort িব্রর কেব্রত হব্রব। িারণ unique োংশে পাশাপাকশ েুইটি ভযালু ফসম হব্রল এিটি ফসভ িব্রর। এইেেয আব্রে সট ত িব্রর কেব্রল সবগুব্রলা element unique হব্রব। cout<<"Initial list2: "; for(it=List2.begin();it!=List2.end();it++)cout<<(*i t)<<" "; cout<<endl; list এর মব্রধয সবব্রেব্রয় িাযতিরী অপাব্ররশে হল, েুইটি কলে O(1) time complexity ফত সংযুক্ত List1.splice(List1.end(),List2); 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  11. Sujan prodhan 11 M েুইটি string িব্রেয়ার িরার েেয ‘==’ অপাব্ররটর বযবহার িরা হয়। cout<<"After splice: "; string concat িরার েেয বযবহার িরা হয় ‘+’ for(it=List1.begin();it!=List1.end();it++)cout<<(*i t)<<" "; string append িরার েেয ‘+=’ বযবহার িরা যায়। cout<<endl; vector এর মত string এর push_back(), pop_back(), erase(), size() public member function আব্রছ। return 0; } Initial list1: 7 49 73 58 30 72 44 78 23 9 14. string.cpp Initial list2: 40 65 92 42 87 After splice: 7 49 73 58 30 72 44 78 23 9 40 65 92 42 87 #include<iostream> #include<string> #include<stdlib.h> কবদ্রঃ splice অপাব্ররশব্রে time complexity সবসময় O(1) েয়। যখে ফিাে কলব্রের মাব্রে ইটাব্ররটর কেব্রয় আব্ররিটি কলে সংযুক্ত িরা হয় তখে time complexity O(n) হব্রয় যায়। n হল number of element of list. কিন্তু ফিাে কলব্রের প্রেব্রম বা ফশব্রে অেয কলে সংযুক্ত িরব্রত সবসময় O(1). using namespace std; int main() { string str0="ABCDEFGH"; string str1="ABCDEFGH"; string str2="Extinction is the rule."; String: string str3="Survival is the exception."; string েরমাল করং এর মতই। তব্রব এর কিছু কেোর আব্রছ, যার েেয string বযবহার িরা অব্রেি সহে। string class বযবহাব্ররর েেয string header file include িরব্রত হব্রব। cout<<"Is str0 & str1 same: "<<(str0==str1)<<endl; string str4=str2+" "+str3; cout<<"Concat str2 & str3: "<<str4<<endl; string capacity, element access, modifiers: for(char i='a';i<='g';i++) string এর প্রকতটি character [] অপাব্ররটর কেব্রয় access িরা যায়। { str0+=i; 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  12. Sujan prodhan 12 M } string inp; cout<<"New str0: "; cin>>inp; for(int i=0;i<str0.size();i++)cout<<str0[i]; cout<<"First input: "<<inp<<endl; cout<<endl; cin.ignore(); getline(cin,inp); str0.erase(0,3); cout<<"Second input: "<<inp<<endl; printf("%s",inp.c_str()); cout<<"After erase operation in str0: "<<str0<<endl; return 0; } return 0; Some advance operation in string: } Is str0 & str1 same: 1 min(),max() function কেব্রয় েুইটি string A ও B এর মব্রধয lexicographically smallest ও largest করং ফবর িরা যায়। Concat str2 & str3: Extinction is the rule. Survival is the exception. New str0: ABCDEFGHabcdefg find() public member function কেব্রয় string এ অেয এিটি পযাট ত াে খুাঁব্রে ফবর িরা যায়। এটি প্রেম ফেব্রি খুাঁব্রে পাওয়া পযাট ত াব্রের পকেশে করট ত াে িব্রর। ফশব্রে ফেব্রি পযাট ত াে করং খুাঁব্রে ফবর িরার েেয rfind() বযবহার িরা হয়। After erase operation in str0: DEFGHabcdefg string এ ইেপুট ফেয়ার েেয cin/getline() বযবহার িরা লাব্রে। আউটপুব্রটর েেয cout, আর যকে printf() কেব্রয় কপ্রন্ট িরা লাব্রে তাহব্রল c_str() public member function কেব্রয় c string এ িেতভাট িব্রর ফেয়া লাব্রে। String এর ফযব্রিাে পকেশে ফেব্রি ফযব্রিাে সাইব্রের sub string এর েেয substr() public member function বযবহার িরা হয়। 15.string input.cpp 16. String operation.cpp #include<iostream> #include<string> #include<iostream> #include<stdlib.h> #include<string> using namespace std; #include<stdlib.h> int main() using namespace std; { int main() 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  13. Sujan prodhan 13 M stack হল এমে এিধরব্রের ডাটারািোর যা, “last in, first out” প্রপাটি ত ফমব্রে েব্রল। এরমাব্রে হল এমে এিটি কলে, ফযই কলব্রে ফয সবার পব্রর insert হব্রব ফস সবার আব্রে ফবর হব্রব। linked list কেব্রয় েযাি implement িরা যায়। কিন্তু stl stack বযবহার িরব্রল ফসই কেকেস আর িরা লাব্রে ো। { string str="somewhere, something incredible is waiting to be known."; cout<<"Find some from first: "<<str.find("some")<<endl; Stack declare: cout<<"Find some from last: "<<str.rfind("some")<<endl; stack বযবহাব্ররর েেয header file stack include িরব্রত হব্রব। stack< Type >Name; stack< int >Name; cout<<"substring: "<<str.substr(0,9)<<endl; cout<<"substring: "<<str.substr(11,9)<<endl; Stack operation: cout<<"lexicographically small: "<<min(str.substr(0,9),str.substr(11,9))<<endl; stack এর প্রধাে অপাব্ররশে হল, push(), pop(), top() public member function. push() কেব্রয় stack এর ফশব্রে েতুে element ফযাে িরা হয়। pop() কেব্রয় stack এর ফশব্রে ফযাে হওয়া element ফেব্রল ফেয়া হয়। top() কেব্রয় stack এর সবার ফশব্রে বা সবার উপব্ররর element access িরা হয়। stack এর ফিাে begin(), end() iterator return িব্রর এমে public member function ফেই। তাই পুব্ররা stack এিসাব্রে iterate িরার ফিাে উপায় ফেই। cout<<"lexicographically large: "<<max(str.substr(0,9),str.substr(11,9))<<endl; return 0; } Find some from first: 0 তাছাড়াও stack এর আরও েুইটি িাযতিরী public member function হল, size(), empty(). Find some from last: 11 substring: somewhere 17.stack.cpp substring: something lexicographically small: something lexicographically large: somewhere #include<iostream> #include<stack> Stack: #include<stdlib.h> 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  14. Sujan prodhan 14 M using namespace std; Push: 15 int main() Push: 93 { Stack size: 5 stack<int>S; Top element: 93 for(int i=0;i<5;i++) After pop top element: 15 { Is stack empty: 1 int x=rand()%100; cout<<"Push: "<<x<<endl; Queue: S.push(x); queue ডাটারািোর “first in first out” ফপ্রাপাটি ত ফমব্রে েব্রল। মাব্রে কলব্রে ফয element সবার আব্রে add হব্রব, ফসই element সবার আব্রে ফবর হব্রব। } cout<<"Stack size: "<<S.size()<<endl; Queue declare: queue বযবহাব্ররর েেয header file queue include িরব্রত হব্রব। cout<<"Top element: "<<S.top()<<endl; queue< Type >Name; S.pop(); queue< int >Name; cout<<"After pop top element: "<<S.top()<<endl; Queue operation: queue এর অপাব্ররশে গুলা হল, push(), front(), pop(). push() public member function কেব্রয় queue ফত element এযাড িরা হয়। front() কেব্রয় সবার সামব্রের (ব্রয আব্রে এযাড হব্রয়কছল) ফসইটা access িরা হয়। pop() কেব্রয় সবার উপব্ররর element কডকলট িব্রর/ ফেব্রল ফেয়া হয়। েযাব্রির মতও queue ফত public member function begin(), end() ো োিায় সিল element iterate িরা যায় ো। while(!S.empty())S.pop(); cout<<"Is stack empty: "<<S.empty()<<endl; return 0; } Push: 83 Push: 86 Push: 77 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  15. Sujan prodhan 15 M queue এর সাইে এর েেয size() এবং empty কিো তা োোর েেয empty() member function বযবহার িরা হয়। Insert 83 Insert 86 Insert 77 Insert 15 18. queue.cpp Insert 93 Q front element: 83 #include<iostream> Q front after pop: 86 #include<queue> Queue size: 4 #include<stdlib.h> Is queue empty: 1 using namespace std; int main() Deque: { Double ended queue. এর front ও back উভয় কেব্রি ফেব্রি ভযালু add িরা যায় ও remove িরা যায়। queue<int>Q; for(int i=0;i<5;i++) { deque declare: int x=rand()%100; deque বযবহাব্ররর েেয header file deque include িরব্রত হব্রব। cout<<"Insert "<<x<<endl; Q.push(x); deque< Type >Name; } deque< int >Name; deque operation: cout<<"Q front element: "<<Q.front()<<endl; deque এর কিছু অপাব্ররশে queue এর মতই। front ফেব্রি element add এবং remove িরার েেয push_front() ও pop_back() বযবহার িরা হয়। Back ফেব্রি element add এবং remove িরার েেয push_back() ও pop_back() বযবহার িরা হয়। Q.pop(); cout<<"Q front after pop: "<<Q.front()<<endl; cout<<"Queue size: "<<Q.size()<<endl; while(!Q.empty())Q.pop(); cout<<"Is queue empty: "<<Q.empty()<<endl; তাছাড়া deque [] operator কেব্রয় access িরা যায়। এর size(), erase(), clear() public member function আব্রছ। return 0; } 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  16. Sujan prodhan 16 M Priority Queue: Set operation: priority queue এমে এি ধরব্রের কিউ ফযখাব্রে front element সবসময় বড়টা োব্রি, বা যার priority ফবকশ ফস োব্রি। priority queue এর property ফেে িব্রর ফছাট element সবসময় front এ রাখা যায়। set উব্রেখব্রযােয অপাব্ররশেগুব্রলা হল, insert(), erase(), clear(), find(), size(), empty(). set এ begin() ও end() public member function োিায় iterator কেব্রয় পুব্ররা set iterate িরা যায়। Priority queue declare: 19.set.cpp Priority queue বযবহাব্ররর েেয header file queue include িরব্রত হব্রব। #include<iostream> priority_queue< Type >Name; #include<set> priority_queue< int >Name; using namespace std; int main() Priority queue operation: { priority queue অপাব্ররশে গুব্রলা queue এর অপাব্ররশব্রের মতই। তব্রব এখাব্রে front() এর পকরবব্রতত আব্রছ top(), যা সবসময় priority queue ফত োিা বড় ভযালু (by default) করট ত াে িব্রর। set<int>S; set<int>::iterator it; for(int i=1;i<100;i++) { Set: S.insert(i%10); } set হল এমে এি ধরব্রের কলে ফযখাব্রে সব element এিবার িব্রর োিব্রব। মাব্রে এিটি value যতবাব্ররই insert িরা ফহাি ো ফিে, োিব্রব এিবার। set সিল element ফি sorted আিাব্রর রাব্রখ। cout<<"Set size: "<<S.size()<<endl; cout<<"Set element: "; for(it=S.begin();it!=S.end();it++)cout<<(*it)<<" "; cout<<endl; set<int>::iterator it1,it2; Set declare: it1=S.find(4); set বযবহাব্ররর েেয header file set include িরব্রত হব্রব। it2=S.find(7); S.erase(it1,it2); set< Type >Name; cout<<"Set element after erase 4 to 6: "; for(it=S.begin();it!=S.end();it++)cout<<(*it)<<" "; set< int >Name; 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  17. Sujan prodhan 17 M মত access. map এ সিল element তার key value কেব্রয় sorted আিাব্রর োব্রি। map এর সিল element iterator কেব্রয় iterate িরা যায়। find() public function কেব্রয় ফযব্রিাে key map এ আব্রছ কিো তা খুাঁব্রে ফবর িরা যায়। erase() public member function কেব্রয় ফযব্রিাে key remove িব্রর ফেয়া যায়। cout<<endl; S.clear(); cout<<"Is set empty: "<<S.empty()<<endl; return 0; } 20. map.cpp #include<iostream> Set size: 10 Set element: 0 1 2 3 4 5 6 7 8 9 #include<map> Set element after erase 4 to 6: 0 1 2 3 7 8 9 using namespace std; Is set empty: 1 int main() set এর insert(),find(),erase() সবগুলার time complexity O(log2(n)) { map<char,int>Map; map<char,int>::iterator it; Map: for(char c='a';c<='j';c++) ফিাে কিছুব্রি িী-ভযালু কেব্রয় মযাপ িব্রর রাখার েেয map stl বযবহার িরা হয়। মযাব্রপর বযবহাব্ররর এিটি বড় সুকবধা হল, ডাইোকমি হওয়াব্রত ফযব্রিাে class,string, variable মযাকপং িরা যায়। { int value=(int)c; Map[c]=value; } cout<<"Map size: "<<Map.size()<<endl; map declare: cout<<"Map key value: \n"; map বযবহাব্ররর েেয header file map include িরব্রত হব্রব। for(it=Map.begin();it!=Map.end();it++) { map< key_Type , value_type >Name; cout<<"Key-> "<<(*it).first<<" value-> "<<(*it).second<<endl; map< int, char >Name; } map operation: cout<<"Key a value: "<<Map['a']<<endl; map ভযালু assign িরব্রত হয় Map[key]=value এইভাব্রব। এিটা কেকেতষ্ট key এর ভযালু পাওয়া েেয value=Map[key] েরমাল এযাব্রর index এর if(Map.find('a')!=Map.end())cout<<"key 'a' found"<<endl; else cout<<"key 'a' not found"<<endl; 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  18. Sujan prodhan 18 M map এ সকল অপাররশরের time complexity logarithmic in size. O(log2(N)). if(Map.find('z')!=Map.end())cout<<"key 'z' found"<<endl; else cout<<"key 'z' not found"<<endl; Map.clear(); Caution: cout<<"Is map empty: "<<Map.empty()<<endl; map এ specific key আব্রছ কিো তা েুইভাব্রব ফেি িরা যায়। প্রেম উপায় হল Map[key]==0 তাহব্রল ফসই key মযাব্রপ ফেই আব্ররিটা উপায় হল find() public member function use িব্রর। return 0; } 21.map key value search technique 1.cpp Map size: 10 Map key value: #include<iostream> Key-> a value-> 97 #include<map> Key-> b value-> 98 using namespace std; Key-> c value-> 99 int main() Key-> d value-> 100 { Key-> e value-> 101 map<char,int>Map; Key-> f value-> 102 for(char c='a';c<='j';c++) Key-> g value-> 103 { Key-> h value-> 104 int value=(int)c; Key-> i value-> 105 Map[c]=value; Key-> j value-> 106 } Key a value: 97 key 'a' found for(char c='a';c<='z';c++) key 'z' not found { Is map empty: 1 if(Map[c]==0)cout<<"key "<<c<<" not found"<<endl; else cout<<"key "<<c<<" found"<<endl; } 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  19. Sujan prodhan 19 M এখাব্রে উপব্রর েুই ধরব্রের সাে ত technique এ প্রেম technique এ এিটু োব্রমলা আব্রছ। প্রেম পদ্ধকতব্রত প্রব্রতযিবার সাে ত িরার সময় মযাব্রপ specific key value ো োিব্রলও ওই key value কেব্রয় েতুে এিটি স্লট map এ allocate িব্রর কেব্রে এইভাব্রব Map[key]=0. এব্রত িব্রর মযাব্রপর সাইে প্রব্রতযি সাব্রে ত ফবব্রর যাব্রে। ফপ্রাগ্রাকমং িব্রন্টে এ এই ফটিকেি memory limit exceed error এর এিটা িারণ হব্রত পাব্রর। তাই সবসময় key search িরার েেয কিতীয় technique use িরা ভাল। return 0; } 22.map key value search technique 2.cpp #include<iostream> #include<map> কেব্রের ফিাডটুকু ফেখব্রল সমসযাটা আরও ভাল িব্রর বুো যাব্রবঃ using namespace std; int main() { 23. map search fault.cpp map<char,int>Map; for(char c='a';c<='j';c++) #include<iostream> { #include<map> int value=(int)c; using namespace std; Map[c]=value; int main() } { map<char,int>Map; for(char c='a';c<='z';c++) for(char c='a';c<='j';c++) { { if(Map.find(c)!=Map.end())cout<<"Key "<<c<<" found"<<endl; int value=(int)c; else cout<<"Key "<<c<<" not found"<<endl; Map[c]=value; } } return 0; cout<<"Initially map size: "<<Map.size()<<endl; } for(char c='a';c<='z';c++) { 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

  20. Sujan prodhan 20 M element range এর প্রেম ফেব্রি ওই পযতন্ত < val োব্রি। if(Map[c]==0){} } C++ এর upper_bound() function sorted container এর ফরে [first,last) এর মব্রধয এমে এিটি iterator position return িব্রর ফযখাব্রে সব element range এর প্রেম ফেব্রি ওই পকেশে পযতন্ত <=val োব্রি। cout<<"After Search map size: "<<Map.size()<<endl; return 0; } Initially map size: 10 After Search map size: 26 upper_bound, lower_bound algorithm header file এ আব্রছ। এব্রের time complexity O(log2(N)). এখাব্রে ফেখা যাব্রে সাে ত িরার আব্রে মযাব্রপর সাইে কছল 10, সাে ত িরার পর ফবব্রড় হব্রয়ব্রছ 26. uppper_bound(), lower_bound() use িব্রর sorted container এর ফরে এর মব্রধয এিটা কেকেতষ্ট ফরে এর োম্বার িতগুব্রলা আব্রছ তা সহব্রে ফবর িব্রর ফেলা যায়। iterator: #include <iostream> #include <algorithm> C++ এর iterator অব্রেিটা পব্রয়ন্টাব্ররর মত, যা ফিাে container object এর পব্রয়ন্টার কেব্রয় এিটা কেকেতষ্ট ফরব্রের মব্রধয প্রকতটা element iterate িরব্রত পাব্রর। ফযসব stl গুলাব্রত begin(),end() public member function আব্রছ ফসইগুলা iterator কেব্রয় iterate িরা যায়। #include <vector> using namespace std; int main () { int myints[] = {10,20,30,30,20,10,10,20,50,1,39,40}; iterator declare এর নেয়ম হলঃ vector<int> v(myints,myints+12); container::iterator name; sort (v.begin(), v.end()); vector<int>::iterator low,up; ফযমেঃ set এর iterator declare িরব্রত হব্রল, set::iterator it; এইভাব্রব িরব্রত হব্রব। low=lower_bound (v.begin(), v.end(), 20); up= upper_bound (v.begin(), v.end(), 30); cout<<"Total element in range [20,30]: "<<up-low<<endl; upper bound & lower bound: C++ এর lower_bound() function sorted container এর ফরে [first,last) এর মব্রধয এমে এিটি iterator position return িব্রর ফযখাব্রে সব return 0; } 01902383808 ru.cse29@gamil.com RAJSHAHI UNIVERSITY

More Related