template lecture 9

template lecture 9 - Lecture 9 Part I Standard Template...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 9 – Part I Standard Template Library (STL)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Outline STL Introduction Containers STL container categories Fundamental containers Iterators and generic algorithms Case Study
Background image of page 2
Introduction to STL Data Structures How to store collections of data Fundamental part of CS curriculum A reusable library of data structures allows programs to be developed more easily STL C++ library of data structures and their associated algorithms Generic algorithms: these algorithms can be used with many different types of containers.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
STL Container Categories Sequential containers Fundamental containers Vector, list, and deque Adapters class Layered on top of the fundamental containers Stack, queue, priority queue Associative containers Set and map Very simple databases
Background image of page 4
Fundamental Containers vector -- #include <vector> list -- #include <list> Deque -- #include <deque> Double ended queues Common Operations All three fundamental containers support a set of common operations but with different time complexity Some operations may not be supported by a particular container at(int) is not supported by the list
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Common Operations (1) Operation Vector List Deque container() O(1) O(1) O(1) at(int) O(1) O(1) back() O(1) O(1) O(1) begin() O(1) O(1) O(1) capacity() O(1) empty() O(1) O(1) O(1) end() O(1) O(1) O(1) erase(iterator) O(n) O(1) O(n)
Background image of page 6
Common Operations (2) Operation Vector List Deque front() O(1) O(1) O(1) insert(iter,v) O(n) O(1) O(n) pop_back(v) O(1) O(1) O(1) pop_front(v) O(1) O(1) push_back(v) O(1) O(1) O(1) push_front(v) O(1) O(1) size() O(1) O(1) O(1) operator[] O(1) O(1) operator= O(n) O(n) O(n)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
vector Container An indexed container Fundamental operation Subscripts using operator[] Elements can be added to the end using member function push_back Does not support pop_front() and push_front(…) Case study Classic algorithm, sieve of Eratosthenes A list of all numbers up to the bound represented by a Boolean vector
Background image of page 8
sieve.cpp const int size = 100; int j, k; vector<bool> sieve(size, true); for (int j=2; j*j<size; j++) if (sieve[j]) for (int k=j+j;k<size; k+=j) sieve[k]=false; for (j=2; j<size; j++) if (sieve[j]) cout << j << “is prime\n”;
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Implementation of Vector template <typename T> class vector { public: typedef T* iterator; vector(); private: T* buffer; int current_size; int current_capacity; };
Background image of page 10
list Container Implemented as linked lists Supports constant time insertion into the middle of a collection Does not support these common operations at(…), capacity(), resize(…), operator[] Supports additional operations that are not recognized by the other two containers merge(list), remove(value), … Example A simple inventory management system
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Additional Operations Supported by list Container merge(list) //merge the current list with the argument list remove(v) //remove all instances of v
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 40

template lecture 9 - Lecture 9 Part I Standard Template...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online