// Bjarne Stroustrup 1/15/2010 // Chapter 8 Exercise 14 /* Write a function that takes a vector argument and returns a vector containing the number of characters in each string. Also find the longest and shortest string and the lexicographically first and last string. How many separate functions would you use for these tasks? Why? */ #include "std_lib_facilities.h" vector get_sizes(const vector& vs) { vector res(vs.size()); for (int i =0; i& v) { if (v.size()==0) error("longest(): empty vector"); // protect against the empty string int m = v[0].size(); // we now know that there is a v[0] int max_index = 0; for (int i=0; i& v) { if (v.size()==0) error("shortest(): empty vector"); // protect against the empty string int m = v[0].size(); // we now know that there is a v[0] int min_index = 0; for (int i=0; i& v) { if (v.size()==0) return ""; // the empty string is first int first_index = 0; for (int i=0; i& v, int& last) { if (v.size()==0) { last = -1; // -1 indicates "the empty vector" return; } int last_index = 0; for (int i=0; i. I might have chosen to do that if I knew that this was exactly the information I always wanted. However, I chose separate functions, each being simple because each does a single thing and each doing its individual task without overhead from producing related results that I may not need. */ int main() try { vector vs; vs.push_back("Technicalities"); vs.push_back("a"); vs.push_back("A"); vs.push_back("hellohellohell"); // same length as "Technicalities" vs.push_back("Hellohellohell"); // lexicographically 'H' < 'h' // vs.push_back(""); // the empty string vs.push_back("Technicalities"); // More technicalities vs.push_back("!"); // same length as "a" cout << "sizes: "; vector lengths = get_sizes(vs); for (int i=0; i