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

Post on 13-Jan-2016

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Friends &Standard Template

LibraryCSCI3110 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.

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.

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

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.

Trade offs

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

Pros:More flexibility through friends

Examples

1) function2) class3) member function

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” ?

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 ” ?

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” ?

Implications of friend Relationship

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

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

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)

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() );

Destructor

v1.~vector<int>( )

L1.~list<int>( )

Random access of vector

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

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()

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.

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

• void pop_front()

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

More container types

• Stack • Queue • Priority Queue

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).

Print out all elements in a vector without iterator

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

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

cout << *itr << endl;

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. ==!=

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 ++

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.

Associative Containers

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

Multimap:-is a map that allows duplicate keys.

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() ;

}

top related