1 / 14

ACM presents Introduction to STL

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?.

noam
Download Presentation

ACM presents Introduction to STL

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. ACM presentsIntroduction to STL - ArnabChowdhury - Rahul Prasad

  2. 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

  3. 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

  4. The Standard Template Library Topics to be discussed.. • string • stack • queue • vector • map • pair • sstream

  5. 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

  6. 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” }

  7. 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.

  8. 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

  9. 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!”; }

  10. 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

  11. 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)); } }

  12. 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

  13. 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/

  14. Advanced STL Topics • Iterators, Containers • priority_queue • set • deque • bitset

More Related