Lecture5.pdf - AMS 595 Programming Languages and C 5 Standard Template Library C Programming From Problem Analysis to Program Design Third Edition 1

Lecture5.pdf - AMS 595 Programming Languages and C 5...

This preview shows page 1 - 12 out of 53 pages.

AMS 595: Programming Languages and C++ 5: Standard Template Library C++ Programming: From Problem Analysis to Program Design, Third Edition 1
Image of page 1
Outline 1. STL 2. Vectors 3. Examples 2
Image of page 2
Problem solving often requires information be viewed as a list n List may be one-dimensional or multidimensional C++ provides two list mechanisms n Arrays w Traditional and important because of legacy libraries w Restrictions on its use n Container classes w First-class list representation w Common containers provided by STL n Vector, queue, stack, map, … w Preferred long-term programming practice Lists Vectors are a first-class mechanism for representing lists
Image of page 3
4 Standard Template Library (STL) Defines powerful, reusable components That implement common data structures and algorithms Containers: data structures that store objects of any type Iterators: used to manipulate container elements Algorithms: searching, sorting and many others Sequence containers: - linear data structures such as vectors and linked lists Associative containers: - non-linear containers such as hash tables Container adapters: - constrained sequence containers such as stacks and queues
Image of page 4
5 Sequence Containers STL provides three sequence containers - vector: based on arrays - deque (double-ended queue): based on arrays - list: based on linked lists
Image of page 5
6 Sequence Containers: vector List representation that is mostly comparable in performance to arrays The implementation of a vector is based on arrays Direct access to any element via indexes n Indices are in the range 0 … size of list – 1 List size is dynamic n Can add items as we need them (and remove them too) n Insertion at the end is normally efficient. n The vector simply grows Insertion and deletion in the middle is expensive An entire portion of the vector needs to be moved
Image of page 6
Example #include <vector> #include <iostream> using namespace std; int main() { vector<int> A(4, 0); // A: 0 0 0 0 A.resize(8, 2); // A: 0 0 0 0 2 2 2 2 vector<int> B(3, 1); // B: 1 1 1 for (int i = 0; i < B.size(); ++i) { A[i] = B[i] + 2; } // A: 3 3 3 0 2 2 2 2 A = B; // A: 1 1 1 return 0; }
Image of page 7
Vector Construction vector() The default constructor creates a vector of zero length vector(size_type n, const T &val = T()) Explicit constructor creates a vector of length n with each element initialized to val vector(const T &V) The copy constructor creates a vector that is a duplicate of vector V . Shallow copy!
Image of page 8
Construction Basic construction vector<T> List; Example vector<int> A; // 0 ints vector<float> B; // 0 floats vector<Rational> C; // 0 Rationals Base element type Container name
Image of page 9
Construction Basic construction vector<T> List(SizeExpression); Example vector<int> A(10); // 10 ints vector<float> B(20); // 20 floats vector<Rational> C(5); // 5 Rationals int n = PromptAndRead(); vector<int> D(n); // n ints Base element type Container name Number of elements to be default constructed
Image of page 10
Construction Basic construction vector<T> List(SizeExpression, Value); Example vector<int> A(10, 3); // 10 3s vector<float> B(20, 0.2); // 20 0.2s Rational r(2/3); vector<Rational> C(5, r); // 5 2/3s Base element type Container name Number of elements to be default constructed Initial value
Image of page 11
Image of page 12

You've reached the end of your free preview.

Want to read all 53 pages?

  • Fall '08
  • Staff
  • Val, Standard Template Library, Vector Construction

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture