220 likes | 478 Views
The vector class. CS342 Data Structures Based on Ford & Topp. The vector container characteristics. Similar to a one-dimensional array: use index for direct access. Can dynamically grow and contract. Many useful member functions. A "near" STL class. A simple example.
E N D
The vector class CS342 Data Structures Based on Ford & Topp
The vector container characteristics • Similar to a one-dimensional array: use index for direct access. • Can dynamically grow and contract. • Many useful member functions. • A "near" STL class
A simple example // Vector function writeVetor( ) template <typename T> void writeVctor(const vector<T> &v ) { int i, n = v.size( ); // size( ) is a member function for (i = 0; i < n; i++) cout << v[i] << '\t'; // identical to an array cout << endl; }
Declaring vector objects examples // without initialization vector<int> v(5); // vector of size 5 containing 0's vector<string> s(10); // size 10, each element contain null string // with initialization vector<char> line(80, 'A'); // 80 characters, each initialized to 'A' vector<time> t(5, time(12,30, 0)); // 5 elements with initial value 12:30:00 // initialization from an array int a[5] = {1, 2, 3, 4, 5}; // array with initial values vector<int> v(a, a + 5); // initial values copied from the array a // notice a + 5is the address just past the end // of array a // initialization without specifying the size, the initial size will be defaulted to 0 vector<double> dblv; vector<string> s2;
Vector member function push_back( ) and pop_back( ) v.push_back(55); v.pop_back( );
The sizeof( ) operator and back( ) member function char vowel[ ] = {'a'. 'e', 'i', 'o', 'u'}; int vowelSize = sizeof(vowel)/sizeof(char); vector<char> v(vowel, vowel + vowelSize); cout << v.back( ); // output 'u' v.push_back('w'); // add 'w' to end of v v.back( ) = 'y'; // change back from 'w' to 'y'
Example: start with an empty vector and build a list with input (values of double type) from the user vector<double> list; // declares an empty list double data; cin >> data; while(data != -99) { list.push_back(data); }
Example: the pairing of back( ) and pop_back( ) int a[ ] = {1, 2, 3, 4, 5, 6}; int aSize = sizeof(a) / sizeof(int); vector<int> v(a, a + aSize); while(!v.empty( )) { cout << v.back( ) << '\t'; v.pop.back( ); } // output: 6 5 4 3 2 1
Resizing a vector with resize( ) member function int a[ ] = {1, 2, 3, 4, 5}; int aSize = sizeof(a) / sizeof(int); vector<int> v(a, a + aSize); // size = 5 v.resize(2 * aSize); // size = 10, "fill" value is 0 v.resize(4); // size = 4, data is lost v.resize(10, 1); // size = 10. fill value is 1
How is the vector class defined: the vector class abstraction • The constructors vector( ); // default constructor creates an empty vector. vector(int n, const T& value = T( )); /* creates a vector with n elements, each have the specified value. If value is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, a nd the default value of type T is specified by the notation T( ). */ vector(T *first, T *last); /* initialize the vector using the address range [first, last).
The abstraction of the vector class continued • The member functions or operations T &back( ); // returns the value of the item at the rear of the vector // Pre-condition: the vector must contain at least one element. const T &back( ); // const version of back( ); bool empty( ) const; // returns true if vector is empty, false otherwise T &operator[ ](int i); // allows element at index i to be retrieved or modified. const T &operator[ ](int i); // const version of T &operator[ ](int i); void push_back(const T &value); /* Post-condition: a new element is added to the rear and the size of the vector is increased by 1. */
The abstraction of the vector class continued • The member functions or operations (continued) void pop_back( ); /* removes the item at the rear; Precondition: the vector is not empty Post condition: the size of the vector is decreased by 1. */ void resize(int n, const T & fill = T( )); /* modifies the size of the vector; if the size is increased, new elements with value fill are added to the rear of the vector. If the value is decreased, the original values at the front are retained. Post-condition: the vector has size n. */ int size( ) const; /* returns the number of elements in the vector. */
A simple application: join two vectors together template <typename T> void join(vector<T> &v1, const vector <T> &v2) { int i, sizev2 = v2.size( ); for (i = 0; i < sizev2; i++) v1.push_back(v2[i]); }
The matrix class • Because of its importance in science and engineering applications, matrix class has been included in the C++ STL. • Matrix is implemented a vector of vectors: vector<vector<T> > mat; /* the outer vector corresponds to rows of the matrix, the elements in the inner vector of type T correspond to the column entries for each vector row. For example, mat[0] is the vector of column entries corresponding to row 0, etc. Note that there must be a space between the closing angle brackets > > or is will be interpreted as >> which is the extraction operator or right shift operator in bit manipulation. */