140 likes | 313 Views
ACM presents Introduction to STL. - Arnab Chowdhury - Rahul Prasad. What is STL?. STL – Standard Template Library Part of ISO standard C++ library Provides efficient, easy to use, robust implementations of the common data structures and algorithms. What are Templates?.
E N D
ACM presentsIntroduction to STL - ArnabChowdhury - Rahul Prasad
What is STL? • STL – Standard Template Library • Part of ISO standard C++ library • Provides efficient, easy to use, robust implementations of the common data structures and algorithms
What are Templates? • Template function is a function built-in as an ISO standard. Incorporated as a standard since the release of C++99 compilers. • Templates allow the usage of generic types (Advantage: no need of writing the same code all over again for a different data type) Example template <class X> // X can represent any data type as long as it supports the // operations which are being performed upon it. class ABC { ABC() { } public: getMax(X a, X b) { return (a > b ? a: b); } }; ABC <double> a; max_val = a.getMax(15.30, 53.12); // Call from main
The Standard Template Library Topics to be discussed.. • string • stack • queue • vector • map • pair • sstream
Why use STL? • Reduce Development Time (important specially with respect to Programming Contests where time is a de-facto) • Highly Reliable Code (no or small amount of Debugging effort required) • Highly Robust (Container size grows automatically) • Portable and easily maintainable
The string Class • Though ‘string’ is not a part of C++ STL, but it is commonly grouped as one. • Character Arrays represent strings easily. Even the string class uses character arrays to implement strings. But the difference being the automatic handling of space. # include <iostream> # include <string> using namespace std; int main() { string s = “ACM Coding Week”; string str; cout << “Length of the entered string is ” << s.length(); // 15 s += “ 2012”; // s = “ACM Coding Week 2012”; str = s.substr(2, 5); // str = “M Cod” }
Stack # include <stack> stack <int> s; // Declare s.push(5); // Push Operation N = s.top() // Accessing top element s.pop(); // Popping out Topmost element Implementation Example… Check to see whether a given string containing open and closing parentheses only is balanced or not.
Queue # include <queue> queue <string> customer_name; // Declare customer_name.push(“Abhivnav”); // Push // Accessing front Element string s = customer_name.front(); customer_name.pop(); //Popping front element
Maps • A Map is a data structure which has an associative property. It includes a key to which a certain value is associated. • Suppose, you want to store the telephone numbers of your friends and want to get their telephone numbers as soon as you type in their names as a Query. Maps provide a useful solution in such cases by giving direct access to such elements. Example.. # include <map> …. int main() { map <string, int> m; string s; while (cin >> s) { cin >> m[s]; } cout << “Enter a Query Name: “; cin >> s; if (m.find(s) != m::end) cout << s << “found! Telephone no: ” << m[s]; else cout << “Not Found Query String!”; }
Vector #include<vector>// Pre-allocatevector<int> v(100);v[80]=1; // OKv[200]=1; // bad//Grow tailvector<int> v2;int i;while (cin >> i)v.push_back(i); v[i] //Access
Pair • Stores 2 different or similar data types. • Useful in cases where you need to deal with 2 parameters only. # include <vector> .. int main() { vector < pair <int, double> > v; int a; double b; while (cin >> a >> b) { v.push_back(make_pair(a, b)); } }
String Stream # include <sstream> … int main() // Depth of file in directory structure { string s = “/home/arnab/Documents/acm/x.cpp”, x; int count =0; for (int i = 0; i < s.length(); ++i) if (s[i] == ‘/’) s[i] = ‘ ‘; stringstreamss(s); while (ss >> x) ++count; cout << count << “\n”; } Output: 5
Some other useful Functions next_permutaion(v.begin(), v.end()); prev_permutaion(v.begin(), v.end()); sort(v.begin(), v.end()); // Here v can be either a vetor or an array refernce or any // other data structure storing data sequentially. lower_bound(v.begin(), v.end(), x); // Finds the first occurrence of x in the range of v upper_bound(v.begin(), v.end(), x); // Finds the last occurrence of x in the range of v For further references, visit: http://www.cplusplus.com/reference/
Advanced STL Topics • Iterators, Containers • priority_queue • set • deque • bitset