Вы находитесь на странице: 1из 21

EE-163

Computers and Programming


FE Electrical
Lecture 33 to 40
Vectors
Course Instructors: Fezan Rafique / Muhammad Hassan ul Haq
Lecturers, Dept of Electrical Engg, NEDUET
Vector (Definition)
Vector is an abstracted data type made upon an array. It is a class, just
like string. But unlike strings, vector class needs a header file <vector>
to be included in the program before it is used.

e.g. #include<vector>

What is a class?
A class is a collection of data and functions wrapped into a single unit
called object. Class is a general category just like a data-type(int, float,
char etc). When you declare an instance of a class, its called an object.

Class contains data members and member functions.

General Structure of a Class

A Generic Class

Data member Member function

Any data type can Functions written to


come here, even access, modify or just
multiple times print the data stored
in one or more data
1
members
EE-163 Lectures 33 to 40
1) Introduction to Vectors
Vector Class General Description
Data member Member function
An array of given data Array.begin(),
type: int array[] Array.push_back(),
Array.pop_back() etc
1 //Intro to vectors: Declaration, initialization and accessing values
2 #include<iostream>
3 #include<conio2.h>
4 #include<vector> //New header, to include vector class
5
6 using namespace std;
7
8 int main (void)
9 {
10 //Declaring a vector object of type int and name integer1
11 //vector is similar to array and contains memory locations
12 //starting from 0 and going to the last element (SIZE-1)
13 vector<int> integer1(10);
14
15 //method of inserting values: same as arrays
16 integer1[0]=40; //40 written to location 3
17 integer1[1]=50; //50 written to location 4
18 integer1[2]=60; //60 written to location 5
19
20 //Printing these locations
21 cout<<endl<<"integer1[0]="<<integer1[0];
22 cout<<endl<<"integer1[1]="<<integer1[1];
23 cout<<endl<<"integer1[2]="<<integer1[2];
24 //Printing additional locations
25 cout<<endl<<"integer1[3]="<<integer1[3];
26 cout<<endl<<"integer1[4]="<<integer1[4];
27 cout<<endl<<"integer1[5]="<<integer1[5];
28
2
29 return 0;
}
EE-163 Lectures 33 to 40
2) Introduction to Vectors – Declaration Methods
1 //Vector Declaration Methods
2 #include<iostream>
3 #include<conio2.h>
4 #include<vector> //New header, to include vector class
5
6 using namespace std;
7
8 int main (void)
9 {
10 //*************************************************************
11 //Declaration method 1: Only naming the vector object
12 vector<float> temp;
13 //This vector has no memory assigned to it and results
14 //in run-time error when accessing it like temp[0]. Only
15 //push_back() function can be used to add elements
16 //*************************************************************
17 //Declaration method 2: naming the vector object and declaring
18 // its size
19 //Declaring a vector object of type char and name vowels
20 //vector is similar to array and contains memory locations
21 //starting from 0 and going to the 4 (i.e SIZE-1, where SIZE is 5)
22 vector<char> vowels(5);
23
24 //Assigning values to these locations
25 vowels[0]='a';
26 vowels[1]='e';
27 vowels[2]='i';
28 vowels[3]='o';
29 vowels[4]='u';
30
31 //Printing these locations
32 cout<<endl<<"vowels[0]="<<vowels[0];
33 cout<<endl<<"vowels[1]="<<vowels[1];
34 cout<<endl<<"vowels[2]="<<vowels[2];
3
EE-163 Lectures 33 to 40
35 cout<<endl<<"vowels[3]="<<vowels[3];
36 cout<<endl<<"vowels[4]="<<vowels[4];
37
38 //Lets make a mistake
39 cout<<endl<<"vowels[6]="<<vowels[6];
40 cout<<endl<<"vowels[169]="<<vowels[169];
41 cout<<endl<<endl;
42
43 //*************************************************************
44 //Declaration method 3: naming the vector object, declaring
45 // its size and assigning initial value
46 //Declaring a vector object of type int and name integers
47 //vector is similar to array and contains memory locations
48 //starting from 0 and going to the 5 (i.e SIZE-1, where SIZE is 6)
49 //all locations are initialized with value 10
50 vector<int> integers(6,10);
51 //Printing these locations
52 cout<<endl<<"integers[0]="<<integers[0];
53 cout<<endl<<"integers[1]="<<integers[1];
54 cout<<endl<<"integers[2]="<<integers[2];
55 cout<<endl<<"integers[3]="<<integers[3];
56 cout<<endl<<"integers[4]="<<integers[4];
57 cout<<endl<<"integers[5]="<<integers[5];
58
59 return 0;
60 }

