31251 Lecture Notes - Lecture 2: Include Directive, Priority Queue, The Queue
Week 2 Lecture
Simple I/O
• There are a number of ways to handle basic input and output in C++.
• i ad out are i the iostrea lirary ad are iterpreted as streas of
information.
• The same abstractions are used for the input and output of files. For file I/O, the
lirary fstrea is used ith ofstrea used for ritig ad ifstrea for readig.
Classes and Destructors
• ~ is a destrutor ad is used to aage eory, he oied ith the delete
keyword. Without deleting the data, the heap memory is not deallocated and there
is a memory leak.
Exceptions
• Ulike Jaa, C++ a ath or thro aythig…it is ot liited or restrited i
any way.
Queues
• The Queue is the basic FIFO structure which keeps things in order.
• In an ADT queue, the queue is built through restricting features.
• Deque is a less restricted queue, however not quite as unrestricted as a list. It is
double-ended and allows you to remove data at both ends. It allows treatment as
queue or stack, and allows more flexibility which is more useful in practice.
• A priority queue is a queue, but elements are inserted by priority, and come out in a
priority order. It may need searching or sorting to be done in the middle to make it
work.
Stacks
• A stack is like a queue but it has a last in first out structure (like a stack of things).
You a add to the top ad oly reoe fro the top.
Week 3 Lecture
Vectors
• Vectors are the middle ground, in between arrays and lists.
• Vectors offer a resizable array, similar to an Arraylist in Java.
• They are a teplated lass, hih has a ariety of ethods ithi the lass.
• One particular feature of the vector class is operator overloading, which allows you
to change the way an operator works (reusing the same symbol). For example, in the
vector class, square brackets allow you to treat the vector as an array.
• push_ak is a ethod that puts soethig o the ed.
• In the vector class, capacity and size are different things. Capacity shows the
potential capability, and size shows what it currently holds. Capacity only grows
he ore eleets are added, ad does’t shrik he eleets are reoed.
There is a function which can shrink the capacity so not as much memory/space is
used.
• When you run out of space within the vector, the capacity is doubled.
• In C++ libraries, type definitions are used to shorten std::etc::pointer.
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
For file i/o, the li(cid:271)rary (cid:862)fstrea(cid:373)(cid:863) is used (cid:449)ith (cid:862)ofstrea(cid:373)(cid:863) used for (cid:449)riti(cid:374)g a(cid:374)d (cid:862)ifstrea(cid:373)(cid:863) for readi(cid:374)g. Classes and destructors (cid:862)~(cid:863) is a destru(cid:272)tor a(cid:374)d is used to (cid:373)a(cid:374)age (cid:373)e(cid:373)ory, (cid:449)he(cid:374) (cid:272)o(cid:373)(cid:271)i(cid:374)ed (cid:449)ith the delete keyword. Without deleting the data, the heap memory is not deallocated and there is a memory leak. Exceptions: u(cid:374)like ja(cid:448)a, c++ (cid:272)a(cid:374) (cid:862)(cid:272)at(cid:272)h(cid:863) or (cid:862)thro(cid:449)(cid:863) a(cid:374)ythi(cid:374)g it is (cid:374)ot li(cid:373)ited or restri(cid:272)ted i(cid:374) any way. Queues: the queue is the basic fifo structure which keeps things in order, deque is a less restricted queue, however not quite as unrestricted as a list. In an adt queue, the queue is built through restricting features. double-ended and allows you to remove data at both ends. It allows treatment as queue or stack, and allows more flexibility which is more useful in practice: a priority queue is a queue, but elements are inserted by priority, and come out in a priority order.