Lecture150 vectors

# Lecture150 vectors - Making our programs more flexible So...

This preview shows pages 1–5. Sign up to view the full content.

Lecture 1 -- 1 Computer Science I - Martin Hardwick Making our programs more flexible So far we have largely programmed using Arrays of integers Arrays of strings Arrays of doubles There is an issue with this style of programming Suppose we want an array of student records, or hospital  records Suppose we do not know how big our array needs to be The first issue is overcome using structs and objects We started looking at struct’s in the last lecture We will look at Objects soon because they are very important  and are like struct’s that have functions as well as data The second issue is overcome using the new STL Vector class It comes loaded with useful methods It can grow and shrink while a program executes

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 1 -- 2 Computer Science I - Martin Hardwick Vector Syntax: Example: Think of a vector as a way to create arrays that can grow bigger. you can use a vector for any type of data you can use it anywhere an array is required To access the components of a vector use the [] operator. To add an item to the end use push_back() To remove an item from the end use pop_back () To change an item use [] To test an item use == vector <type> <name>; vector <acct> bank; vector <int> numbers; numbers[10] = 99999; numbers.push_back (20); bank[i].put_name(“Hardwick”); if (bank[j].get_num () == 10000)
Lecture 1 -- 3 Computer Science I - Martin Hardwick Study the following page http://www.cprogramming.com/tutorial/stl/vector.html Understand how to Create a vector Add items to a vector Get items from a vector Why vectors are good Vectors use STL STL is the structured template language An extension to C++ that gives great flexibility to the way  classes and other structures are defined. You can define STL classes for trees, lists, vectors and many  other structures – More in CS 2 and DSA

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 1 -- 4 Computer Science I - Martin Hardwick Why Vectors are good Have methods already programmed to let you add items, find items. Size to find the size – makes parameter passing much easier
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

Lecture150 vectors - Making our programs more flexible So...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online