Methods of declaring a Vector - Summary


1) Declaring only the vector with size 0(zero)
vector<char> name;
[Note: this vector can only be accessed by member functions]
2) Declaring vector with some size and no initial value
vector<int> coursegrades(6);
3) Declaring vector with size and initial values
vector<double> temp(7,0.0);
4
EE-163 Lectures 33 to 40
3) Adding data to vectors
1 //Adding data to vectors
2 #include<iostream>
3 #include<vector> //New header, to include vector class
4
5 using namespace std;
6
7 int main (void)
8 {
9 vector<int> integer;
10 vector<double> num(5);
11
12 //size() function from vector class
13 //gives current size of any vector
14 cout<<"size of integer vector:"<<integer.size();
15 //***********************************************************
16 //Adding data to vectors, Method 1:using push_back() function
17 integer.push_back(12); //push to location 0
18 integer.push_back(10); //push to location 1
19 integer.push_back(8);
20 integer.push_back(6);
21 integer.push_back(4);
22 integer.push_back(2);
23 integer.push_back(0); //push to location 6
24
25 //push_back() expands the vector size
26 cout<<endl<<"integer[0]="<<integer[0];
27 cout<<endl<<"integer[1]="<<integer[1];
28 cout<<endl<<"integer[2]="<<integer[2];
29 cout<<endl<<"integer[3]="<<integer[3];
30 cout<<endl<<"integer[4]="<<integer[4];
31 cout<<endl<<"integer[5]="<<integer[5];
32 cout<<endl<<"integer[6]="<<integer[6];
33
34 //to find size of a vector: You don't need to save size as a constant
35 cout<<endl<<"Size of integer vector="<<integer.size(); 5
EE-163 Lectures 33 to 40
36 //***********************************************************
37 //Adding data to vectors, Method 2:using location number
38 num[0]=1.3;
39 num[1]=2.3;
40 num[2]=5.3;
41 num[3]=2.5;
42 num[4]=4.3;
43 //dispaly contents
44 cout<<endl<<"num[0]="<<num[0];
45 cout<<endl<<"num[1]="<<num[1];
46 cout<<endl<<"num[2]="<<num[2];
47 cout<<endl<<"num[3]="<<num[3];
48 cout<<endl<<"num[4]="<<num[4];
49
50 cout<<endl<<"Size of num vector="<<num.size();
51 return 0;
52 }

4) Adding data to vectors: Practical Method


1 #include<iostream>
2 #include<vector> //New header, to include vector class
3 using namespace std;
4 int main (void)
5 {
6 vector<int> integer;
7 vector<double> num(5);
8 int number=12;
9 //size() function from vector class
10 //gives current size of any vector
11 cout<<"size of integer vector:"<<integer.size();
12 //***********************************************************
13 //Adding data to vectors, Method 1:using push_back() function
14 for(int index=0;index<=6;index++)
15 {
16 integer.push_back(number);
17 number=number-2; 6
18 }
EE-163 Lectures 33 to 40
22 for(int index=0;index<integer.size();index++)
23 {
24 cout<<endl<<"integer["<<index<<"]="<<integer[index];
25 }
26 cout<<"\nNew size of integer = "<<integer.size()
27 <<endl;
28
29
30 cout<<"size of num vector:"<<num.size();
31 //***********************************************************
32 //Adding data to vectors, Method 2:using location number
33 for(int index=0;index<=4;index++)
34 {
35 cout<<endl<<"enter num "<<index<<":";
36 cin>>num[index];
37 }
38 //dispaly contents
39 for(int index=0;index<num.size();index++)
40 {
41 cout<<endl<<"num["<<index<<"]="<<num[index];
42 }
43
44 cout<<endl<<"New size of num vector="<<num.size();
45 return 0;
46 }

Adding data to vectors: Summary


* If push_back() function is used, the vector can grow beyond its initia
size.
* push_back() is ideal for a vector that is initialized without size, the
function allows the vector to grow dynamically.

