http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
Chapter 8 // Exercise 2
2. Write a function print() that prints a vector of ints to cout. Give it two arguments: a string for 'labeling' the output and a vector.
Not quite sure what he means when he wants us to 'label' the output. Does he mean a note that says "these numbers are from this vector"? That's how I've interpreted it anyway. For the print() function we pass a string reference to avoid copying data and since we are just printing the contents of a vector there is no need to modify it so it is passed via const reference. For this exercise I added a little in main just to show the function working.
Not quite sure what he means when he wants us to 'label' the output. Does he mean a note that says "these numbers are from this vector"? That's how I've interpreted it anyway. For the print() function we pass a string reference to avoid copying data and since we are just printing the contents of a vector there is no need to modify it so it is passed via const reference. For this exercise I added a little in main just to show the function working.
#include "stdafx.h"
#include "std_lib_facilities.h"
//prints a given vector to the screen with a label
void print(string& label, const vector<int>& v)
{
cout << label << ": " << endl;
for (int i = 0; i < v.size(); ++i)
cout << v[i] << endl;
cout << '\n';
}
int main()
{
vector<int> numberVector = { 1,2,3,4,5,6,7,8,9,10 };
string label = "Vector of Numbers";
print(label, numberVector);
keep_window_open();
return 0;
}
No comments:
Post a Comment