Title: CS1102C Tutorial
1CS1102C Tutorial
- TA Pavel Korshunov
- Tutorial 3
- 11 Sep 2006
http//www.comp.nus.edu.sg/pavelkor/cs1102c/
2STL Iterator
- Iterator is just a pointer to the element
- vectorltintgtiterator viter
- You can think of it astypedef int
vectorltintgtiterator - It is a funny name for simple thing
3STL vector
http//www.comp.nus.edu.sg/pavelkor/cs1102c/
- It is a dynamic array
- Its size can vary on the fly
- Gives you some additional useful functions
- Has the best of normal array and linked list
4STL vector
http//www.comp.nus.edu.sg/pavelkor/cs1102c/
- Create vectorltintgt vec
- Initialize and assign values like in array
vec23 or insert() element in the middle or
push_back() inserts in the end - Access elements like in array veci or
vec.at(i) or - To go through elements use iterators
- vectorltintgtiterator viter vec.begin()
//gives you the first element of vector - while(viter ! vec.end())
- cout ltlt viter //viter gives the current
element of vector - viter //like in pointers to arrays shifts
to the next element -
3
begin()
end()
5Linked List
http//www.comp.nus.edu.sg/pavelkor/cs1102c/
- Linked List is a list of nodes that are linked
through pointers - Each node has
- some data (some precious value inside)
- a pointer to the next node
- One special pointer head. It points to 1st node
- We need Linked List to store many objects of the
same type similar to array
data
6Linked List
http//www.comp.nus.edu.sg/pavelkor/cs1102c/
Called tail
node
head
data1
data2
data3
- You must understand the concept well!
- Understand pointers -gt understand linked list
- Always draw it on the paper first
7Double Linked List
http//www.comp.nus.edu.sg/pavelkor/cs1102c/
head
tail
node
data1
data2
data3
- Each node has
- Pointer to the next node
- Pointer to the previous node
- List has
- Pointer head giving beginning of the list
- Pointer tail giving the end of the list
8STL List and Vector
- Vector is array of dynamic size
- List is double linked list
- Implementations are different but
- Methods to work with them are the same
- To write a program, you dont even need to know
what is List and Vector
9STL Vector
http//www.cppreference.com/cppvector/index.html
assign() assign elements to a vector at()
returns an element at a specific location back()
returns a reference to last element of a vector
begin() returns an iterator to the beginning of
the vector clear() removes all elements from the
vector empty() true if the vector has no
elements end() returns an iterator just past the
last element of a vector erase() removes
elements from a vector front() returns a
reference to the first element of a vector
insert() inserts elements into the vector
pop_back() removes the last element of a vector
push_back() add an element to the end of the
vector size() returns the number of items in the
vector
10STL List
http//www.cppreference.com/cpplist/index.html
assign() assign elements to a vector back()
returns a reference to last element of a vector
begin() returns an iterator to the beginning of
the vector clear() removes all elements from the
vector empty() true if the vector has no
elements end() returns an iterator just past the
last element of a vector erase() removes
elements from a vector front() returns a
reference to the first element of a vector
insert() inserts elements into the vector
pop_back() removes the last element of a vector
push_back() add an element to the end of the
vector pop_front() removes the first element of
the list push_front() add an element to the
front of the list size() returns the number of
items in the vector
11Exceptions
http//www.comp.nus.edu.sg/pavelkor/cs1102c/
- try, throw, catch -gt like a ball game
- try
- Whatever is inside will not crash -gt cool!
- If error inside than stop running and pass to
- catch()
- Catches only exceptions of specified types and
does something about it (for example output error
code and exit) - You can catch all exceptions if type (see book
for details) - catch (exception e)
- cout ltlt now my program never crashes ltlt endl
12Exceptions
http//www.comp.nus.edu.sg/pavelkor/cs1102c/
- throw exception (exception is just a class)
- Any class can be thrown as an exception
- Programmers agreed that exception class should
have method what() - assert (false) is like try, catch and exit in
one place
13Exceptions
http//www.comp.nus.edu.sg/pavelkor/cs1102c/
- void MyMethod() throw (MyException)
- // lt0 is error! MyException() constructor is
called and object is thrown out - if(my_variable lt 0) throw MyException()
-
- int main()
- try //trying to catch the error
- MyMethod() //and error happened
- catch (MyException e) //and errors is
caught.. - cout ltlt e.what()ltltendl //hey! What!? output
the message - return -1 //such a buggy program
-
- return 0
- //main finished
If an exception occurred and it is not in any of
catches than program crashes