* Array notation (num[0], ch[5] etc) can also be used with vectors but
with a constraint: only a vector of pre-declared size can be accessed.
7
EE-163 Lectures 33 to 40
5) Accessing data from vectors
1 #include<iostream>
2 #include<vector> //New header, to include vector class
3
4 using namespace std;
5
6 int main (void)
7 {
8 vector<int> integer(5,3);
9
10 //***********************************************************
11 //Accessing vector contents, Method 1:using location number
12 cout<<endl<<"integer[0]="<<integer[0];
13 cout<<endl<<"integer[1]="<<integer[1];
14 cout<<endl<<"integer[2]="<<integer[2];
15 cout<<endl<<"integer[3]="<<integer[3];
16 cout<<endl<<"integer[4]="<<integer[4];
17 integer.push_back(43);//push_back extends the vector size
18 cout<<endl<<endl;
19 for(int index=0;index<integer.size();index++)
20 {
21 cout<<endl<<"integer["<<index<<"]="<<integer[index];
22 }
23
24 //***********************************************************
25 //Accessing vector contents, Method 2:using at() function
26 for(int index=0;index<integer.size();index++)
27 {
28 cout<<endl<<"integer at location "<<index<<"="<<integer.at(index);
29 }
30
31 //***********************************************************
32 //Accessing vector contents, Method 3:using pop_back() function
33 //This is a destructive method of vector access, and removes the
34 //last element
35 8
EE-163 Lectures 33 to 40
36 for(int index=0;index<integer.size();index++)
37 {
38 cout<<endl<<"integer["<<index<<"]="<<integer[index];
39 }
40 cout<<endl;
41 integer.pop_back();
42 for(int index=0;index<integer.size();index++)
43 {
44 cout<<endl<<"integer["<<index<<"]="<<integer[index];
45 }
46 cout<<endl;
47 integer.pop_back();
48 for(int index=0;index<integer.size();index++)
49 {
50 cout<<endl<<"integer["<<index<<"]="<<integer[index];
51 }
52 cout<<endl;
53 integer.pop_back();
54 for(int index=0;index<integer.size();index++)
55 {
56 cout<<endl<<"integer["<<index<<"]="<<integer[index];
57 }
58/ cout<<endl;
59 integer.pop_back();
60 for(int index=0;index<integer.size();index++)
61 {
62 cout<<endl<<"integer["<<index<<"]="<<integer[index];
63 }
64 cout<<endl;
65 integer.pop_back();
66 for(int index=0;index<integer.size();index++)
67 {
68 cout<<endl<<"integer["<<index<<"]="<<integer[index];
69 }
70 return 0;
71 }
9
EE-163 Lectures 33 to 40
6) Additional example: averaging grades

1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5 int main(void)
6 {
7 vector<int> grades;
8 int total, grade;
9 //Using push_back() to accept grades
10 for(int i = 1; i <= 5; ++i)
11 {
12 cout << "Enter a grade: ";
13 cin >> grade;
14 grades.push_back(grade);
15 }
16 //Manually adding grades
17 total = grades[0] + grades[1] + grades[2]
18 + grades[3] + grades[4];
19 cout << "Totalling manually,\n ";
20 cout << "The total of the grades is: " << total
21 << endl;
22 //Automatically adding grades with a loop
23 total = 0;
24 for(int i = 0; i < grades.size(); ++i)
25 {
26 total += grades[i];
27 }
28 cout << "Totalling automatically,\n ";
29 cout << "The total of the grades is: " << total
30 << endl;
31 return 0;
32 }

10
EE-163 Lectures 33 to 40
7) Additional example: string vectors

1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5 int main()
6 {
7 vector<string> names;
8 names.push_back("Riaz");
9 names.push_back("Sana");
10 names.push_back("Ahmed");
11 names.push_back("Kamal");
12 cout << endl;
13 //printing all elements
14 for(int i = 0; i < names.size(); ++i)
15 {
16 cout << names[i] << " ";
17 }
18 cout << endl;
19 //popping the last element out
20 names.pop_back();
21 //printing remaining elements
22 for(int i = 0; i < names.size(); ++i)
23 {
24 cout << names[i] << " ";
25 }
26 cout << endl;
27 //popping the last element out
28 names.pop_back();
29 //printing remaining elements
30 for(int i = 0; i < names.size(); ++i)
31 {
32 cout << names[i] << " ";
33 }
34 cout << endl; 11
EE-163 Lectures 33 to 40
35 //popping the last element out
36 names.pop_back();
37 //printing remaining elements
38 for(int i = 0; i < names.size(); ++i)
39 {
40 cout << names[i] << " ";
41 }
42 cout << endl;
43 return 0;
44 }

8) Passing vectors to functions


