290 likes | 382 Views
Introduction to C ++ STL. STL Classes. Container Classes Storage Class stack, queue, vector, map ... Iterator Classes Algorithm Classes frequently used algorithms sort, find, binary search, next_permutation. STL Containers. Vector. random access ([] operator) like an array
E N D
STL Classes • Container Classes • Storage Class • stack, queue, vector, map ... • Iterator Classes • Algorithm Classes • frequently used algorithms • sort, find, binary search, next_permutation ...
Vector • random access ([] operator) like an array • add / remove an element in constant time at the last of vector • add / remove an element in linear time in the middle of vector • automatic memory management • needn’t specify quantity of elements
Vector Operations #include <vector> int main() { vector<int> v; v.push_back(10); cout<<v[0]<<endl; v.push_back(20); cout<<v[0]<<” “<<v[1]<<endl; cout << v.size() << “ “ << v.capacity() << endl; v.pop_back(); print(v); cout << v.size() << “ “ << v.capacity() << endl; } /* Output: 10 10 20 2 2 10 1 2 */
String Class Operations • string s = "hello"; • string s1 = s.substr(0, 3), // "hel" • s+=“ Ayan” • s2 = s.substr(1, 6), // "ello A“ • s4 = s.substr(1); // "ello Ayan"
List • Sequence • Elements in sequence containers are ordered in a strict linear sequence. • Individual elements are accessed by their position in this sequence. • Doubly-linked list
List Operations #include <list> int main() { list <int> la, lb; la.push_back(0), la.push_back(1), la.push_back(3); lb.push_back(4), lb.push_front(2); la.sort(); lb.sort(); la.merge(lb); /*merge function merges 2 sorted lists*/ print(la); } /* Output: 0 1 2 3 4 */
Set • Associative • Elements in associative containers are referenced by their key and not by their absolute position in the container. • Ordered • The elements in the container follow a strict order at all times. All inserted elements are given a position in this order. • Unique keys • No two elements in the container can have equivalent keys. • Implemented as balanced binary search trees. • Insertion Searching complexity O(logn)
Set Operations #include <set> struct ltstr { bool operator() (const char* s1, const char* s2) const { return (strcmp(s1, s2) < 0); } } int main() { const char* str[6] = {“Ayan”, “Sagar”, “Nitin” ,“Sasy”, “Sadhu”}; set<const char*, ltstr> s(str, str + 6); print(s); } /* Output: AyanNitin Sadhu Sagar Sasy */
MultiSet • Same as set • But elements need not be unique.
Stack Operations #include <stack> int main() { stack<int> s; s.push(8); s.push(5); s.push(6); cout << s.top() << endl; s.pop(); cout << s.top() << endl; } /* Output: 6 5 */
Queue Operations #include <queue> int main() { queue<int> q; q.push(8); q.push(5); q.push(6); cout << q.top() << endl; q.pop(); cout << q.top() << endl; } /* Output: 8 5 */
priority queue • Max Heap • Maximum Element at top of queue
Priority_Queue Operations #include <queue> int main() { priority_queue< int > pq; int ary[6] = {1, 4, 2, 8, 5, 7}; for (int i = 0; i < 6; pq.push(ary[i++])); while (!pq.empty()) { cout << pq.top() << “ “; pq.pop(); } } /* Output: 8 7 5 4 2 1 */
Map • Associative • Elements in associative containers are referenced by their key and not by their absolute position in the container. • Ordered • The elements in the container follow a strict order at all times. All inserted elements are given a position in this order. • Map • Each element associates a key to a mapped value: Keys are meant to identify the elements whose main content is the mapped value. • Unique keys • No two elements in the container can have equivalent keys. • Implemented as balanced binary search trees. • Insertion Searching complexity O(logn)
Map Operations #include <map> int main() { map<string, int> grade; grade[“Mark”] = 95; grade[“Edward”] = 87; grade[“Louise”] = 66; grade[“Allen”] = 76; for(map<string,int> iterator it=grade.begin();it!=grade.end();it++) cout<<*it.first<<“ “<<*it.second<<endl; cout << grade[“Allen”] << endl; } /* Output: Allen, 76 Edward, 87 Louise, 66 Mark, 95 76 */
Deque • Double Ended Queue • Functionality similar to vectors, • but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end • Sequence • Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.
Deque Operations #include <deque> int main() { deque<int> dq; dq.push_back(3); dq.push_front(1); dq.insert(de.begin() + 1, 2); dq[2] = 0; } /* Output: 1 2 0 *
STL Iterators • Every STL container holds a nested iterator class
Iterator Operations #include <vector> #include <iterator> #include <algorithm> int main() { vector<int> v; int ary[6] = {1, 4, 2, 8, 5, 7}; for (int i = 0; i < 6; v.push_back(ary[i++])); for (vector<int>::iterator it = v.begin(); it != v.end(); it++) cout << (*it) << “ “; cout << endl; } /* Output: 1 4 2 8 5 7 */
find • int main () • { • int myints[] = { 10, 20, 30 ,40 }; • int * p; // pointer to array element: • p = find (myints,myints+4,30); • ++p; • cout << "The element following 30 is " << *p << '\n'; • vector<int> myvector (myints,myints+4); • vector<int>::iterator it; // iterator to vector element • it = find (myvector.begin(), myvector.end(), 30); • ++it; • cout << "The element following 30 is " << *it << '\n'; • return 0; • } The element following 30 is 40 The element following 30 is 40
Next_permutation • // next_permutation example • #include <algorithm> // std::next_permutation, std::sort • int main () • { • int myints[] = {1,2,3}; • sort(myints,myints+3); • cout<<"The 3! possible permutations with 3 elements:\n"; • do { • print(myints); • } while(next_permutation(myints,myints+3)); • return 0; • } The 3! possible permutations with 3 elements: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
sort() #include<algorithm>int main() { int ar[5]={3,2,4,1,5}; vector<int> v; for(int i=0;i<5;i++) v.push_back(ar[i]); sort(ar,ar+5); sort(v.begin(),v.end(),greater<int>); print(ar); print(v); } /* Output 1 2 3 4 5 5 4 3 2 1 */
Binary Search // lower_bound/upper_bound example #include <iostream> // std::cout #include <algorithm> // std::lower_bound, std::upper_bound, std::sort #include <vector> // std::vector int main () { intmyints[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up; low=std::lower_bound (v.begin(), v.end(), 20); up= std::upper_bound (v.begin(), v.end(), 20); std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return 0; } /*Output lower_bound at position 3 upper_bound at position 6 */
A Small Example • vector<pair<int,int> > v(5); • for(int i=0;i<5;i++) v[i]=make_pair(5-i,i); • sort(v.begin(),v.end()); • for(vector<pair<int,int> >::iterator it=v.begin();it!=v.end();it++) • cout<<*it.first<<“ “<<*it.second<<endl; • for(int i=0;i<5;i++) • cout<<v[i].first<<“ “v[i].second<<endl; Output 1 4 2 3 3 2 4 1 5 0
Longest Increasing Subsequence set<int> st; set<int>::iterator it; st.clear(); for(int i=0; i<n; i++) { it=st.find(array[i]); if(*it==array[i]); else { st.insert(array[i]); it=st.find(array[i]); it++; if(it!=st.end()) st.erase(it); } for(set<int>::iterator it2=st.begin();it2!=st.end();it2++) printf("%d ",*it2); printf("\n"); } cout<<st.size()<<endl;
Longest Increasing Subsequence Operations /*Input 5 1 2 1 */ /*Output 1 1 2 1 2 2 */ /*Input 5 1 4 2 4 3 */ /*Output 1 1 4 1 2 1 2 4 1 2 3 3 */