8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 8 Strings and Vectors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Chapter 8 Strings and Vectors. Slide 8- 2 Overview 8.1 An Array Type for Strings 8.2 The Standard string Class 8.3 Vectors.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
C++ Standard Template Library
“Generic Programming” ECE 297
Constructors and Destructors
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 16 Exception Handling
Pointers and Dynamic Arrays
Chapter 8 Strings and Vectors
Pointers and Linked Lists
ECE 264 Object-Oriented Software Development
Motivation and Overview
CMPE Data Structures and Algorithms in C++ September 7 Class Meeting
Announcements HW6 due this Wednesday
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 4 Linked Lists.
STL Common tools for C++.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
CS Computer Science IB: Object Oriented Programming
group work #hifiTeam
Collections Intro What is the STL? Templates, collections, & iterators
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
ADT Implementations: Templates and Standard Containers
Today’s Learning Objective
What’s Left in the Course
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Name: Rubaisha Rajpoot
CMSC 202 ArrayList Aug 9, 2007.
C-Strings, Strings, Vectors, STL
Constructors and Other Tools
Announcements HW6 due this Wednesday
Constructors and Destructors
Vectors.
The Standard Template Library
CS 144 Advanced C++ Programming February 7 Class Meeting
COP 3330 Object-oriented Programming in C++
CMSC 202 Lesson 6 Functions II.
the Standard Template Library
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Collections Intro What is the STL? Templates, collections, & iterators
CMSC 341 C++ and OOP.
Chapter 3 Lists, Stacks, and Queues
An Introduction to Programming though C++
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1

Vectors Vectors are like arrays that can change size as your program runs Vectors, like arrays, have a base type To declare an empty vector with base type int: vector<int> v; <int> identifies vector as a template class You can use any base type in a template class: vector<string> v; Slide 8- 2 2

Accessing vector Elements Vectors elements are indexed starting with 0 [ ]'s are used to read or change the value of an item: v[i] = 42; cout << v[i]; [ ]'s cannot be used to initialize a vector element Slide 8- 3 3

Initializing vector Elements Elements are added to a vector using the member function push_back push_back adds an element in the next available position Example: vector<double> sample; sample.push_back(0.0); sample.push_back(1.1); sample.push_back(2.2); Slide 8- 4 4

The size Of A vector The member function size returns the number of elements in a vector Example: To print each element of a vector given the previous vector initialization: for (int i= 0; i < sample.size( ); i++) cout << sample[i] << endl; Slide 8- 5 5

The Type unsigned int The vector class member function size returns an unsigned int Unsigned int's are nonnegative integers Some compilers will give a warning if the previous for-loop is not changed to: for (unsigned int i= 0; i < sample.size( ); i++) cout << sample[i] << endl; Slide 8- 6 6

Alternate vector Initialization A vector constructor exists that takes an integer argument and initializes that number of elements Example: vector<int> v(10); initializes the first 10 elements to 0 v.size( ) would return 10 [ ]'s can now be used to assign elements 0 through 9 push_back is used to assign elements greater than 9 Slide 8- 7 7

Vector Initialization With Classes The vector constructor with an integer argument Initializes elements of number types to zero Initializes elements of class types using the default constructor for the class http://ideone.com/7FDQ4D Slide 8- 8 8

The vector Library To use the vector class Include the vector library #include <vector> Vector names are placed in the standard namespace so the usual using directive is needed: using namespace std; Slide 8- 9 9

vector Issues Attempting to use [ ] to set a value beyond the size of a vector may not generate an error The program will probably misbehave The assignment operator with vectors does an element by element copy of the right hand vector For class types, the assignment operator must make independent copies Slide 8- 10 10

vector Efficiency A vector's capacity is the number of elements allocated in memory Accessible using the capacity( ) member function Size is the number of elements initialized When a vector runs out of space, the capacity is automatically increased A common scheme is to double the size of a vector More efficient than allocating smaller chunks of memory Slide 8- 11 11

Controlling vector Capacity When efficiency is an issue Member function reserve can increase the capacity of a vector Example: v.reserve(32); // at least 32 elements v.reserve(v.size( ) + 10); // at least 10 more resize can be used to shrink a vector Example: v.resize(24); //elements beyond 24 are lost Slide 8- 12 12

Section 8.3 Conclusion Can you Declare and initialize a vector of 10 doubles? Write code to increase the size of a vector in at least two different ways? Describe the difference between a vector's size and its capacity? Slide 8- 13 13