1 //Passing vectors to functions
2 #include <iostream>
3 #include <vector>
4
5 using namespace std;
6 //inputintvector needs a vector by reference
7 //since it needs to modify/fill the array
8 void inputintvector(vector<int> &vect, int SIZE);
9
10 //this functions displays all vector contents
11 void displayintvector(vector<int> vect);
12
13 //this function totals all elements of a vector
14 //and returns this sum
15 int sumintvector(vector<int> vect);
16
17 int main(void)
18 {
19 vector<int> grades;
20 int total;
21 cout<<"\nEnter grades vector:\n";
22 inputintvector(grades,6);
23 cout<<"\nContents of grades vector are:\n";
24 displayintvector(grades);
25 cout << endl; 12
26 total=sumintvector(grades);
EE-163 Lectures 33 to 40
27 cout<<"\nTotal of grades="<<total;
28 cout << endl;
29 return 0;
30 }
31
32 int sumintvector(vector<int> vect)
33 {
34 int total = 0;
35 for(int i = 0; i < vect.size(); ++i)
36 {
37 total += vect[i];
38 }
39 return total;
40 }
41
42 void displayintvector(vector<int> vect)
43 {
44 for(int i = 0; i < vect.size(); ++i)
45 {
46 cout << vect[i] << endl;
47 }
48 }
49
50 void inputintvector(vector<int> &vect, int SIZE)
51 {
52 int num;
53 for(int i = 0; i < SIZE; ++i)
54 {
55 cout<<"Enter element "<<i<<":";
56 cin>>num;
57 vect.push_back(num);
58 }
59 }

Note: Unlike arrays, vectors are passed to functions by values. If a


vector needs to be passed by reference, the function prototype and
definition should contain address operator (&) before vector name.13
EE-163 Lectures 33 to 40
9) Function that fills random numbers in vector
1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 #include <vector>
5
6 using namespace std;
7
8 //this functions fills the vector with 'size' no. of elements
9 //and range of random numbers between 1 and 'range'
10 void fillrandominteger(vector<int> &vect, int size, int range);
11
12 //this functions displays all vector contents
13 void displayintvector(vector<int> vect);
14
15 int main()
16 {
17 vector<int> num;
18 int total;
19 cout<<"\nFilling vector with random enteries\n";
20 fillrandominteger(num,10,10);
21 cout<<"\nContents of grades vector are:\n";
22 displayintvector(num);
23 return 0;
24 }
25
26 void fillrandominteger(vector<int> &vect, int size, int range)
27 {
28 srand(time(NULL));
29 int number;
30 for(int i = 0; i < size; ++i)
31 {
32 number = rand() % range + 1;
33 vect.push_back(number);
34 }
} 14
EE-163 Lectures 33 to 40
35 void displayintvector(vector<int> vect)
36 {
37 for(int i = 0; i < vect.size(); ++i)
38 {
39 cout << vect[i] << endl;
40 }
41 }

10) Two functions to do sequential searching in vectors

1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 #include <vector>
5 #include <algorithm>//new header containing advance algorithms
6
7 using namespace std;
8
9 //this functions fills the vector with 'size' no. of elements
10 //and range of random numbers between 1 and 'range'
11 void fillrandominteger(vector<int> &vect, int size, int range);
12
13 //this functions displays all vector contents
14 void displayintvector(vector<int> vect);
15
16 //sequential search for integer vector
17 int searchintvector(vector<int> vect, int value);
18
19 int main()
20 {
21 int found, item;
22 vector<int> numbers;
23
24 //declaring an iterator: a type of
25 //variable that contains a single
26 //vector address
15
EE-163 Lectures 33 to 40
27 vector<int>::iterator location;
28
29 fillrandominteger(numbers,10,100);
30 cout<<"\nThe array contents are:\n";
31 displayintvector(numbers);
32 cout << "\nEnter a value to search for: ";
33 cin >> item;
34 found = searchintvector(numbers, item);
35 if (found > -1)
36 {
37 cout << "Found the item at position: "
38 << found << endl;
39 }
40 else
41 {
42 cout << "Didn't find item in vector."
43 << endl;
44 }
45 cout << "\nEnter another value to search for: ";
46 cin >> item;
47 location=find(numbers.begin(),numbers.end(),item);
48 cout<<"\nFound the item at position:"<<location-numbers.begin();
49 return 0;
50 }
51
52 void fillrandominteger(vector<int> &vect, int size, int range)
53 {
54 srand(time(NULL));
55 int number;
56 for(int i = 0; i < size; ++i)
57 {
58 number = rand() % range + 1;
59 vect.push_back(number);
60 }
61 }
62
16
63
EE-163 Lectures 33 to 40
35 void displayintvector(vector<int> vect)
36 {
37 for(int i = 0; i < vect.size(); ++i)
38 {
39 cout << vect[i] << endl;
40 }
41 }
42
43 int searchintvector(vector<int> vect, int value) {
44 for(int i = 0; i < vect.size(); ++i)
45 {
46 if (vect[i] == value)
47 {
48 return i;
49 }
50 }
51 return -1;
52 }

