Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Size Macro approach 19 Macros Macros do not perform error checking. // macro approach #define macro_array_size(array) (sizeof(array)/sizeof(array[0]))

Similar presentations


Presentation on theme: "Array Size Macro approach 19 Macros Macros do not perform error checking. // macro approach #define macro_array_size(array) (sizeof(array)/sizeof(array[0]))"— Presentation transcript:

1 Array Size Macro approach 19 Macros Macros do not perform error checking. // macro approach #define macro_array_size(array) (sizeof(array)/sizeof(array[0])) int ja[] = {0,1,2,3,4,5,6}; s = macro_array_size(ja); cout << “array_size = " << s << endl; //correct, returns 7 // int* pa = ja; int* pa = &ja[0]; s = macro_array_size(pa); //fails, returns 1 cout << "macro_array_size = " << s << endl; //fails, returns 1

2 Array Size Function template approach 19 array_size() Here we pass an array by reference to array_size() that extracts the size of the array and returns it template template inline size_t array_size(const T (&lhs)[N]) { return N; return N;} int ja[] = {0,1,2,3,4,5,6}; s = array_size(ja); cout << "function template approach: " << s << endl; //correct, returns 7 // int* pa = ja; int* pa = &ja[0]; s = array_size(pa); // fails, no matching function for call // to `array_size(int*&) cout << "macro_array_size = " << s << endl; // fails, no matching function for call // to `array_size(int*&)

3 Array Size Class template approach 19 N Here, N must be defined during the template class declaration, however. template template class CalcArraySize{ public: CalcArraySize(const T (&lhs)[N]) : size(N) { } CalcArraySize(const T (&lhs)[N]) : size(N) { } size_t getSize() { return size; } size_t getSize() { return size; }protected: size_t size; size_t size;}; int ia[10]; CalcArraySize a(ia); s = a.getSize(); 10 will be a fixed limit and that’s not exactly what we want.

