More on the STL - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

More on the STL

Description:

vector, list, deque. Adapter containers are restricted versions of other containers ... list T can expand to hold as many items as needed. O(1) delete of any item ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 13
Provided by: wat88
Category:
Tags: stl | list | more

less

Transcript and Presenter's Notes

Title: More on the STL


1
More on the STL
  • vectorltTgt
  • listltTgt
  • stackltTgt
  • queueltTgt
  • priority_queueltTgt

2
STL Container Classes
  • Sequence containers organize items by position
    1st, 2nd, 3rd, ., last
  • vector, list, deque
  • Adapter containers are restricted versions of
    other containers
  • stack, queue, priority_queue
  • Associative containers organize items based on a
    key value
  • set, map, multiset, multimap

3
vectorltTgt vs. listltTgt
  • vectorltTgt
  • can expand to hold as many items as needed
  • O(1) insert/delete at end of the list
  • O(n) insert/delete at position i in the list
  • indexing (veci) gives gives O(1) access to any
    position
  • listltTgt
  • can expand to hold as many items as needed
  • O(1) delete of any item
  • O(1) insert before any item
  • no indexing cannot access item at position i in
    O(1)

4
Use different storage structures
vectorltTgt items stored in an array (contiguous
storage)
0
8
1
2
3
4
5
9
10
11
6
7
listltTgt items stored in a linked list
(non-contiguous storage)
0
1
2
3
8
4
5
6
7
5
Partial listltTgt API
  • list() //construct an
    empty list
  • push_back(item) //add item at end of list
  • pop_back() //remove item at end of
    list
  • push_front(item) //add item at front of list
  • pop_front() //remove item at front of
    list
  • size() //return number of
    items in list
  • back() //return item at end
    of list
  • front() //return item at
    front of list

6
STL iterators
  • an iterator is a generalization of a pointer
  • used to access an element in the container
  • each STL container class has a nested iterator
    class associated with it
  • vectorltTgtiterator
  • listltTgtiterator
  • STL container classes have methods which return
    iterators
  • begin() //returns an iterator accessing first
    element
  • end() //returns an iterator that is past
    the last //element

7
iterator operations
  • iter //access next element
  • iter-- //access prior element
  • and ! //compare 2 iterators
  • iter //dereference the iterator

8
An Example
listltintgt myList // store some items in
myList listltintgtiterator iter iter
myList.begin() while (iter ! myList.end())
//process iter iter
9
STL Components
  • Container classes - templates for classes which
    hold a collection of elements
  • Algorithms - templates for functions which
    operate on a range of elements from a container
  • range is specified by iterators
  • Iterators
  • give access to the element in a container
  • allow for movement from one element to another

10
STL Algorithms
  • designed to operate on a sequence of elements
    rather than on a specific container
  • the sequence is designated by two iterators
  • all container classes have the following two
    methods
  • begin( ) - returns an iterator positioned
    at the container's
    first element
  • end( ) - returns an iterator
    positioned past the
    container's last element (past-the-end)
  • C.begin( ), C.end( ) specifies a sequence which
    contains all elements of the container C

11
STL Algorithms
  • some examples of STL algorithms
  • find(iter1, iter2, value) //returns an
    iterator
  • max_element(iter1, iter2) //returns an iterator
  • sort(iter1, iter2) //sorts
    using lt
  • for_each(iter1, iter2, F) //applies F to
    every
    //item
  • see STL Programmer's Guide link on home page

12
include ltiostreamgt include ltvectorgt include
ltalgorithmgt include ltcstdlibgt using namespace
std void set(int val) void display(int val)
int main( ) vectorltintgt A(5)
for_each(A.begin( ), A.end( ), set) // would
not work if vectorltintgt A used
for_each(A.begin( ), A.end( ), display) cout
ltlt endl sort(A.begin( ), A.end( )) //
operatorlt must be defined for A's element type
for_each(A.begin( ), A.end( ), display) cout
ltlt endl return 0 void set(int val)
val rand( ) void display(int val) cout
ltlt " " ltlt val
Write a Comment
User Comments (0)
About PowerShow.com