Lesson 11 Vectors
Lesson 11 Vectors
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.
A Generic Class
* 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 }
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 }
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