1 / 6

Standard Template Library (STL)

Standard Template Library (STL). Generic Templates. For container classes like Stack or Queue Algorithms are the same for elements of int, float, string, or objects Standard Template Library provides generic templates for container classes

Download Presentation

Standard Template Library (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. Standard Template Library (STL)

  2. Generic Templates • For container classes like Stack or Queue • Algorithms are the same for elements of int, float, string, or objects • Standard Template Library provides generic templates for container classes • It also provides generic templates for algorithms like search, sort.

  3. Stack Member Functions • bool empty() • Returns true if stack is empty; false, otherwise • int size() • Returns the size • T top() • Access top element • void push(T item) • Insert element at top • void pop() • Remove element from top

  4. Using STL Template Stack • Include STL stack. • #include <stack> • Instantiate stack of type T. • stack myStack<string>; • Use myStack as usual. string name1 = “Anne”; string name2 = “Ben”; myStack.push(name1); // insert at top myStack.push(name2);cout << myStack.top();

  5. QueueMember Functions • bool empty() • Returns true if queue is empty; false, otherwise • int size() • Returns the size • T front () • Access front element • T back() • Access back element • void push(T item) • Insert element at back • void pop() • Remove element from front

  6. Using STL Template Queue • Include STL queue. • #include <queue> • Instantiate queue of type T. • stack myQ<string>; • Use myQ as usual. string name1 = “Anne”; string name2 = “Ben”; myQ.push(name1); // insert at back myQ.push(name2);cout << myQ.front ();

More Related