1 Standard Template Library (STL) – C++ Vectors - A collection of data types and algorithms In C++, we can use vectors, rather than arrays. Vectors are declared with the following syntax: vector<type> variable_name (number_of_elements); // declares a vector of 5 integers vector<int> values (5); // declares a vector of 20 doubles vector<double> marks (20); When we use vectors in our programs, we must provide, at the top of the file, the line: #include <vector> In the following example, the program asks the user for the marks for a group of 20 students and stores them in a vector: #include <iostream> #include <vector> using namespace std; int main() { vector<double> student_marks (20); for (int i = 0; i < 20; i++) { cout << "Enter marks for student #" << i+1 << ": "; cin >> student_marks[i]; } return 0; } 1 2 If we don't know in advance that there are 20 students, we could ask the user how many students are there, and resize the vector accordingly. This is shown in the example below: #include <iostream> #include <vector> using namespace std; int main() { // no size specified vector<double> student_marks; int num_students; cout << "Number of students in this group: "; cin >> num_students; //set array size student_marks.resize (num_students); for (int i = 0; i < num_students; i++) { cout << "Enter marks for student #" << i+1 << ": "; cin >> student_marks[i]; } return 0; } 2 3 We can use the push_back() method to append one element at the end of the array. The operation includes resizing to one more element to accommodate for the extra element, and storing the given value at the end of the array. The example below shows how to use the push_back() method to accept numbers from the user and store them in a vector, until the user indicates that there are no more numbers. After all the numbers were entered, we use for loop to print them in the order that they were entered (this is just for illustrations purpose -- it's not particularly useful to do that!): #include <iostream> #include <vector> using namespace std; int main() { vector<double> student_marks; int number; char answer; cout << "Do you want to enter numbers (y/n)? "; cin >> answer; while (answer == 'y') { cout << "Enter value: "; cin >> number; // Now that we have the number from the user, // append it at the end of the vector student_marks.push_back(number); cout << "Do you want to enter more numbers (y/n)? "; cin >> answer; } 3 4 // // // // Now print all the values; notice that we didn't need to count how many elements were entered: we can always use the size() method to ask student_marks how many are there! for (int i { cout << << } // '\t' = 0; i < student_marks.size(); i++) "Student #" << i+1 << '\t' student_marks[i]; is the code for the TAB character return 0; } 4 5 // Find minimum and Maximum numbers #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> vObject(5); int i; vObject[0] vObject[1] vObject[2] vObject[3] vObject[4] = = = = = 3; 12; 21; 73; 1; cout << "Contents of vObject: "; for(i = 0; i <vObject.size(); i++) cout << vObject[i] << " "; cout << "\nMaximum element is: "; cout << *max_element(vObject.begin(), vObject.end()); cout << "\nMinimum element is: "; cout << *min_element(vObject.begin(), vObject.end()); // Now sort the vObject elements. sort(vObject.begin(), vObject.end()); cout << "Sorted content of vObject: "; for(i = 0; i <vObject.size(); i++) cout << vObject[i] << " "; return 0; } 5 6 #include <iostream> #include <vector> #include <numeric> using namespace std; int main() { vector<int> v; int n; // Populating the vector from value of 1 to 10. for(int i = 1; i <= 10; ++i) { cout << “Enter integer number: “; cin >> n; v.push_back(n); // load the number into vector v } // accumulate sum up all the value in the specified // range. In this case the green is from v.begin // (the start of the vector) to v.end (the end of // element in the vector). By specifying a value of // 0.0, it sum up everything into a double value // type. The double value is used to avoid integer // computation in the division which would cause // the result to be truncated. // As for v.size, it returns the number of element // in the vector cout << "Mean :" << accumulate(v.begin(), v.end(), 0.0) / v.size() << endl; } 6