/****************************************************************************
 * This program illustrates the library functions max and insert.
 ****************************************************************************/

#include <algorithm>
#include <string>
#include <vector>
#include<iostream>

using std::string;
using std::vector;
using std::cout;using std::endl;
using std::max;
/* 'width' returns the length of the longest string in a vector of strings. */
string::size_type width(const vector<string>& v)
{
	string::size_type maxlen = 0;
	/* 'int maxlen=0;' would cause the call to 'max' to fail
	 * at compile time because 'max' applies only to values 
	 * of matching type.
	 *
	 * The loop invariant is that maxlen stores the size of the longest
	 * string in the vector encountered before position 'i'.
	 */
	for(vector<string>::size_type i = 0; i != v.size(); ++i)
		maxlen = max(maxlen, v[i].size());/* 'max' returns the larger
						   * of its arguments.
						   */
	return maxlen;
}
/* This function outputs the strings in the parameter vector of strings, 
 * separated by spaces.
 */
void sentence_out(const vector<string> & s)
{
	vector<string>::const_iterator iter=s.begin();
	while(iter!=s.end())
		cout<<*iter++<<" ";
}
	
/*****************************************************************************
 * 'main' loads the vector 'calm' with the strings "The", "homework", "was",
 * "hard." and loads 'emphasis' with "very,", "very,", and "very". Using 
 * 'insert', it creates 'emphatic', that holds the strings 'calm' except that 
 * the strings from 'emphasis' are inserted after the "was" in 'calm'.
 * **************************************************************************/
int main()
{
	vector<string> calm, emphasis, emphatic;
	for(int i=0; i!=2; i++)
		emphasis.push_back("very,");
	emphasis.push_back("very");
	calm.push_back("The");
	calm.push_back("homework");
	calm.push_back("was");
	calm.push_back("hard.");
	emphatic=calm;
	vector<string>::iterator iter=emphatic.begin()+3;
	/* 'insert' takes the values starting at the position of the second
	 * iterator parameter through one before the last iterator parameter 
	 * and inserts them in the structure of the first parameter starting
	 * at the position indicated by that iterator.
	 */
	
	emphatic.insert(iter, emphasis.begin(),emphasis.end());
	sentence_out(calm);
	cout<<endl;
	sentence_out(emphatic);
	cout<<"\nThe longest word in 'emphatic' is "<<width(emphatic)
		<<" characters long.";
	return 0;
}
	
	

