Presentation is loading. Please wait.

Presentation is loading. Please wait.

14 – Sequential Containers

Similar presentations


Presentation on theme: "14 – Sequential Containers"— Presentation transcript:

1 14 – Sequential Containers
4.1 Template Classes and the Vector 4.2 Applications of Vectors 14 – Sequential Containers

2 Attendance Quiz #13 Sequential Containers

3 Tip #14: eof in Loops Iterators Anytime you see istream::eof() as a loop condition, the code is almost certainly wrong! Why?? Because iostream::eof will only return true after reading the end of the stream. It does not indicate, that the next read will be the end of the stream. Oh crap, now we read the end and *only* now the eof bit will be set (as well as the fail bit) Bad while(!inStream.eof()) { int data; // yay, not end of stream yet, now read ... inStream >> data; // do stuff with (now uninitialized) data } Good int data; while(inStream >> data) { // do stuff with correctly initialized data (hopefully) } We can be sure that the read was successful. if it wasn't, the returned stream from operator>> would be converted to false and the loop wouldn't even be entered.

4 Big-O Example T(n) = n2 + 3n + 9 void T(int n) {
Program Correctness void T(int n) { for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) // Simple Statement; } // Simple Statement 1; // Simple Statement 2; // Simple Statement 3; // ... // Simple Statement 8; // Simple Statement 9; The outer loop will execute n times. The inner loop will execute n times for each outer loop iteration. We can conclude that the relationship between processing time and n (the number of Simple Statements processed) is: T(n) = n2 + 3n + 9 The growth rate of f(n) will be determined by the fastest growing term, which is the one with the largest exponent. In the example, an algorithm of O(n2 + 3n + 9) is more simply expressed as O(n2). The loop will execute 3 Simple Statements n times Finally, 9 Simple Statements are executed.

5 Formal Definition of Big-O
Program Correctness T(n) is the Big-O of f(n) if there exists two constants, n0 and c, greater than zero, such that for all n > n0, c  T(n) >= f(n). In other words, as n get sufficiently large (larger than n0), there is some constant c for which the processing time will always be greater than or equal to f(n). Given f(n) = n2 + 3n + 9, show that this is O(n2). Find constants n0 and c such that for all n > n0, cn2 > n2 + 3n + 9 (ie. find the point where cn2 = n2 + 3n + 9). Let n = n0, and solve for c c = / n0 + 9 / n02 When n0 is 3, this gives us a c of 3. So, 3n2 > n2 + 3n + 9 for all n > 3

6 Formal Definition of Big-O
Program Correctness An algorithm of O(n2 + 3n + 9) is more simply expressed as O(n2) 3n2 n > n0, c  T(n) >= f(n) n2 + 3n + 9

7 Growth Rates Program Correctness The growth rate of f(n) will be determined by the fastest growing term, which is the one with the largest exponent. In the example, an algorithm of O(n2 + 3n + 9) is more simply expressed as O(n2). In general, it is safe to ignore all constants and to drop the lower-order terms when determining the order of magnitude. Basic Rules: Nested loops are multiplied together. Sequential loops are added. Only the largest term is kept, all others are dropped. Constants are dropped. Conditional checks are constant (i.e. 1).

8 Common Growth Rates Program Correctness

9 Effects of Different Growth Rates
Program Correctness Algorithms with exponential and factorial growth rates have an effective practical limit on the size of the problem they can be used to solve. With an O(2n) algorithm, if 100 inputs takes an hour then, 101 inputs will take 2 hours 105 inputs will take 32 hours 114 inputs will take 16,384 hours (almost 2 years!) Encryption algorithms take advantage of this characteristic: Some cryptographic algorithms can be broken in O(2n) time, where n is the number of bits in the key. A key length of 40 is considered breakable by a modern computer. A key length of 100 bits will take a billion-billion (1018) times longer than a key length of 40.

10 What is the Big-O of the following:
// #1 for (int i = 0; i < n; i++) { cout << i << endl; } // #5 for (int i = 0; i < size; i++) { cout << arr[i] << endl; } O(n) O(n) // #2 { cout << i << endl; } O(1) // #6 for (int i = 0; i < size; i++) { cout << a[i] << endl; } for (int j = 0; j < size; j++) cout << a[i] + a[j] << endl; // #3 for (int i = 0; i < n + 1; i++) { for (int j = i + 1; j < n; j++) //do swap stuff, constant time } O(n2) O(n log n) // #4 int fib(int num) { if (num <= 1) return num; return fib(num - 2) + fib(num - 1); } O(2n)

11 Sequential Containers
Chapter 4 Sequential Containers

12 Objectives Sequential Containers To become familiar with the Standard Template Library (STL) and template classes. To understand how to use a vector class and how to implement it using an array for storage. To understand the difference between a shallow copy and deep copy. To introduce linked lists and study the differences between single-, double-, and circular linked list data structures. To introduce structs. So far we’ve considered one data structure — the array. The C++ developers have provided more tools for programmers by providing a rich set of data structures written as C++ template classes. These template classes are all part of the Standard Template Library (STL). In this chapter we discuss the STL classes called sequences.

13 Sequence Classes Sequential Containers A sequence has the property that elements can be inserted or removed anywhere in the sequence, not just at the beginning or at the end. Some sequences are indexed, which means their elements can be accessed in arbitrary order (called random access) using a subscript to select an element. For other sequences you must start at the beginning and process the elements in order. Iterators facilitate sequential access and random access. The vector and list (linked list) classes implement the common interface requirements for sequential containers.

14 4.1, pgs. 232-236 4.1 Template Classes and the Vector Vector
Specification of the vector Class Function at and the Subscripting Operator 4.1, pgs

15 C++ Templates Sequential Containers Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. template <typename T1, typename T2> class MyPair { private: T1 name; T2 value; public: MyPair(T1 name, T2 value) this->name = name; this->value = value; } }; MyPair<string, int> myDog("Dog", 36); MyPair<double, double> myFloats(3.0, 2.18);

