Download presentation
Presentation is loading. Please wait.
Published byClement Eaton Modified over 9 years ago
1
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen
2
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.
3
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.
4
Who can access what Accesspublicprotectedprivate members of the same classyesyesyes members of derived classyesyesno not membersyesnono Friendsyesyesyes
5
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.
6
Trade offs Cons: C++ friends violates the OOP principle of information hide ; Pros: More flexibility through friends
7
Examples 1)function 2)class 3)member function
8
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” ?
9
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 ” ?
10
Rule3: Friendships are not inherited A 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” ?
11
Implications of friend Relationship
12
History of STL 1979 Alexander Stepanov began working out his initial ideas of generic programming. 1983, Ada provided support STL 1985, Eiffel provided 1 st OOP support STL 1994 July, C++ ANSI/ISO committee approved the addition of STL
13
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 Container Unordered Associative Container
14
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)
15
Vector declaration and initialization vector vectorOne; //vectorOne: {} vector vectorTwo(5); //vectorTwo: {0, 0, 0, 0, 0} vector vectorThree(5, 3); //vectorThree: {3, 3, 3, 3, 3} vector v2 = v1; vector v3(v1); vector v4(vectorTwo.begin(), vectorTwo.end() );
16
Destructor v1.~vector ( ) L1.~list ( )
17
Random access of vector MyVector[0] -- no bound checking MyVector.at(1) -- bound checking
18
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()
19
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.
20
Operations only work in List container void push_front(…) void pop_front()
21
More container types deque: (pronounced “deck”) is a double-ended queue, implemented as a dynamic array that can grow from both ends fordward_list are sequence containers that allow constant time insert and erase operations anywhere within the sequence. Forward lists are implemented as singly-linked lists
22
More container types Stack Queue Priority Queue
23
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).
24
Print out all elements in a vector without iterator for( int i = 0; i != v.size( ); ++i ) cout << v[ i ] << endl;
25
Print out all elements in a vector with iterator for( vector ::iterator itr = v.begin( ); itr != v.end( ); itr++) cout << *itr << endl;
26
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. == !=
27
Visiting all elements in the container vector ::iterator itr = v.begin( ); while( itr !=v.end( ) ) cout << *itr++ << endl; //same as //cout<< *itr <<endl; // itr ++
28
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.
29
Associative Containers Multiset: -is a set where duplicate elements are allowed. Multimap: -is a map that allows duplicate keys.
30
Tips for (STL) homework 1) array 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() ; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.