use the following pieces of code and write the function printVector that will di
ID: 3605905 • Letter: U
Question
use the following pieces of code and write the function printVector that will display the vector by using a function template, and using a const_iterator for outputting vector elements. (Consider using auto in your iterator's declaration)
// prototype for function template printVector
template <tyename T> void printVector(const vector <T>& integers2);
.....
vecctor <int> integers;
//function push_back is in vectors
integers.push_back(2);
integers.push_back(3);
integers.push_back(4);
cout << " Output vector using iterator notation: ";
printVector(integers);
// Your code here......
Explanation / Answer
#include<iostream>
#include<vector>
using namespace std;
template <typename T>
void printVector(const vector <T>& integers2);
int main()
{
vector <int> integers;
//function push_back is in vectors
integers.push_back(2);
integers.push_back(3);
integers.push_back(4);
cout << " Output vector using iterator notation: ";
printVector(integers);
}
template <typename T>
void printVector(const vector <T>& integers2)
{
for (vector<int>::const_iterator iter = integers2.begin(); iter != integers2.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
}
/*output:
Output vector using iterator notation: 2 3 4
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.