Concept explainers
The Standard Template Library (STL) includes the collection of data types and
- A special data type called vector is offered by STL which is useful for standard arrays.
A vector stores the elements or values in sequence order and the values are stored in continuous memory locations.
- The subscript operator “[]” in the vector helps to read the content of each element.
Syntax:
Syntax to define a vector is as follows:
//Declaring an empty vector
vector <data_type> name_of_vector;
In the above line,
- The keyword “vector” represents the vector declaration.
- The “<data_type>” represents the type of data the vector can hold; the data types such as “int”, “float”, “string”, “char”, and “double”.
- The “name_of_vector” represents the name of the vector.
Storing values in a vector:
To store a value in a vector, a special member function called “push_back()” has been used.
Consider the following format to store value in a vector:
//store the value in vector
Name_of_vector.push_back(element_for_insertion);
In the above line,
- The “Name_of_vector” represents the vector name.
- The “Push_back()” is a member function which is used to store values in a vector.
- The “element-for-insertion” is the parameter which holds the value to be stored in the vector.
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Starting Out with C++ from Control Structures to Objects Plus MyLab Programming with Pearson eText -- Access Card Package (9th Edition)
- Question2: Define an integer vector and ask the user to give you values for the vector. because you used a vector, so you don't need to know the size. user should be able to enter a number until he wants(you can use while loop for termination. for example if the user enters any negative number, but until he enters a positive number, your program should read and push it inside of the vector). then calculate the average of all numbers he entered. find minimum number in the vector find maximum number in the vector find size of the vectorarrow_forwardComplete the following function that counts the even numbers in a 2D vector of integers. int count_evens(const std::vector<std::vector<int>>& v) { // Add your code... } Please add output screenshot!arrow_forwardDeclare a vector named pizzasList that stores 3 items of type PizzaIngredients.Only lines 12-14 can be affected, the rest of the program cannot be changed.arrow_forward
- Declare a vector named pizzasInStore that stores 3 items of type PizzaIngredients. Only lines 12-14 can be affected, the rest of the program cannot be changed.arrow_forward1- Define an integer vector and ask the user to give you values for the vector. because you used a vector, so you don't need to know the size. user should be able to enter a number until he wants(you can use while loop for termination. for example if the user enters any negative number, but until he enters a positive number, your program should read and push it inside of the vector). the calculate the average of all numbers he entered. 2- Write a program that asks the user to enter integers as the width and length of a rectangle that should be stored in the variables 'a' and 'b' respectively. There are also two integer pointers named ptrA and ptrB. Assign the values of 'a' and 'b' to ptrA and ptrB respectively, Calculate the are and perimeter of the rectangle by using these pointers.arrow_forward# phyton A vector contains the grades obtained by students of a class in an assessment, with the following characteristics: Notes are actual values between zero and ten.If the value is equal to -1.0, then this means that the student has missed the assessment.The average approval rate at this University is 6 (six).Write a program that reads two vectors: the note vector described and a vector containing the students' names of the same size. As an output, print the following information in the console in this order: How many students missed the evaluation?How many students have been approved?Among the students present, how many students failed?Among the students present, what was the average grade, with up to two decimal places of accuracy?What's the name of the student who got the highest grade in the class?TipsCreate different counters to find the value of outputs 1 through 4. These counters are different from the loop counter that traverses the vector;Within the while loop, use five…arrow_forward
- Please complete the following functions: 1. int loadDictionary(istream& dictfile, vector<string>& dict) 2. int permute(string word, vector<string>& dict, vector<string>& results) 3. void display(vector<string>& results) PLEASE USE VECTORS TO SOLVE. I will upvote if correct.arrow_forwardCreate an vector of foods in C++ and then ask user for its favourite food. If favourite food is present then print "your food will be served soon" otherwise print "Sorry".arrow_forwardHow to use a getline(cin, ) function for a vector to make sure that it stores in the vector. First it skips the first one. Then, I added cin.ignore(), but then I have to enter it twice but it doesn't store it in the vector. This is what I have now: cout << "\n\nPlease input five Colleges to compare and their cities.\n";for (vector<string>::size_type num=0; num < 5; num++){cin.get();cout << "\n\nPlease enter the name for college # " << (num + 1) << ": \n";getline(cin, College[num]);CULIST.push_back(College[num]);cout << "Enter city of College # "<<(num+1)<< ": \n";getline(cin,location[num]);CULIST.push_back(location[num]);arrow_forward
- Part B:Given a vector of size S and a number N. Write a function that calculates if there isany pair of numbers in the vector whose sum is equal to N? Hint: you can use the binary search function you implemented in part A. Notes:You must use iterators in your implementation. Input Format:The input consists of 2 lines. The first line contains 2 numbers, S and N. The secondline contains S numbers which represent the contents of the vector. Input:3 53 1 2 Output:YESarrow_forwardQuestions: What will be the first from and to towers, given that there is an odd (not even) number of rings? Once the towers are initialized, what rings will be on towers 1, 2, and 3? Given the C++ code: // toh.cpp#include <iostream>#include <vector>using namespace std; int main() { vector<int> t[3]; int n; cout << "Enter the number of rings: "; cin >> n; int from = 0; int candidate = 1; int to, move = 0; if ( n % 2 == 0) to = 2; // (from + to)%mod3, counter clockwise else to = 1; // (from + to)%mod3, clockwise for ( int i = n + 1 ; i >= 1; i-- ) t[0].push_back(i); t[1].push_back(n+1); t[2].push_back(n+1); if ( n % 2 == 0 ) { while ( t[1].size() < (n+1) ) { cout << "move " << ++move << ":" << " Move candidate " << candidate; cout << " from tower " << (char) (from + 'A'); cout << " to tower "…arrow_forwardAdd each element in origList with the corresponding value in offsetAmount. Print each sum followed by a space. Ex: If origList = {40, 50, 60, 70} and offsetAmount = {5, 7, 3, 0}, print: 45 57 63 70 #include <iostream>#include <vector>using namespace std; int main() {const int NUM_VALS = 4;vector<int> origList(NUM_VALS);vector<int> offsetAmount(NUM_VALS);unsigned int i; for (i = 0; i < origList.size(); ++i) {cin >> origList.at(i);} for (i = 0; i < offsetAmount.size(); ++i) {cin >> offsetAmount.at(i);} cout << endl; return 0;} Please help me with this problem using c++.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning