Professional Documents
Culture Documents
http://eee241.gantep.edu.tr/
Lecture 8
Vectors
Sayfa 1
Sayfa 2
Concept of Vectors
Up to now, a variable has represented a single value,
we call this a scalar variable; it has a type and a name.
A vector variable can represent a group of values, all with the
same name and data type.
To declare and use vectors we must include the vector header:
#include <vector>
The vector data class provides many powerful methods for
vector processing and dynamic memory management.
We will study the basics of vectors in this lecture.
Sayfa 3
Vector Declaration
The general form of the declaration of a vector array is:
vector<type> name(numberOfElements);
For example, consider a variable named mass :
Scalar declaration:
double mass;
This variable can
represent only one
value at a time.
Vector declaration:
vector<double>
mass(4);
Sayfa 4
Empty vectors
vector<type> name(numberOfElements);
optional
If you do not specify numberOfElements then
the vector will be created with no (zero) elements
vector<int> scores;
This is an empty vector!
Sayfa 5
name[1]
name[8]
name[15]
name[22]
name[29]
name[36]
name[43]
name[50]
name[57]
name[64]
name[71]
name[78]
name[85]
name[92]
name[99]
name[106]
name[113]
name[2]
name[9]
name[16]
name[23]
name[30]
name[37]
name[44]
name[51]
name[58]
name[65]
name[72]
name[79]
name[86]
name[93]
name[100]
name[107]
name[114]
name[3]
name[10]
name[17]
name[24]
name[31]
name[38]
name[45]
name[52]
name[59]
name[66]
name[73]
name[80]
name[87]
name[94]
name[101]
name[108]
name[115]
name[4]
name[11]
name[18]
name[25]
name[32]
name[39]
name[46]
name[53]
name[60]
name[67]
name[74]
name[81]
name[88]
name[95]
name[102]
name[109]
name[116]
name[5]
name[12]
name[19]
name[26]
name[33]
name[40]
name[47]
name[54]
name[61]
name[68]
name[75]
name[82]
name[89]
name[96]
name[103]
name[110]
name[117]
name[6]
name[13]
name[20]
name[27]
name[34]
name[41]
name[48]
name[55]
name[62]
name[69]
name[76]
name[83]
name[90]
name[97]
name[104]
name[111]
Clearly it is easier to declare one vector (size 118) than it is to declare 118 scalars!
Sayfa 6
Vector Initialisation
The general form of vector declaration:
vector<type> name(numberOfElements);
initialises all elements of the vector to zero.
Alternatively an initialiser can be given at declaration:
vector<type> name(numberOfElements, value);
initialises all elements of the vector to value.
Examples
vector<double>
mass(6);
mass(6,1.8);
Vector Assignment
Consider the vector declaration: vector<double>
a(5);
0.0
0.0
0.0
0.0
0.0
=
=
=
=
8.4;
3.6;
9.1;
3.9;
The values of the elements are now re-defined (except for a[3]):
8.4
3.6
9.1
0.0
3.9
cout << "Input " << n << " real numbers:" << endl;
for(int i=0; i<n; i++) cin >> a[i];
cout << "In reverse order: " << endl;
for(int i=n-1; i>=0; i--) cout << a[i] << " ";
cout << endl;
return 0;
}
Output
Input n: 5
Input 5 real numbers:
1.2 3.5 -0.4 10.2 7.1
In reverse order:
7.1 10.2 -0.4 3.5 1.2
Sayfa 9
Processing Vectors
Vector processing often involves looping over elements.
#include <iostream>
#include <vector>
using namespace std;
Sum of the
values of the
elements of
a vector:
int main () {
int n = 5;
vector<double> a(n);
a[0]=1.7; a[1]=4.1; a[2]=5.6; a[3]=3.4; a[4]=3.1;
double s=0.0;
for (int i=0; i<n; i++)
s = s + a[i];
cout << "The sum is " << s << endl;
}
Output
Sum of the
values of the
elements of
a vector:
int main () {
int n = 5;
vector<double> a(n);
a[0]=1.7; a[1]=4.1; a[2]=5.6; a[3]=3.4; a[4]=3.1;
double s=0.0;
for (int i=0; i<a.size(); i++)
s = s + a[i];
cout << "The sum is " << s << endl;
}
Output
Sum of the
values of the
elements of
a vector:
double sum(vector<double> x) {
double s=0.0;
for (int i=0; i<x.size(); i++)
s = s + x[i];
return s;
}
Passing a vector to a
function works just
the same as passing
a scalar.
Output
Passing by reference
A function to square each element of a vector.
#include <iostream>
#include <vector>
using namespace std;
Vector x is passed by
reference so that the
modification to each
element is preserved.
int main () {
vector<double> a(3);
a[0]=1.7; a[1]=4.1; a[2]=5.6;
cout << a[0] << "\t" << a[1] << "\t" << a[2] << endl;
vsquare(a);
cout << a[0] << "\t" << a[1] << "\t" << a[2] << endl;
}
Output
1.7
2.89
4.1
16.81
5.6
31.36
Sayfa 13
name.pop_back();
name.clear();
name.resize(s);
s
Sayfa 14
add a value
to the stack
6
Sayfa 15
The
The
The
The
size is
content
size is
content
3
is: 8.3 8.3 8.3
4
is: 8.3 8.3 8.3 5.9
The
The
The
The
size is
content
size is
content
3
is: 8.3 8.3 8.3
2
is: 8.3 8.3
The
The
The
The
size is
content
size is
content
3
is: 8.3 8.3 8.3
0
empty vector
is:
The
The
The
The
size is
content
size is
content
3
is: 8.3 8.3 8.3
5
is: 8.3 8.3 8.3 0.0 0.0
This program builds a vector from values input from the keyboard.
The size of the vector (initially zero) increases until a zero is input.
#include <iostream>
#include <vector>
using namespace std;
Output
int main() {
vector<int> iv;
56
int n;
while(true) {
cout << "Input: ";
cin >> n;
if (n==0) break;
iv.push_back(n);
}
23
89
65
34
Input: 34
Input: 65
Input: 89
Input: 23
Input: 56
Input: 0
iv is:
iv[0] = 34
iv[1] = 65
iv[2] = 89
iv[3] = 23
iv[4] = 56
10
Solved problems
Create a program that inputs the atomic number Z, and
outputs the corresponding elements Name, Mass and Classification.
Z
1
2
3
4
5
6
7
8
9
10
11
12
Symbol
H
He
Li
Be
B
C
N
O
F
Ne
Na
Mg
Name
Hydrogen
Helium
Lithium
Beryllium
Boron
Carbon
Nitrogen
Oxygen
Fluorine
Neon
Sodium
Magnesium
Mass (amu)
1.00794
4.002602
6.941
9.012182
10.811
12.0107
14.00674
15.9994
18.998404
20.1797
22.9898
24.305
Classification
Non-metal
Noble Gas
Alkali Metal
Alkaline Earth
Metaloid
Non-Metal
Non-Metal
Non-Metal
Halogen
Noble Gas
Alkali Metal
Alkaline Earth
Sayfa 21
The solution (except for the part where we assign data to the vectors) .
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> name(12), symbol(12), clas(12);
vector<double> mass(12);
Assign all elements of name, symbol, clas and mass from the table.
int Z; cout << "Input atomic number Z: "; cin >> Z;
cout
cout
cout
cout
cout
cout
<<
<<
<<
<<
<<
<<
"
"
"
"
"
"
+-------------------"
| Z = " << Z
| " << name[Z-1] << " ("
| Mass = " << mass[Z-1]
| Class: " << clas[Z-1]
+-------------------"
<<
<<
<<
<<
<<
<<
endl;
endl;
symbol[Z-1] << ")" << endl;
" amu." << endl;
endl;
endl;
Sayfa 22
11
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> name(12), symbol(12), clas(12);
vector<double> mass(12);
name[ 0]="Hydrogen";
name[ 1]="Helium";
name[ 2]="Lithium";
name[ 3]="Beryllium";
name[ 4]="Boron";
name[ 5]="Carbon";
name[ 6]="Nitrogen";
name[ 7]="Oxygen";
name[ 8]="Fluorine";
name[ 9]="Neon";
name[10]="Sodium";
name[11]="Magnesium";
symbol[ 0]="H";
symbol[ 1]="He";
symbol[ 2]="Li";
symbol[ 3]="Be";
symbol[ 4]="B";
symbol[ 5]="C";
symbol[ 6]="N";
symbol[ 7]="O";
symbol[ 8]="F";
symbol[ 9]="Ne";
symbol[10]="Na";
symbol[11]="Mg";
clas[ 0]="Non-Metal";
clas[ 1]="Noble Gas";
clas[ 2]="Alkali Metal";
clas[ 3]="Alkaline Earth";
clas[ 4]="Metaloid";
clas[ 5]="Non-Metal";
clas[ 6]="Non-Metal";
clas[ 7]="Non-Metal";
clas[ 8]="Halogen";
clas[ 9]="Noble Gas";
clas[10]="Alkali Metal";
clas[11]="Alkaline Earth";
mass[ 0]=1.00794;
mass[ 1]=4.002602;
mass[ 2]=6.941;
mass[ 3]=9.012182;
mass[ 4]=10.811;
mass[ 5]=12.0107;
mass[ 6]=14.00674;
mass[ 7]=15.9994;
mass[ 8]=18.998404;
mass[ 9]=20.1797;
mass[10]=22.9898;
mass[11]=24.305;
<<
<<
<<
<<
<<
<<
"
"
"
"
"
"
+-------------------"
| Z = " << Z
| " << name[Z-1] << " ("
| Mass = " << mass[Z-1]
| Class: " << clas[Z-1]
+-------------------"
<<
<<
<<
<<
<<
<<
endl;
endl;
symbol[Z-1] << ")" << endl;
" amu." << endl;
endl;
endl;
In the next
lecture we will
assign this data
by reading it
from a file
Sayfa 23
Finally
In this lecture we have looked at vectors.
You can find the lecture slides in the course website:
http://eee241.gantep.edu.tr/
For further reading, search for the relevant sections at:
http://cpp.gantep.edu.tr/
and
http://www.learncpp.com/
Consider this as your course text book.
Sayfa 24
12
Sayfa 25
13