Create a generic function print(ls, n) that prints to standard output the first n elements of list ls. The elements are printed on separate lines. If ls has less than n elements, then the entire list...


Create a generic function print(ls, n) that prints to standard output the first n elements of list ls. The elements are printed on separate lines. If ls has less than n elements, then the entire list is printed.


I attached a picture of what I have so far. I'm having a hard time figuring out how I would make it print a certain number of elements that I specify.


1<br>O#include <iostream><br>#include <list><br>3<br>using namespace std;<br>4.<br>7<br>template <class T><br>Qvoid print(const list<T>& ls,const int & n)<br>{<br>10<br>11<br>for (const T& e : 1s)<br>{<br>cout <« e << endl;<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>Dint main()<br>{<br>list <int> ls ({ 23,31,42,1,4,64,7,3,92 });<br>print(ls,4);<br>19<br>20<br>21<br>22<br>23<br>

Extracted text: 1 O#include #include 3 using namespace std; 4. 7 template Qvoid print(const list& ls,const int & n) { 10 11 for (const T& e : 1s) { cout <« e="">< endl;="" 12="" 13="" 14="" 15="" 16="" 17="" 18="" dint="" main()="" {="" list=""> ls ({ 23,31,42,1,4,64,7,3,92 }); print(ls,4); 19 20 21 22 23

Jun 04, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here