0% found this document useful (0 votes)
59 views8 pages

Vectors : Pearson Publishing

The document discusses creating and manipulating vectors in C++. It covers creating vectors of integers and objects, accessing vector elements like arrays, and passing vectors to functions by reference so changes are reflected in the original vector.

Uploaded by

Octo1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views8 pages

Vectors : Pearson Publishing

The document discusses creating and manipulating vectors in C++. It covers creating vectors of integers and objects, accessing vector elements like arrays, and passing vectors to functions by reference so changes are reflected in the original vector.

Uploaded by

Octo1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter 8:

Vectors*

Copyright, 2013, Bill Komanetsky and


Pearson Publishing
Creating a Vector of int’s

• Create the int variable


int myInteger;
• Create the vector
vector<int> myintVector;
• Assign the int variable a value
myInteger = 99;

• Push the int variable onto the end of the vector


myintVector.push_back(myInteger);

Copyright, 2013, Bill Komanetsky and


Pearson Publishing
Creating a Vector of int’s

• After a number of these, you have an vector/list of


integers you access like an array.
Position 0
411
Position 1
265
Position 2
110
Position 3
965
Position 4
999
• Access the 3rd integer in the vector
cout << myintVector[2];
• Remember, like arrays, vectors start at 0
Copyright, 2013, Bill Komanetsky and
Pearson Publishing
Creating a Vector of students

• Create an object instance of the ‘Student’ class


student myStudent;
• Create the vector
vector<student> mystudentVector;
• Assign values to the student variable (assuming the
variables are public)
myStudent.lastName = “Smith”;
myStudent.firstName = “Jim”;
myStudent.age = 19;
• Push the student variable onto the end of the vector
mystudentVector.push_back(myStudent);

Copyright, 2013, Bill Komanetsky and


Pearson Publishing
Creating a Vector of students
• After a number of these, you have an vector/list of
Students you access like an array.
Position 0 Smith Jim 19 3.5 CS
Position 1 Collins Mary 18 4.0 BIO
Position 2 Campb Kristie 19 3.8 CS
Position 3 ell
Fillinga Sue 21 4.0 VIT
Position 4 me
Christie John 51 2.6 CS
• Access the last name of the 3rd student in the
vector
cout << mystudentVector[2].lastName;
• Remember, like arrays, vectors start at 0

Copyright, 2013, Bill Komanetsky and


Pearson Publishing
Passing Vectors to a Function
• When passing an array, the array’s pointer is sent
to the function automatically
• ex: void sortArray(int v[ ]) { }
• If you change the value of the array contents, the
data in the REAL array is changed.

• When passing a vector, a copy of the vector is


sent to the function, not the REAL vector.
• You must send a reference or pointer to the
function. In C++, a reference is preferred.
• ex: void sortVector(myStudentVector &v) { }

Copyright, 2013, Bill Komanetsky and


Pearson Publishing
More Detail
• Function Prototype
void sortVector(vector<student> &);

• Function Definition
void sortVector(vector<student> &vectorToSort) {
//program statements
}

• Calling/Invoking the Function


sortVector(studentVector);

Copyright, 2013, Bill Komanetsky and


Pearson Publishing
Using the Vector Data in a Function
• In the bubble or selection sort functions, sorting by last
name (for example)
for (int i=0; i<(vectorToSort.size()-1); i++) {
if (vectorToSort[i].lastName >
vectorToSort[i+1].lastName) {
tempStudent = vectorToSort[i];
vectorToSort[i] = vectorToSort[i+1];
vectorToSort[i+1] = tempStudent;
}//if
else { }
}//for
• Even though you passed a reference of the original vector,
you access its contents like it’s the real thing in the function.
Copyright, 2013, Bill Komanetsky and
Pearson Publishing

You might also like