- 115 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Vectors' - bernard-oneill

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Vectors

- Vectors allow you to make resizeable lists of data.
- With vectors, you can also write functions that will work on data sets of any size
- To work with the vector class, include the standard namespace header file <vector>

Vectors

- To declare a vector, use one of the following :
vector <datatype> identifier; //empty, uninitializedvector <datatype> identifier(int size); //size given, uninitialized vector <datatype> identifier(int size, datatype initialvalue); //size + initialized

- Examples:
//empty, uninitialized vector of doubles called myScores.

vector <double> myScores;

//make an uninitialized vector of 3 strings called myNames.

vector <string> myNames(3);

//make a vector of 10 ints, initialized to 5.

vector <int> nums(10,5);

Vectors

- Vectors can be manipulated much like arrays once they are created. The first element has index 0, and you can use subscript notation to manipulate specific values in the vector.
- Examples:
//assign 1st element of string vector myNames

myNames[0] = “Mandi”;

//loop through nums vector counting 5’s

for(int x = 0; x < nums.size(); x++)if(nums[x]== 5)

fivecount++;

Vectors

- To define vectors as parameters of functions, use & if you want the function to change the vector, otherwise use const or just leave out the &.
- Exampleheaders:
//a display function

void display(const vector <int> nums)

//fill functionvoid display(vector <int> &nums)

//template display functiontemplate <typename bob>void display(const vector <bob> nums)

Vector Functions

- size() – tells you how many elements there are… the loop below counts how many times the char ‘a’ occurs in a vector of chars called word. Regardless of how many elements are in the vector, this loop will work because it uses the size() method to figure out how many elements are in word.for(int x=0; x < word.size(); x++)if(word[x]==‘a’)
count++;

Vector Functions

- resize() – allows you to change the number of elements there are… the example below resizes a vector called nums to have 12 elements, then a vector called names is resized to be one element smaller than it was.
nums.resize(12);

names.resize(names.size()-1); //be careful!

Write a program that reads a text file that has a student’s full name (on its own line) and several test scores after it. Make sure there is NO WHITE SPACE after the last number. The number of scores is unknown, and may change since the student’s teacher will open the file and type in new grades whenever they come up. Your program will always open the file, read the student name and then store the grades in a vector. The program will then compute the test average - throw out the lowest score (only if there are at least two scores). A sample text file with output is below. You may want to include more output (like the dropped score, size of the vector, etc… to help you error trap as you are coding.

Tom Cruise

99.6 77 77 88

33.5 44.9 12 44 33

Download Presentation

Connecting to Server..