11) Function to do sorting in vectors

1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 //contains many vector functions
5 #include <vector>
6 //contains sort() function
7 #include <algorithm>
8 using namespace std;
9 //this functions fills the vector with 'size' no. of elements
10 //and range of random numbers between 1 and 'range'
11 void fillrandominteger(vector<int> &vect, int size, int range);
12 //this functions displays all vector contents
13 void displayintvector(vector<int> vect);
14 //this functions displays all vector contents
15 void displaystrvector(vector<string> vect);
16 17
EE-163 Lectures 33 to 40
17 int main()
18 {
19 //********Sorting integer array*********
20 //******** in Ascending Order *********
21 vector<int> numbers;
22 fillrandominteger(numbers, 10, 50);
23 cout<<"\nThe numbers vector is:\n";
24 displayintvector(numbers);
25 sort(numbers.begin(),numbers.end());
26 cout<<"\nThe sorted numbers vector is:\n";
27 displayintvector(numbers);
28
29 //otherwise, filling will
30 //start from where it stopped
31 numbers.clear();
32
33 //********Sorting integer array*********
34 //******* in Descending Order *********
35 fillrandominteger(numbers, 10, 50);
36 cout<<"\nA new numbers vector:\n";
37 displayintvector(numbers);
38 sort(numbers.rbegin(),numbers.rend());
39 cout<<"\nThe sorted numbers vector is:\n";
40 displayintvector(numbers);
41
42 //********Sorting string array*********
43 vector<string> courses;
44 courses.push_back("Circuit Analysis");
45 courses.push_back("Engg Surveying");
46 courses.push_back("Pakistan Studies");
47 courses.push_back("Computers and Programming");
48 courses.push_back("Engg Drawing");
49 courses.push_back("Differential Equations");
50 courses.push_back("Circuit Design");
51 cout<<"\nThe list of courses is:\n";
52 displaystrvector(courses);
18
53 sort(courses.begin(),courses.end());
EE-163 Lectures 33 to 40
54 cout<<"\nThe sorted list of courses is:\n";
55 displaystrvector(courses);
56 return 0;
57 }
58
59 void fillrandominteger(vector<int> &vect, int size, int range)
60 {
61 srand(time(NULL));
62 int number;
63 for(int i = 0; i < size; ++i)
64 {
65 number = rand() % range + 1;
66 vect.push_back(number);
67 }
68 }
69
70 void displayintvector(vector<int> vect)
71 {
72 for(int i = 0; i < vect.size(); ++i)
73 {
74 cout << vect[i] << endl;
75 }
76 }
77
78 void displaystrvector(vector<string> vect)
79 {
80 for(int i = 0; i < vect.size(); ++i)
81 {
82 cout << vect[i] << endl;
83 }
84 }

19
EE-163 Lectures 33 to 40
12) Application program to find palindromes
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4
5 using namespace std;
6
7 void displaystrvector(vector<string> vect);
8
9 string stringreverse(string word);
10
11 int main()
12 {
13 string aword;
14 cout << "Enter a word: ";
15 cin >> aword;
16 string raword = stringreverse(aword);
17 if (raword == aword)
18 cout << aword << " is a palindrome."
19 << endl;
20 else
21 cout << aword << " is not a palindrome."
22 << endl;
23 return 0;
24 }
25
26 void displaystrvector(vector<string> vect)
27 {
28 for(int i = 0; i < vect.size(); ++i)
29 {
30 cout << vect[i] << endl;
31 }
32 }
33
34
20
EE-163 Lectures 33 to 40
35 string stringreverse(string word)
36 {
37 string rword = "";
38 vector<string> wrd;
39 for(int i = 0; i < word.length(); ++i)
40 {
41 wrd.push_back(word.substr(i,1));
42 }
43 //function from algorithm
44 reverse(wrd.begin(), wrd.end());
45
46 //constructing a new string
47 for(int i = 0; i < wrd.size(); ++i)
48 {
49 rword =rword + wrd[i];
50 }
51 return rword;
52 }

21

Вам также может понравиться