T08_STLstr_t

T08_STLstr_t - Tutorial 8 COMP152 Spring 2010 STL and...

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

View Full Document Right Arrow Icon
STL and String Tutorial Tutorial 8 COMP152 Spring 2010
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 adapters Priority queue STL algorithms String streams Examples Case study: Text editing COMP152 2
Background image of page 2
STL's priority_queue Adapter A queue can be specified queue<T, C<T> > aQueue; C may be any container supporting push_back() and pop_front() The default container is vector Could also use priority_queue<T, deque<T> > aQueue; For the best performance, use class vector as the underlying container Enables insertions in sorted order and deletions from front Elements are inserted in priority order Highest-priority element will be the first to be removed Maintains sorted order via heapsort Comparison of elements is performed with comparator function object less<T> by default COMP152 3
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's priority_queue Adapter Operations (call functions of the underlying container) push insert element at appropriate location to maintain sorted order (calls push_back, then reorders elements with heapsort) pop remove highest-priority element (moves top element of heap to back, then calls pop_back) top returns reference to top element (calls front) empty determine if the priority_queue is empty (calls empty) size get the number of elements (calls size) Again, each common operations is implemented as an inline function COMP152 4
Background image of page 4
priorityqueue.cpp (1/2) Instantiate a priority_queue that stores double values using a vector as the underlying data structure Add elements to the priority_queue COMP152 5 std::priority_queue< double > priorities; // push elements onto priorities priorities.push( 3.2 ); priorities.push( 9.8 ); priorities.push( 5.4 );
Background image of page 5

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

View Full DocumentRight Arrow Icon
priorityqueue.cpp (2/2) Retrieve the highest-priority element in priorities for output Remove the highest-priority element in the priorities Sample Output: COMP152 6 cout << "Popping from priorities: "; // pop element from priority_queue while ( !priorities.empty() ) { cout << priorities.top() << ' '; // view top element priorities.pop(); // remove top element } // end while Popping from priorities: 9.8 5.4 3.2
Background image of page 6
STL Algorithms STL separates algorithms from the containers Elements of containers are accessed through iterators Much easier to add new algorithms STL algorithms do not depend on the implementation details of the containers on which they operate As long as the container’s (or array’s) iterators satisfy the requirements of the algorithm STL algorithms can work on C-style, pointer-based arrays, on STL containers and on user-defined data structures COMP152 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

T08_STLstr_t - Tutorial 8 COMP152 Spring 2010 STL and...

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

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