03 Project1 the STL and You

Embed Size (px)

Citation preview

  • 8/12/2019 03 Project1 the STL and You

    1/23

    Project 1:

    The STL and You

    A quick intro to the STL to give you tools toget started with stacks and queues, without

    writing your own!

    Use a deque instead!Speed up your output!

  • 8/12/2019 03 Project1 the STL and You

    2/23

    Important Note

    These are not the only data structures youwill need for Project 1!

    This is intended to help you with theRouting Schemes portion, where youhave to remove/add when searching fromthe current location

    See page 4 in the Project 1 specificationfor more details

    2

  • 8/12/2019 03 Project1 the STL and You

    3/23

    STL Containers

    The STL containers are implemented astemplate classes

    There are many types available, but someof them are critical for Project 1 Stack Queue Deque (can take the place of both stack and

    queue)

    Common/similar member functions3

  • 8/12/2019 03 Project1 the STL and You

    4/23

    The STL Stack

    You must #include Create an object of template class, for

    example:stack values;

    You can push an element onto the top ofthe stack, look at the top element of thestack, and pop the top element from thestack

    4

  • 8/12/2019 03 Project1 the STL and You

    5/23

    The STL Queue

    You must #include Create an object of template class, for

    example:queue values;

    You can push an element onto the back ofthe queue, look at the front element of thequeue, and pop the front element from thequeue

    5

  • 8/12/2019 03 Project1 the STL and You

    6/23

    Common Member Functions

    The stack and queue containers use manyof the same member functionsvoid push( elem ) add element to container void pop() remove the next element from thecontainer bool empty() returns true/false

    The only difference is which end thepush() operation affects

    6

  • 8/12/2019 03 Project1 the STL and You

    7/23

    Different Member Functions

    The stack uses: top() look at the next element (the top ofthe stack)

    The queue uses: front() look at the next element (thefront of the queue)

  • 8/12/2019 03 Project1 the STL and You

    8/23

    Using Stack/Queue in Project 1

    If you want to use stack and queue for thesearching in Project 1, create one of eachtype

    Must use them inside a single function(which will probably be long) Cannot make a template function, due to

    .top() versus .front()

    8

  • 8/12/2019 03 Project1 the STL and You

    9/23

    The Deque Container

    The deque is pronounced deck Prevents confusion with dequeue (dee-cue)

    It is a double-ended queue Basically instead of being restricted to

    pushing or popping at a single end, youcan perform either operation at either end

    #include

    9

  • 8/12/2019 03 Project1 the STL and You

    10/23

    Deque Member Functions

    The deque provides the following:void push_front( elem ) front()

    void pop_front()

    void push_back( elem ) back()void pop_back()

    bool empty()

    10

  • 8/12/2019 03 Project1 the STL and You

    11/23

    Using a Deque in Project 1

    If you want to use a single data structurefor searching in Project 1, use a deque

    When youre supposed to use a stack,push_front()

    For a queue, push_back()

    Always use front() and pop_front()

    11

  • 8/12/2019 03 Project1 the STL and You

    12/23

    More Information

    More information on these STL data typescan be found in the Josuttis textbook Stacks and queues can be found in sections

    12.1 and 12.2, respectively Deques are in section 7.4 Vectors in section 7.3

    12

  • 8/12/2019 03 Project1 the STL and You

    13/23

    3D Data Structure

    Create a *** (triple pointer) Create a nested vector

    Use the . resize() member function on eachdimension before reading the file

    Create a nested array For any choice, exploit locality of reference

    Use subscripts in this order:[level][row][col]

  • 8/12/2019 03 Project1 the STL and You

    14/23

    Creating/Initializing a Vector

    Here is an example of creating andinitializing a 2D vector, with 10 rows and 20columns, all initialized to -1:

    int rows = 10;int cols = 20;

    vector twoDimArray(rows,vector(cols,-1));

  • 8/12/2019 03 Project1 the STL and You

    15/23

    Speeding up Output

    C++ cout can be slow, but there areseveral ways to speed it up: Use '\n' Use string streams Turn off synchronization of C/C++ I/O

  • 8/12/2019 03 Project1 the STL and You

    16/23

    '\n' versus endl

    Whenever the endl object is sent to astream, after displaying a newline it alsocauses that stream to flush Same as calling stream.flush()

    Causes output to be written to the harddrive RIGHT NOW

    Doing this after every line takes up time Using '\n' does not flush

  • 8/12/2019 03 Project1 the STL and You

    17/23

    String Streams

    Basically a string object that you can use > with (most of this example is output only)

    #include

    Create an object:ostringstream os;

    Send things to it:

    os

  • 8/12/2019 03 Project1 the STL and You

    18/23

    Synchronized I/O

    What if you used both printf() (from C)and cout (C++) in the same program? Would the output order always be the same? What if you were reading input?

    To insure consistency, C++ I/O issynchronized with C-style I/O

    If youre only using one method, turning offsynchronization saves time

  • 8/12/2019 03 Project1 the STL and You

    19/23

    Turning off Synchronized I/O

    Add the following line of code in yourprogram

    It must appear before ANY I/O is done!

    ios_base::sync_with_stdio(false);

  • 8/12/2019 03 Project1 the STL and You

    20/23

    Warning!

    If you turn off synchronized I/O, and thenuse valgrind , it will report memory leak

    The only way to get accurate feedbackfrom valgrind is to:1. Comment out the call to sync_with_stdio()

    2. Recompile3. Run valgrind4. Un-comment the sync/false line5. Proceed to edit/compile/submit/etc.

  • 8/12/2019 03 Project1 the STL and You

    21/23

    Finding the Path

    Once you reach the goal, you have todisplay the path that found it Either on top of the map, or in list mode

    The map, stack/queue/deque do not havethis information

    You have to save it separately!

  • 8/12/2019 03 Project1 the STL and You

    22/23

  • 8/12/2019 03 Project1 the STL and You

    23/23

    Backtracking Example

    When youre at the buck, howdid you get here? What squarewere you on when the Bsquare was added to thestack/queue/deque? Every square must remember the

    previous square

    If youre using queue -basedrouting, it was the square to thewest

    S B