16 The Vector Sequential Containers A vector is a template class based on an array which means you can select its elements in arbitrary order as determined by the subscript value. A vector supports the following operations that an array does not: Increase or decrease its length. Insert an element at a specified position without writing code to shift the other elements to make room. Remove an element at a specified position without writing code to shift the other elements to fill in the resulting gap. Vectors are used most often when a programmer wants to add new elements to the end of a list but still needs the capability to access, in arbitrary order, the elements stored in the list.

17 Vector Size Sequential Containers The size of a vector automatically increases as new elements are added to it, and the size decreases as elements are removed. If an insertion is not at the end of the vector, the existing entries in the vector are shifted automatically to make room for the entry being inserted. Similarly, if an element other than the last one is removed, the other entries are shifted automatically to close up the space that was vacated. The number of elements a vector currently contains is returned by the member function size. Each vector object also has a capacity, which is the number of elements it can store. When a vector’s size is equal to its capacity, the capacity is increased automatically.

18 String Vector Sequential Containers vector<string> my_vector;
my_vector.push_back("Bashful"); my_vector.push_back("Awful"); my_vector.push_back("Jumpy"); my_vector.push_back("Happy"); my_vector.insert(2, "Doc"); my_vector.push_back("Dopey"); my_vector.erase(1); my_vector[2] = "Sneezy";

19 Specification of the vector Class
Sequential Containers Because Item_Type is determined when the vector is created, the actual argument types and return types for these functions may be different for different instantiations of the vector class 2 entries for operator [] and member function at - const values can’t be changed. Function at validates the index The size_t type is an unsigned int; we use it here because all vector indexes must be nonnegative

20 Function at and Subscript Operator
Sequential Containers The non-const operators reference an element of the vector whose value can be changed. The const subscripting operator (“[]”) also returns a reference to element of the vector but the value cannot be changed. Both of the following statements retrieve and display the value of element 2 of vector<int> v cout << v[2]; cout << v.at(2); Both of the following statements add 6 to the value of element 2 of vector v and assign that sum to element 3 of vector v v[3] = v[2] + 6; v.at(3) = v.at(2) + 6; In these statements, the reference to element 2 retrieves a value of vector v that is not changed In contrast, both references to element 3 change its value. The function at validates the index.

21 4.2 Applications of Vectors
4.2, pgs

22


Download ppt "14 – Sequential Containers"

Similar presentations


Ads by Google