1.Assume proper includes have been executed, but no using directive or declarati
ID: 3577335 • Letter: 1
Question
1.Assume proper includes have been executed, but no using directive or declaration. Write a definition of an iterator for a vector of ints that is initialized to point to the first member of the vector vec.
2.Suppose you want to run code that involves the loop
//Assume vector v and iterator p has been defined and
//given appropriate values
for (p = v.begin(); p != v.end(); p++)
cout << *p << " ";
Which of the following could you use to declare the iterator p? Why?
std::vector<int>::iterator p;
std::vector<int>::const_iterator p;
3.Declare a stack template container to hold values of type double using the default container.
4.If myVec has type vector<double> what type does myVec.front() return?
5.Given the following search function declaration, give the corresponding declaration for a templated search function?
int search( int array[], int start, int target, int size);
//pre: start is > 0, and < size
//the position of the first occurance of target at or after start is returned, or -1 is returned
Explanation / Answer
1)
using Iter = std::vector<int>::const_iterator;
Iter it = vec.begin(); // it points to first element og vector
2)
std::vector<int>::const_iterator p;
Because, we can change the content of vector while iterating if
iterator is not constant
3)
std::stack<double> s;
4)
If myVec has type vector<double> thenmyVec.front() return double value
5) template <typename T>
int search( T array[], int start, T target, int size);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.