friends & standard template library csci3110 advanced data structures lecturer: dr. carroll and...

30
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Upload: clement-eaton

Post on 13-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Friends &Standard Template

LibraryCSCI3110 Advanced Data Structures

Lecturer: Dr. Carroll and Nan Chen

Page 2: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Simple definition of friends

• Friends are functions or classes declared with the friend keyword.

• friends are particularly common in cases of operator overloading because it is often necessary for an overloaded operator to have access to the internals of the classes that are arguments to the operator.

Page 3: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

What is C++ friend function

• A friend function of a class can be defined outside that class’s scope A friend function has the right to access all private and protected members of the class.

Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

Page 4: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Who can access what

Access public protected privatemembers of the same class yes yes yesmembers of derived class yes yes nonot members yes no noFriends yes yes yes

Page 5: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

How to call a friend function ?

• Friends are not in the class's scope, and

• They are not called using the member-selection operators (. and –>) unless they are members of another class.

• The friend declaration can be placed anywhere in the class declaration. It is not affected by the access control keywords.

Page 6: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Trade offs

Cons:C++ friends violates the OOP principle of information hide ;

Pros:More flexibility through friends

Page 7: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Examples

1) function2) class3) member function

Page 8: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Rule1: Friendships are not mutual If class A is a friend of class B, class B is not automatically a friend of class A

Exercise :Discuss with your neighbor about an example of “Friendships are not mutual” ?

Page 9: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Rule2: Friendship is not chained

• If class A is a friend of class B, and class B is a friend of class C, class A is not automatically a friend of class C.

Exercise :Discuss with your neighbor about an example of“Friendship is not chained ” ?

Page 10: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Rule3: Friendships are not inheritedA friend of Base class is not automatically a friend of Derived class and vice versa;

Exercise :Discuss with your neighbor about an example of “Friendship is not inherited” ?

Page 11: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Implications of friend Relationship

Page 12: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

History of STL

• 1979 Alexander Stepanov began working out his initial ideas of generic programming.• 1983, Ada provided support STL• 1985, Eiffel provided 1st OOP support STL• 1994 July, C++ ANSI/ISO committee approved the addition of STL

Page 13: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Container Type

• array (c++11)• vector• deque• forward_list (c++11)• list

• stack• queue• priority_queue

• set / multiset• map / multimap

• unordered_set / unordered_multiset • unordered_map /

unordered_multimap

Sequence Container Container Adapter

Associative ContainerUnordered Associative Container

Page 14: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Review STL container types

• array : fixed-size sequence containers

• vector:is a dynamic array capable of growing as needed to contain its elements (different from the vector in math)

Page 15: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Vector declaration and initialization

vector<int> vectorOne; //vectorOne: {}vector<int> vectorTwo(5); //vectorTwo: {0, 0, 0, 0, 0}vector<int> vectorThree(5, 3); //vectorThree: {3, 3, 3, 3, 3}vector<int> v2 = v1; vector<int> v3(v1); vector<int> v4(vectorTwo.begin(), vectorTwo.end() );

Page 16: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Destructor

v1.~vector<int>( )

L1.~list<int>( )

Page 17: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Random access of vector

• MyVector[0] -- no bound checking • MyVector.at(1) -- bound checking

Page 18: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Operation on the containers (vector,list)int size( )void clear( ) or c.erase( c.begin( ), c.end( ) ). bool empty( )

void push_back(…)void pop_back()

Page 19: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Review STL container types

• list: special type of sequence container called a doubly linked list where each element in the container contains pointers that point at the next and previous elements in the list. Lists only provide access to the start and end of the list -- there is no random access provided.

Page 20: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Operations only work in List container• void push_front(…)

• void pop_front()

Page 21: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

More container types

• deque:(pronounced “deck”) is a double-ended queue, implemented as a dynamic array that can grow from both ends

• fordward_listare sequence containers that allow constant time insert and erase operations anywhere within the sequence. Forward lists are implemented as singly-linked lists

Page 22: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

More container types

• Stack • Queue • Priority Queue

Page 23: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Iterator of container

• begin( ): returns an appropriate iterator representing the first item in the container.

• end( ): returns an appropriate iterator representing the end marker in the container (i.e., the position after the last item in the container).

Page 24: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Print out all elements in a vector without iterator

for( int i = 0; i != v.size( ); ++i ) cout << v[ i ] << endl;

Page 25: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Print out all elements in a vector with iteratorfor( vector<int>::iterator itr = v.begin( ); itr != v.end( ); itr++)

cout << *itr << endl;

Page 26: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Methods on iterator

++iter (or iter++):advances the iterator itr to the next location. *iter : returns a reference to the object stored at iterator itr’s location. ==!=

Page 27: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Visiting all elements in the container

vector<int>::iterator itr = v.begin( );

while( itr !=v.end( ) ) cout << *itr++ << endl; //same as//cout<< *itr <<endl;// itr ++

Page 28: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Associative Containers

Set:is a container that stores unique elements, with duplicate elements disallowed. The elements are sorted according to their values.

Map:is a set where each element is a pair, called a key/value pair. The key is used for sorting and indexing the data, and must be unique. The value is the actual data.

Page 29: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Associative Containers

Multiset:-is a set where duplicate elements are allowed.

Multimap:-is a map that allows duplicate keys.

Page 30: Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen

Tips for (STL) homework

1) array < T, N> mySTLarray;2) forword_list has no size()3) Not every container has iterator (array, stack , queue, pq)4) itr != AnotherList.end() 5) for (int i= 0; i <myStack.size() ; i++)

{myStack.top() ;myStack.pop() ;

}