4 Array Size Two arguments are still required 19 This works, but with two arguments required. Vector(T* a, unsigned s) : size(s) { data = new T[size]; data = new T[size]; for(int i=0; i < size; i++) { for(int i=0; i < size; i++) { data[i] = a[i]; }} int a[] = {11, 22, 33, 44, 55, 66, 77}; Vector v(a, sizeof(a)/sizeof(a[0]));

5 5 159.234LECTURE 17 159.234 LECTURE 17 STL 21 Standard Template Library

6 6STL 19 http://www.research.att.com/~bs/3rd_tour2.pdf Bjarne Stroustrup, The C++ Programming Language (3rd Ed.), A Tour of the Standard Library

7 7STL Some Terms What is STL? 19 STL STL is a set of general-purpose template classes, built using the template mechanism. What are its main parts? Containers Iterators Algorithms

8 8STLContainers 19 Containers Containers are objects that hold other objects. Sequence container: A container whose elements are kept in an ordinal sequence, like an array. The position of each element is independent of its value. vector (direct array access) deque (double-ended queue) list (linked-list, no indexed access, faster insertion/deletion) What are they? Different types of containers:

9 9STLContainers 19 Associative container: A container whose elements are kept in a sorted order for allowing efficient retrieval of values based on keys. The positions of the elements are completely determined by their values and those of the other elements in the container. map (look-up table structure) sets (represent mathematical sets using union and intersection operations)

10 10STL Preamble to Iterators 19 #include using namespace std; template // a generic function to print an array printArray void printArray( T* a, int len ){ for(int j=0; j < len; j++) { cout << a[j] << " "; } cout << endl; } Function Template

11 11STL Preamble to Iterators 19 int main(){ //typedef long int MyType; typedef long long int MyType; const int Length = 10; MyType array[Length]; cout << "Size of MyType is: " << sizeof(MyType ) << endl; for( int i=0;i<Length;i++) { array[i] = i + 1; } printArray( array, Length ); MyType This code will work regardless of what MyType actually is.

12 12 #include using namespace std; template // a generic func to print an array void printArray( T * a, int len ){ for(int j=0;j<len;j++) cout << a[j] << " "; cout << endl; } int main(){ //typedef long int MyType; typedef long long int MyType; const int Length = 10; array MyType array[Length]; cout << "Size of MyType is: " << sizeof(MyType ) << endl; for( int i=0;i<Length;i++){ array array[i] = i + 1; } array printArray( array, Length ); MyType *ptr; array MyType *start = array; array MyType *end = array + Length; for( ptr = start; ptr != end; ptr++){ *ptr * = -1; *ptr * = -1; cout << ptr << endl; cout << ptr << endl; } array, printArray( array, Length ); return 0; } Preamble to iterators: Pointer arithmetic Example Output: Size of MyType is: 8 1 2 3 4 5 6 7 8 9 10 0xbffffc10 0xbffffc18 0xbffffc20 0xbffffc28 0xbffffc30 0xbffffc38 0xbffffc40 0xbffffc48 0xbffffc50 0xbffffc58 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 This code will work regardless of what MyType actually is. operators +,The operators +, etc are all overloaded for pointers so that pointer arithmetic works as it should.

13 13 Vector class Vectors are dynamic arrays: arrays that can grow as needed. The template specification for the vector class is: template class vector{ //... }; When using the vector type the required header to be included is

14 14 Vector class Vectors, being objects have additional advantages over ordinary arrays: it can be assigned values quickly (overloaded assignment operator) it can be passed by value it can be returned by value

15 15 Vector class vector(); //default constructor, creates an empty vector vector(const vector* v); //copy constructor: creates a copy of the vector v; //postcondition: *this == v; vector(unsigned n, const T& x = T()); //constructor: creates a vector containing n copies of the element x; //precondition: n >= 0; //postcondition: size() == n; ~vector(); //destructor Some important methods:

16 16 Vector class vector& operator=(const vector& v); //assignment operator: assigns v to this vector, making a duplicate //postcondition: *this == v; unsigned size() const; //returns the number of elements in //this vector unsigned capacity() const; //returns the maximum number of elements that this vector can have without being reallocated void reserve(unsigned n); //reallocates this vector to a //capacity of n elements //precondition: capacity() <= n //postcondition: capacity() == n bool empty() const; // true iff size() == 0; Some important methods:

17 17 Vector class void assign(unsigned n, const T& x=T()); //clears this vector and then inserts n copies of the element x; //postcondition: n >= 0; //postcondition: size() == n; T& operator[](unsigned i); //returns element at index i //precondition: 0 <= i <= size(); //result is unpredictable if precondition is false; T& at(unsigned i); // returns element at index i //precondition: 0 <= i <= size(); //exception is thrown if precondition is false; Some important methods:

18 18 Vector class T& front(); //returns the first element of this vector T& back(); //returns the last element of this vector iterator begin(); //returns an iterator pointing to the first element of this vector iterator end(); //returns an iterator pointing to the dummy element that follows the last element of this vector void push_back(const T& x); // appends a copy of the element x to the back of this vector; //precondition: back() == x; //postcondition: size() has been incremented; Some important methods:

19 19 Vector class void push_back(const T& x); // appends a copy of the element x to the back of this vector; //precondition: back() == x; //postcondition: size() has been incremented; void pop_back(); // removes the last element of this vector; //precondition: size() > 0; //postcondition: size() has been decremented; Some important methods:

20 20 Vector class iterator insert(iterator p, const T& x); // inserts a copy of the element x at position p; returns p; //precondition: begin() <= p <= end(); //postcondition: size() has been incremented; Some important methods:

21 21 Vector class iterator erase(iterator p); // removes the element at position p; returns p; //precondition: begin() <= p <= end(); //postcondition: size() has been decremented; iterator erase(iterator p1, iterator p2); // removes the elements from position p1 to the position before p2; //returns p1 //precondition: begin() <= p1 <= p2 <= end(); //postcondition: size() has been decremented by int(p2-p1); void clear(); //removes all elements //postcondition: size() == 0; Some important methods:

22 22 Vector class Declaring/ defining a vector: vector iv; vector cv(5); vector cv(5, 'x'); vector iv2(iv);

23 23 Vector class Using a vector: // display original size of v v.size() cout << "Size = “ << v.size() <<endl; /* put values onto end of a vector; the vector will grow as needed */ for(i=0; i<10; ++i) v.push_back(i); // change contents of a vector for(i=0; i < v.size(); ++i) v[i] = v[i] + v[i];

24 24 Vector class // access using subscripting for(i=0; i<10; ++i) { v[i] cout << v[i] <<" "; } cout << endl; // access via iterator vector ::iterator p = v.begin(); while(p != v.end()) { *p cout << *p << " "; ++p; } Declaring an iterator: container_name :: iterator iterator_name

25 25 Iterator An Iterator is just a convenient pseudo-pointer that is set up as a type to use associated with each container class. Various operators will have been set up to use with them e.g. =, ==, != and += Standard idiomatic form: int array[10]; for(int i=0; i<10; i++){ } Becomes: vector v(10); vector ::iterator it; for(it=v.begin();it != v.end();it++){ }

26 26 Iterators Iterators objects Iterators are objects designed to give us the ability to cycle through the content of a container. They act like pointers, locating one item in the container at a time. All iterators have the same basic functionality, regardless of the type of container to which they are attached. The fundamental operations are: initialise the iterator at some initial position in the container return the data value stored at the current position change the data value stored at the current position determine whether there actually is an item at the iterator’s current position advance to the next position in the container

27 Iterators can be adapted to provide backward traversal. ForwIter template printForwIterForwIter void print(ForwIter first, ForwIter last, const char* title){ cout << title << endl; while ( first != last) cout << *first++ << '\t'; cout << endl; } Iterators

28 28 int main(){ int data[3] = { 9, 10, 11}; vector d(data, data + 3); //auxiliary constructor vector ::reverse_iterator p = d.rbegin(); print(p, d.rend(), "Reverse"); //... } Example that uses a reverse iterator to traverse a sequence. Iterators See vector_2009.cpp See vector_countries.cpp

29 29 Let’s have a look at the Matrix class template implemented by connecting to the STL vector via composition. Samples 2-D array using pure vector *> See vector_of_vectors.cpp See Matrix_stl.cpp

30 30 STL The standard template library (STL) is the C++ library that provides generic programming for many standard data structures and algorithms. Containers associative Containers come in two major families: sequence (are ordered) and associative (have keys for looking up elements). Iterators can be thought of as an enhanced pointer type. The algorithms use iterators to access containers.Summary

31 31 Other standard components STL relies upon several other standard components for support: allocators: to manage memory allocation for a container predicates: special functions returning true/false results comparison functions, etc.

32 32 C++ Standard C++Standard: http://www.cygnus.com/misc/wp/ International standard for the C++ programming language approved! Morristown, New Jersey, USA, Friday, November 14, 1997 FOR IMMEDIATE RELEASE This week, technical experts representing eight countries and about 40 companies involved with software technologies met in Morristown, New Jersey and completed the content of an international standard for the C++ programming language.

33 33 Scope of the Standard: The C++ standard covers both the C++ language itself and its standard library. The standard library provides standard input/output, strings, containers (such as vectors, lists, and strings), non-numerical algorithms (such as sort, search, and merge), and support for numeric computation.

34 34 The ISO/ANSI Standard for C++ was unanimously approved by the C++ Standardization Committee on June 23, 1998. From: http://www.awl.com/cseng/meyerscd/cstandard.htm The current status (June’99) is that C++ has an International Standard and the committee are in the process of handling Defect Reports. We shall continue to do this until 2003 when ISO rules require us to revise the Standard. From: http://www.inquiry.com/techtips/cpp_pro/

35 35 Iterators There are five iterator types: input output forward bidirectional random access

36 36 Container classes and algorithms dictate the category of iterator available or needed. vector containers allow random-access iterators lists do not; sort requires random-access iterators find requires an input iterator. Iterators


Download ppt "Array Size Macro approach 19 Macros Macros do not perform error checking. // macro approach #define macro_array_size(array) (sizeof(array)/sizeof(array[0]))"

Similar presentations


Ads by Google