Stl Containers

1,708 views
1,634 views

Published on

STL Overview for the uninitiated in 2009

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,708
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
62
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Stl Containers

  1. 1. Oct 20, 2009 STL Containers Dr. Partha Pratim Das Interra Systems (India) Pvt. Ltd. Generalizing Data Structures
  2. 2. Agenda <ul><li>STL Components </li></ul><ul><ul><li>Containers </li></ul></ul><ul><ul><li>Iterators </li></ul></ul><ul><ul><li>Algorithms </li></ul></ul>
  3. 3. STL Components Why & How
  4. 4. STL Components <ul><li>STL is a cooperation of Components </li></ul><ul><li>Key Components </li></ul><ul><ul><li>Containers </li></ul></ul><ul><ul><li>Iterators </li></ul></ul><ul><ul><li>Algorithms </li></ul></ul>
  5. 5. Containers <ul><li>Manage collection of objects of a kind </li></ul><ul><li>Every Container Type reflect a conglomeration of requirements </li></ul><ul><li>May be implemented as </li></ul><ul><ul><li>Arrays </li></ul></ul><ul><ul><li>Linked Lists </li></ul></ul><ul><ul><li>Special Keys for every Element </li></ul></ul>
  6. 6. Iterators <ul><li>Used to step through elements of collections of objects </li></ul><ul><li>Collections may be </li></ul><ul><ul><li>Containers or </li></ul></ul><ul><ul><li>Subsets of Containers </li></ul></ul><ul><li>Offers a small yet common interface for any arbitrary container type </li></ul><ul><li>This works independent of the Internal Structure </li></ul><ul><li>Example: Iterator steps to the next element. </li></ul>
  7. 7. Algorithms <ul><li>Used to process elements of collections </li></ul><ul><li>Examples: </li></ul><ul><ul><li>Search </li></ul></ul><ul><ul><li>Sort </li></ul></ul><ul><ul><li>Modify </li></ul></ul><ul><ul><li>Use </li></ul></ul><ul><li>Algorithms use Iterators </li></ul><ul><li>Algorithms can call user-defined functions (functors) </li></ul>
  8. 8. Concept of STL <ul><li>Separation of Data and Operations </li></ul><ul><ul><li>Data  Managed by Container Classes </li></ul></ul><ul><ul><li>Operations  Defined by Configurable Algorithms </li></ul></ul><ul><ul><li>Iterators  Glue between these Components </li></ul></ul><ul><li>Contradicts OOP – through separation </li></ul>
  9. 9. Basic Model <ul><li>Algorithms </li></ul><ul><ul><li>sort, find, search, copy, … </li></ul></ul><ul><li>Containers </li></ul><ul><ul><li> vector, list, map, hash_map, … </li></ul></ul>iterators <ul><li>Separation of concerns </li></ul><ul><ul><li>Algorithms manipulate data, but don’t know about containers </li></ul></ul><ul><ul><li>Containers store data, but don’t know about algorithms </li></ul></ul><ul><ul><li>Algorithms and containers interact through iterators </li></ul></ul><ul><ul><ul><li>Each container has its own iterator types </li></ul></ul></ul>
  10. 10. Containers Basic Stuff
  11. 11. Types of Containers <ul><li>Sequence Containers </li></ul><ul><ul><li>Ordered Collection </li></ul></ul><ul><ul><li>Every element has a certain position </li></ul></ul><ul><ul><li>Position depends on time and place of Insertion </li></ul></ul><ul><ul><li>Position is independent of value </li></ul></ul><ul><ul><li>Predefined Sequence Containers are: </li></ul></ul><ul><ul><ul><li>vector </li></ul></ul></ul><ul><ul><ul><li>deque </li></ul></ul></ul><ul><ul><ul><li>list </li></ul></ul></ul>
  12. 12. Types of Containers <ul><li>Associative Containers </li></ul><ul><ul><li>Sorted Collection </li></ul></ul><ul><ul><li>Position of an element depends on its value and a certain sorting criterion </li></ul></ul><ul><ul><li>Order of Insertion is immaterial </li></ul></ul><ul><ul><li>Predefined Associative Containers are: </li></ul></ul><ul><ul><ul><li>set </li></ul></ul></ul><ul><ul><ul><li>multiset </li></ul></ul></ul><ul><ul><ul><li>map </li></ul></ul></ul><ul><ul><ul><li>multimap </li></ul></ul></ul>
  13. 13. Types of Containers <ul><li>Container Adaptors </li></ul><ul><ul><li>Meets special needs </li></ul></ul><ul><ul><li>Implemented using fundamental containers </li></ul></ul><ul><ul><li>Predefined Container Adaptors are: </li></ul></ul><ul><ul><ul><li>stack </li></ul></ul></ul><ul><ul><ul><li>queue </li></ul></ul></ul><ul><ul><ul><li>Priority queue </li></ul></ul></ul>
  14. 14. Common Containers Abilities <ul><li>Constructor </li></ul><ul><ul><li>Default: ContType c </li></ul></ul><ul><ul><li>Copy: ContType c1(c2) </li></ul></ul><ul><ul><li>Initialize with a Range: ContType c(beg, end) </li></ul></ul><ul><li>Destructor: ~ ContType c </li></ul>
  15. 15. Common Containers Abilities <ul><li>Size Operations </li></ul><ul><ul><li>c.size(): Actual number of elements </li></ul></ul><ul><ul><li>c.empty(): Whether the container is empty </li></ul></ul><ul><ul><li>c.max_size(): Maximum number of elements possible </li></ul></ul>
  16. 16. Common Containers Abilities <ul><li>Comparison Operators: </li></ul><ul><ul><li>Operators: ==, !=, <, <=, >, >= </li></ul></ul><ul><ul><li>Both containers must have the same type </li></ul></ul><ul><ul><li>Two containers are equal if their elements are equal and have the same order </li></ul></ul><ul><ul><li>To check if a container is less than another, a lexicographic comparison is done </li></ul></ul>
  17. 17. Common Containers Abilities <ul><li>Assignments and swap() </li></ul><ul><ul><li>c1 = c2: </li></ul></ul><ul><ul><ul><li>Copy all elements of the source container and remove all old elements of the destination container </li></ul></ul></ul><ul><ul><ul><li>Linear Complexity - expensive </li></ul></ul></ul><ul><ul><li>c1.swap(c2) / swap(c1, c2) </li></ul></ul><ul><ul><ul><li>Use when containers have the same type and source is no longer used </li></ul></ul></ul><ul><ul><ul><li>Swaps some internal pointers that refer to the data (elements, allocator, sorting criterion) </li></ul></ul></ul><ul><ul><ul><li>Constant Complexity </li></ul></ul></ul>
  18. 18. Common Containers Abilities <ul><li>Iterator Functions </li></ul><ul><ul><li>c.begin(): </li></ul></ul><ul><ul><ul><li>Iterator for first element </li></ul></ul></ul><ul><ul><li>c.end(): </li></ul></ul><ul><ul><ul><li>Iterator for last element </li></ul></ul></ul><ul><ul><li>c.rbegin(): </li></ul></ul><ul><ul><ul><li>Reverse Iterator for first element </li></ul></ul></ul><ul><ul><li>c.rend(): </li></ul></ul><ul><ul><ul><li>Reverse Iterator for last element </li></ul></ul></ul>
  19. 19. Common Containers Abilities <ul><li>Manipulators </li></ul><ul><ul><li>c.insert(pos, elem): </li></ul></ul><ul><ul><ul><li>Insert a copy of elem at pos </li></ul></ul></ul><ul><ul><li>c.erase(beg, end): </li></ul></ul><ul><ul><ul><li>Remove all elements in range [beg, end) </li></ul></ul></ul><ul><ul><li>c.clear(): </li></ul></ul><ul><ul><ul><li>Remove all elements – makes the container empty </li></ul></ul></ul>
  20. 20. Sequence Container: vector <ul><li>Vector models a dynamic array </li></ul><ul><li>#include <vector> </li></ul><ul><li>Special Operations </li></ul><ul><ul><li>Element Access </li></ul></ul><ul><ul><ul><li>c.at(idx) / c[idx] </li></ul></ul></ul><ul><ul><ul><li>c.front() / c.end() </li></ul></ul></ul><ul><ul><li>Insert / Remove at the back </li></ul></ul><ul><ul><ul><li>c.push_back(elem) </li></ul></ul></ul><ul><ul><ul><li>c.pop_back() </li></ul></ul></ul>
  21. 21. Sequence Container: vector <ul><li>Remove the first element with a value </li></ul>std::vector<Elem> coll; std::vector<Elem>::iterator pos; pos = find(coll.begin(), coll.end(), val); if (pos != coll.end()) { coll.erase(pos); }
  22. 22. Sequence Container: deque (“deck”) <ul><li>Similar to Vector </li></ul><ul><ul><li>Manages elements with Dynamic Array </li></ul></ul><ul><ul><li>Provides Random Access </li></ul></ul><ul><ul><li>Almost same interface as Vector </li></ul></ul><ul><ul><li>Deque is open at both ends. </li></ul></ul><ul><li>#include <deque> </li></ul><ul><li>Special Operations </li></ul><ul><ul><li>Insert / Remove </li></ul></ul><ul><ul><ul><li>c.push_back(elem) / c.pop_back() </li></ul></ul></ul><ul><ul><ul><li>c.push_front(elem) / c.pop_front() </li></ul></ul></ul>
  23. 23. Sequence Container: list <ul><li>Manages elements in a doubly linked list </li></ul><ul><li>#include <list> </li></ul><ul><li>Element Access </li></ul><ul><ul><li>front() / back() </li></ul></ul><ul><li>Insert / Remove </li></ul><ul><ul><li>push_ / pop_ at both ends </li></ul></ul><ul><li>Splice / Sort / Merge / Reverse </li></ul>
  24. 24. Sequence Container: strings <ul><li>C++ string classes as containers </li></ul><ul><ul><li>basic_string<> </li></ul></ul><ul><ul><li>string </li></ul></ul><ul><ul><li>wstring </li></ul></ul>
  25. 25. Sequence Container: Ordinary Array <ul><li>Has static or dynamic size </li></ul><ul><li>Not STL Containers </li></ul><ul><ul><li>No size() </li></ul></ul><ul><ul><li>No empty() </li></ul></ul>
  26. 26. Associative Container: set / multiset <ul><li>Sorts the elements according to a sorting criterion </li></ul><ul><li>Sets do not allow duplicates </li></ul><ul><li>Multisets allow duplicates </li></ul><ul><li>#include <set> </li></ul>
  27. 27. Associative Container: map / multimap <ul><li>Manage key/value pairs as elements </li></ul><ul><li>Sorts the elements according to a sorting criterion that is used for the actual key </li></ul><ul><li>Maps do not allow duplicates </li></ul><ul><li>Multimaps allow duplicates </li></ul><ul><li>#include <map> </li></ul>
  28. 28. Container Adapter: stack <ul><li>#include <stack> </li></ul><ul><li>Core Operations </li></ul><ul><ul><li>push() </li></ul></ul><ul><ul><li>top() </li></ul></ul><ul><ul><li>pop() </li></ul></ul>
  29. 29. Container Adapter: queue <ul><li>#include <queue> </li></ul><ul><li>Core Operations </li></ul><ul><ul><li>push() </li></ul></ul><ul><ul><li>front() </li></ul></ul><ul><ul><li>pop() </li></ul></ul><ul><ul><li>back() </li></ul></ul>
  30. 30. Container Adapter: priority queue <ul><li>priority_queue implements a queue from which elements are read according to their priority </li></ul><ul><li>#include <queue> </li></ul><ul><li>Core Operations </li></ul><ul><ul><li>push() </li></ul></ul><ul><ul><li>top() </li></ul></ul><ul><ul><li>pop() </li></ul></ul>
  31. 31. Iterators Basic Stuff
  32. 32. Iterator Interface <ul><li>Can iterate (navigate) over elements </li></ul><ul><li>All or part of the elements in an STL container </li></ul><ul><li>Fundamental Operations </li></ul><ul><ul><li>operator* </li></ul></ul><ul><ul><li>operator++ </li></ul></ul><ul><ul><li>operator== </li></ul></ul><ul><ul><li>operator!= </li></ul></ul><ul><ul><li>operator= </li></ul></ul>
  33. 33. Container Interface for Iterators <ul><li>Fundamental Container Operations for Iterators </li></ul><ul><ul><li>begin() </li></ul></ul><ul><ul><li>end() </li></ul></ul>
  34. 34. Basic Iterator Model <ul><li>A pair of iterators define a sequence </li></ul><ul><ul><li>The beginning (points to the first element – if any) </li></ul></ul><ul><ul><li>The end (points to the one-beyond-the-last element) </li></ul></ul>… begin: end: <ul><li>An iterator is a type that supports the “iterator operations” </li></ul><ul><ul><li>++ Go to next element </li></ul></ul><ul><ul><li>* Get value </li></ul></ul><ul><ul><li>== Does this iterator point to the same element as that iterator? </li></ul></ul><ul><li>Some iterators support more operations (e.g. --, +, and [ ]) </li></ul>
  35. 35. Use of Iterators <ul><li>std::list<char> coll; </li></ul><ul><li>std::list<char>::const_iterator pos; </li></ul><ul><li>for(pos = coll.begin(); pos != coll.end(); ++pos) </li></ul><ul><li>{ </li></ul><ul><li>cout << *pos << ‘ ‘; </li></ul><ul><li>} </li></ul>
  36. 36. Iterator Categories <ul><li>Bidirectional Iterators </li></ul><ul><li>Random Access Iterators </li></ul>
  37. 37. Iterator Adaptors <ul><li>Insert Iterators </li></ul><ul><li>Stream Iterators </li></ul><ul><li>Reverse Iterators </li></ul>
  38. 38. Algorithms Basic Stuff
  39. 39. Algorithm Overview <ul><li>STL provides several standard algorithms </li></ul><ul><li>Algorithms are not member functions of containers </li></ul><ul><li>Algorithms are global functions that operate with iterators </li></ul><ul><li>Algorithms can operate on various types </li></ul><ul><li>Algorithms do not belong to Object Oriented Paradigm, they belong to Generic Programming Paradigm </li></ul><ul><li>#include <algorithm> </li></ul>
  40. 40. References <ul><li>The C++ Standard Library: A Tutorial and Reference </li></ul><ul><ul><li>Nicolai M. Josuttis </li></ul></ul><ul><li>Effective STL </li></ul><ul><ul><li>Scott Meyers </li></ul></ul><ul><li>C++ Template Metaprogramming </li></ul><ul><ul><li>Abrahams & Gurtovoy </li></ul></ul><ul><li>C++ Templates: The Complete Guide </li></ul><ul><ul><li>David Vandevoorde & Nicolai M. Josuttis </li></ul></ul>
  41. 41. Thank You

×