You are on page 1of 26

1. A palindrome is a string that is the same backward as it is forward.

For
example, “tot”
and “otto” are rather short palindromes. Write a program that lets a user enter a
string
and that passes to a bool function a reference to the string. The function should
return
true if the string is a palindrome and false otherwise. At this point, don’t worry
about
complications such as capitalization, spaces, and punctuation. That is, this
simple version
should reject “Otto” and “Madam, I’m Adam.” Feel free to scan the list of string
methods in Appendix F for methods to simplify the task.
cp16ex1.cpp
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 bool ispalindrome(const string &); // function takes a string ref and returns true
if string is palindrome
7
8
9
1
0
1
1
1
2
1
3

int main()
{
string my;

cout << "Enter words (case and punctuation sensitive), q to quit: ";
while (cin >> my && my != "q") // enter words until q is entered
{
if (ispalindrome(my)) // check if entered word is palindrome

1
4

cout << my << " is palindrome!!\n";
else

1
5

cout << my << " is not palindrome!!\n";

1
6

}

1
7

cout << "\nThe end!";

1
8
1
9
2
0}

cin.get();
cin.get();
return 0;

2
1

bool ispalindrome(const string & st) // const & reference, we need original
2 string to compare with reversed version
2
{
2
string temp = st; // save original string to a temporary string
3
2
4
2
5
2
6
2
7
2
}
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3

reverse(temp.begin(), temp.end()); // reverse the temporary
cout << "\nReversed: " << temp << endl; // display it

if (temp == st) // if reversed = original
return true; // then it is palindrome
else
return false; // otherwise, it is not

// function takes a string ref and returns true if string is palindrome 9 1 int main() 0 { 1 1 string my. while (getline(cin. else 1 6 1 cout << my << " is not palindrome!!\n". q to quit: ". but do worry about complications such as capitalization.cpp 1 #include <iostream> 2 #include <string> 3 #include <cctype> // required for isalpha and tolower functions. 1 2 1 3 1 4 cout << "Enter words (capitalization and punctuation ignored). 7 8 bool ispalindrome(const string &). You might find an STL function or two useful although not necessary. spaces. That is. the testing function could reduce the string to “madamimadam” and then test whether the reverse is the same. Do the same problem as given in Programming Exercise 1. although may not be necessary under VS 2010 4 5 6 using namespace std. Don’t forget the useful cctype library.7 3 8 2. For example. and punctuation. cp16ex2. I’m Adam” should test as a palindrome. } .my) && my != "q") // enter words until q is entered { if (ispalindrome(my)) // check if entered word is palindrome 1 5 cout << my << " is palindrome!!\n". “Madam.

convert 3 it to lowercase (to test for equality) and store 1 j++.get(). all proper alphabetic 4 character are stored from 0 to j indexes in a string being examined and edited 3 temp_rev = temp_orig.!. // reduce the string to j places. 2 7 //let's remove all non-alphabetic symbols from a string for (int i = 0. // save string value to temporary variable.get(). return 0. // increase the index to place the next alphabetic character in 3 the same string being examined. else { temp_orig[j] = tolower(st[i]).7 1 8 1 9 2 0 cout << "\nThe end!". no need to create an extra variable 2 } 3 3 } 3 temp_orig. i < st.'). temp_rev. // do the reverse operation . // else.size(). 2 6 int j = 0. i++) // loop through each character in a string { if (!isalpha(st[i])) // if non-alphabetic character is found (such as . thus 2 preserving original passed string untouched 4 2 5 string temp_rev. do 2 8 not store it in a new string 2 9 3 0 continue. 2} 1 2 2 bool ispalindrome(const string & st) // const & reference.begin(). // store the reversed version of alpha-only string to 5 check for equality 3 6 reverse(temp_rev.end()). we need original string to compare with reversed version 2 3{ string temp_orig = st.resize(j). if character is alphabetic. cin. cin.

it is not 4 8 4 9 5 0 5 1 5 2 5 3 5 4 3. // then it is palindrome return false. // otherwise. Then you can use push_back() to . One approach is to use a vector object instead of an array of string.3 so that it gets it words from a file. // display it 4 5 if (temp_rev == temp_orig) // if reversed = original 4 6 else 4 7} return true. Redo Listing 16.3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 cout << "\nReversed: " << temp_rev << endl.

// get the filename 21 (cin >> filename).display the error else .copy how ever many words are in your data file into the vector object and use the size() member to determine the length of the word list.fail()) 25 26 cout << "Error opening file: " << filename << " not found!\n". // if invalid file name or not found . // filename to open 18 vector<string> words.get(). // open it 24 if (fin. // container to store words read from file 19 20 cout << "Enter filename with words to guess: ".cpp 1 #include <iostream> 2 #include <fstream> // include fstream to read files 3 #include <string> 4 #include <cstdlib> 5 #include <ctime> 6 #include <cctype> 7 #include <vector> // include vector to contain array of words read from file 8 9 using namespace std. 14 15 ifstream fin. 10 11 int main() 12 { 13 std::srand(std::time(0)). // ifstream object to read a file 16 string str_finput. // string buffer to read one word at a time from file 17 string filename. cp16ex3. tabs. you should use the >> operator rather than getline(). Because the program should read one word at a time from the file.open(filename). or new lines. The file itself should contain words separated by spaces. 22 23 fin.

56 int guesses = 6.length() > 2) 42 { 43 44 45 46 47 48 49 50 cout << "Total words loaded: " << words. // read one word at a time 31 32 words. // close the file } 37 if (words. It has " << length 58 << " letters.push_back(str_finput).27 { 28 while(!fin.size() << endl.size()].length(). and you guess\n" . // display error msg 40 41 else if (words.length() < 2) // if file was opened but did not contain any words (less than 2 chars in length. // display total # of words loaded to vector array char play. 55 string badchars. cin >> play.size() > 0 && words.close(). // get the random 51 word from vector array. same syntax as with regular array 52 int length = target. while (play == 'y') { string target = words[std::rand() % words. play = tolower(play). // FOR THE REST OF COMMENTS FOR THIS 53 PROGRAM SEE THE TEXTBOOK :) 54 string attempt(length. 57 cout << "Guess my secret word. cout << "Will you play a word game? <y/n> ".eof()) // reading from file until EOF is reached 29 { 30 fin >> str_finput. // immediately store the word in vector array } 33 34 35 36 fin. '-').size() > 0 && words[0]. it is 39 error! 38 cout << "No words to guess or file is empty!\n".size() < 2 && words[0].

while (loc != string::npos) { attempt[loc]=letter. loc + 1). 71 72 73 74 75 } int loc = target. 62 while (guesses > 0 && attempt != target) 63 { 64 char letter. 61 cout << "Your word: " << attempt << endl. 66 cin >> letter.find(letter). 88 loc = target.find(letter) != string::npos) { cout << "You already guessed that. attempt[loc]=letter. 76 --guesses. // add to string 78 79 80 81 82 83 84 85 86 87 } else { cout << "Good guess!\n". if (loc == string::npos) { cout << "Oh.find(letter) != string::npos || attempt. 77 badchars += letter.\n".\n". 70 continue. 67 68 69 if (badchars. bad guess!\n". Try again.59 << "one letter at a time. 89 90 } .find(letter. // check if letter appears again loc = target. loc + 1). 65 cout << "Guess a letter: ". You get " << guesses 60 << " wrong guesses.find(letter.

10 4 } 10 5 } 10 6 cout << "Bye\n". return 0. } 10 9 list.txt 1 2 3 4 5 6 7 8 9 10 apiary beetle cereal danger ensign florid garage health insult jackal keeper loaner .\n". 98 } 99 } 10 0 if (guesses > 0) cout << "That's right!\n". play = tolower(play). 10 3 cin >> play.length() > 0) 96 cout << "Bad choices: " << badchars << endl.91 } 92 cout << "Your word: " << attempt << endl. the word is " << target << ". 10 7 cin.get().get(). 10 1 else cout << "Sorry. 10 8 cin. 10 2 cout << "Will you play another? <y/n> ". 97 cout << guesses << " bad guesses left\n". 93 if (attempt != target) 94 { 95 if (badchars.

int n). 47. 5. (If you decide to use the general unique() function. remove duplicate values. 2. 90.cpp 1 #include <iostream> 2 #include <algorithm> 3 #include <ostream> 4 5 using namespace std. 11. 70. note that it returns the end of the resulting range. Write the function using STL functions. 1. // prototype of reduce function 9 1 int main() 0 { 1 long arr[NUM] = {1. The function should sort an array. 1 . 45. 6 7 const int NUM = 18. and return a value equal to the number of elements in the reduced array. // number of elements in original array 8 int reduce(long []. 47.11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 manage nonce onset plaid quilt remote stolid train useful valid whence xenon yearn zippy 4. 48. 45. 9. Write a function with an old-style interface that has this prototype: int reduce(long ar[]. 4. 73. The actual arguments should be the name of an array and the number of elements in the array. cp16ex4. int). 3. 11.) Test the function in a short program.

ar + n).get(). // sort the array . int n) 2{ 5 2 6 return unique(ar. 2 2 return 0. // disp the info 7 cout << "Array after sort & duplicates removal: ". int newNUM = reduce(arr. // remove duplicate values and immediately return the # of elements of the newly unique and sorted array 2 } 7 2 8 2 9 3 0 3 sort(ar. // reduce array and store the number of 1 new sequence elements into variable 6 cout << "\nNumber of elements in arr[] after reduce: " << newNUM << 1 endl. i < NUM.ar. 2} 3 2 4 int reduce(long ar[]. 2 1 3 int i = 0. ar + n) . NUM).get(). 2 0 cout << arr[i] << " ". 1 4 for (i = 0. i++) // dupe values are still in array after newNUM 1 but not displayed. i++) // display the contents of original array 1 5 cout << arr[i] << " ". STL functions such as erase can be used with list or vector 9 list if needed 2 1 cin. i < newNUM.1 48}. cin. // display sorted & unique 1 original array 8 for (i = 0.

Do the same problem as described in Programming Exercise 4. int n). 70. . // number of elements in original array const int NUM2 = 9. 9.1 3 2 3 3 5. 4. 48}. const int NUM = 18. 45. 73.cpp #include <iostream> #include <algorithm> #include <ostream> #include <string> using namespace std. // array of long(S) string city[NUM2] = {"new york". 47. using both a long instantiation and a string instantiation. 5. 47. "washington". 90. int n). 3. 11. 48. except make it a template function: template int reduce(T ar[]. int n). 1. // reduce template for array of type T with n elements int main() { long arr[NUM] = {1. Test the function in a short program. // template used to display the contents of array T with n elements template <class T> int reduce(T []. "philadelphia". 45. cp16ex5. 2. template <class T> void display_array(T []. 11.

"new york". // sort the array return unique(ar. display_array(arr. int n) { sort(ar. // display newly sorted string array cout << "Total unique City elements: " << cityNewNum. NUM2). NUM). } template <class T> int reduce(T ar[]. display_array(city. ar + n). "miami"}. display_array(arr.get(). // display newly sorted long array cout << "Total unique Arr elements: " << arrNewNum. NUM). arrNewNum). // reduce long array and get the number of sorted & unique elements int cityNewNum = reduce(city. // array of strings cout << "Arr array: ". "tampa". return 0. // remove duplicate values and immediately return the # of elements of the newly unique and sorted array } template <class T> void display_array(T ar[]. int n) .ar. // display the number of new sorted & unique elements cout << "\n\nCities after reduce: ". ar + n) . cin. // same reduce for string array cout << "\n\nArr after reduce: ". "miami". "los angeles".get(). NUM2). // display contents of string array int arrNewNum = reduce(arr. display_array(city. "washington". cityNewNum). // display contents of long array cout << "\nCities array: ". // display the number of new sorted & unique elements cin.

i++) // go through every element of array cout << ar[i] << " ".h 1 #ifndef CUSTOMER_H_ // queue class is reduced to customer class only. 1 2 long when() const { return arrive. i < n. } 6. customer. } 1 3 }. using the STL queue template class instead of the Queue class described in Chapter 12. since for this 2 #define CUSTOMER_H_ // programming exercise we'll use STL queue class instead of our own Queue class from Chapter 12 3 // This queue will contain Customer items 4 class Customer 5 { 6 private: 7 long arrive. // arrival time for customer 8 int processtime.{ for (int i = 0. } int ptime() const { return processtime. 1 4 . // processing time for customer 9 public: 1 0 Customer() { arrive = processtime = 0. Redo the example shown in Listing 12. } 11 void set(long when). // and display its contents cout << endl.15.

h // because we'll use STL queue class instead of our own Queue class from 9 Chapter 12 1 0 .1 5 1 6 inline void Customer::set(long when) 1 7 { processtime = std::rand() % 3 + 1. 1 8 arrive = when. // customer Item class instance is declared here instead of former 8 Queue.h" // include our Customer class without any former Queue class elements from Chapter 12 5 #include <queue> // include queue<typename T> class from STL library for this exercise 6 7 typedef Customer Item.cpp 1 #include <iostream> 2 #include <cstdlib> // for rand() and srand() 3 #include <ctime> // for time() 4 #include "customer. 1 9 } 2 0 #endif 2 1 2 2 cp16ex6.

cin >> qs. using std::endl. // is there a new customer? 2 1 3 int main() 1 { 4 using std::cin. // line queue holds up to qs people std::queue<Item> line. // random initializing of rand() cout << "Case Study: Bank of Heather Automatic Teller\n". 1 bool newcustomer(double x). using std::ios_base. int hours. // # of cycles cout << "Enter the average number of customers per hour: ". cout << "Enter maximum size of queue: ". cout << "Enter the number of simulation hours: ". int qs. formerly Queue line(qs).line. . 1 5 1 6 using std::cout. // hours of simulation cin >> hours.11const int MIN_PER_HR = 60. 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 // setting things up std::srand(std::time(0)). // average # of arrival per hour cin >> perhour. double perhour. // simulation will run 1 cycle per minute long cyclelimit = MIN_PER_HR * hours. // declare the queue STL class variable .

// average time between arrivals 2 8 min_per_cust = MIN_PER_HR / perhour. // add newcomer to line.isfull()".size() == qs) // check to see if queue is full.empty() to 3 reflect new queue STL class . 3 8 else 3 9 { customers++. // new customer data 2 9 3 0 long turnaways = 0. // joined the queue long served = 0.isempty() to !line.7 double min_per_cust.push(temp).set(cycle). STL 3 queue class doesn't have a method to set the size of queue or check if it's full 7 turnaways++. // served during the simulation 3 1 3 2 long sum_line = 0. formerly "line. cycle < cyclelimit. formerly "line. Item temp.empty()) // change from !line." } } 4 if (wait_time <= 0 && !line.enqueue(temp). // time until autoteller is free long line_wait = 0. // cumulative time in line 3 3 3 4 3 5 3 6 // running the simulation for (int cycle = 0. cycle++) { if (newcustomer(min_per_cust)) // have newcomer { if (line. // cycle = time of arrival 4 1 4 2 line. // turned away by full queue long customers = 0. // cumulative line length int wait_time = 0. 4 0 temp.

5 5 cout.precision(2).temp.when(). ios_base::floatfield). 5 3 cout << " customers served: " << served << endl.size(). 5 8 5 9 6 cout << " average wait time: " << (double) line_wait / served << " minutes\n". 4 5 wait_time = temp.pop(). // attend next customer. cout.4 4 { line. 5 7 cout << (double) sum_line / cyclelimit << endl.ptime(). } // reporting results if (customers > 0) { cout << "customers accepted: " << customers << endl.dequeue (temp). // for wait_time minutes 4 6 line_wait += cycle .setf(ios_base::fixed. formerly line.setf(ios_base::showpoint). } . 4 9 5 0 5 1 5 2 sum_line += line. cout << "average queue size: ". 4 7 4 8 } if (wait_time > 0) wait_time--. 5 4 cout << " turnaways: " << turnaways << endl. served++. 5 6 cout.

6 4 return 0. 6 } 5 6 6 // x = average time.get(). in minutes.get(). between customers 6 // return value is true if customer shows up this minute 7 bool newcustomer(double x) 6 8 { 6 9 7 0 7 1 7 2 7 3 7 4 7 5 7 return (std::rand() * x / RAND_MAX < 1). 6 2 cout << "Done!\n". } .0 else 6 1 cout << "No customers!\n". cin. 6 3 cin.

6 7 7 7 8 7 9 8 0 8 1 8 2 8 3 8 4 8 5 8 6 8 7 8 8 8 9 9 0 9 1 9 .

Write a Lotto() function that takes two arguments. // Ticket is now vector<int> for ease of use. // function returns vector<int> (or Ticket). int). typedef vector<int> Ticket. first int argument is a max range (from 1 to max range) to draw Lottery numbers. Suggestion: Have the function create a vector that contains all the possible values. and then use the beginning of the shuffled vector to obtain the values. the numbers selected at random. This would assign to winners a vector that contains six numbers selected randomly from the range 1 through 51. The first should be the number of spots on a lottery card. winners = Lotto(51. The function should return a vector object that contains. // display ticket or vector<int> array .2 9 3 9 4 9 5 9 6 7. Note that simply using rand() doesn’t quite do the job because it may produce duplicate values. and second int argument is how many spots or numbers to guess to win the lotto void DisplayTicket(const Ticket &). For example. you could use the function as follows: vector winners. random_shuffle.6). in sorted order. Also write a short program that lets you test the function. holds lottery numbers (array) Ticket Lotto(int. cp16ex7. use random_shuffle(). The card has numbered spots of which a certain number are selected at random. and the second should be the number of spots selected at random.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> #include <vector> // include vector for vector template #include <algorithm> // include algorithm header to use with sort. and find STL functions using namespace std. A common game is the lottery card.

end()) // compare each winning number to user's picked numbers { cout << myticket[i] << " ".winN). winN. // lotMaxR = max range number for lottery numbers to be drawn.end(). // get proper number from user until 5 or more is entered do { cout << "Enter how many numbers to draw (2 or greater): ". // display playing user's numbers cout << "Number(s) matched on your ticket: ".winN). // display matching numbers from both arrays (lottery drawn numbers and user's quick pick) for (int i = 0.almost impossible win. // if matched. i++) if (find(winners. display the number matchingN++. display epic congratulations message! cout << "Oh my Flying Spaghetti Monster. // display winning numbers cout << "Your ticket numbers: ". // generate random numbers for winning numbers Ticket myticket = Lotto(lotMaxR. else if (matchingN > 5) // display messages for each number of . from 1 to (5 or greater): ". winners. something like QuickPick cout << "Winning numbers: ". you've won min 2x2 game! Prize: 10 FREE tickets!" << endl. // ask for input until 2 or number is entered Ticket winners = Lotto(lotMaxR. from 1 to lotMaxR do { cout << "Enter lottery range. DisplayTicket(winners). display FREE ticket prize msg cout << "Congratulations. i < winN.begin(). if (matchingN == winN && matchingN > 5) // if all winning numbers guessed correctly and the total number of them is 6 or more . matchingN = 0.16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 int main() { int lotMaxR. // and increase the number of matched numbers } if (matchingN == 0) // if no numbers were matched display the message cout << "None" << endl. // ask for how many spots on the lotto card or winning numbers to draw in lottery play } while(cin >> winN && winN < 2). else cout << endl. else if (matchingN == winN && matchingN == 2 ) // special occasion: if out of 2 winning numbers 2 guessed correctly. DisplayTicket(myticket). // ask to input maximum range of possible lottery draw number } while(cin >> lotMaxR && lotMaxR < 5). myticket[i]) != winners. you've guessed all numbers!!! BEER IS ON ME!". // generate random numbers for user's playing ticket.

cin.get(). else switch(matchingN) { case 5: cout << "Congratilations. } cout << "\nBye!".or maybe not :) cout << "Congratulations. // all is done. break.5 Tot = 5.get(). if (Tot < 5) // check for the first argument passed to function { cout << "Invalid number range. i < Ran. break. good bye cin. } Ticket Lotto(int Tot. break. int Ran) // generate and return randomized & sorted array of lottery numbers { Ticket temp. // create a ticket that will be returned once generated with random lottery numbers with respect to arguments passed to function int i = 0. case 3: cout << "Congratulations. Ran = 2. set it to default value of 2.. Good luck!" << endl. } if (Ran < 2) // check the second argument. setting number of winning numbers to default .. return 0. You've guessed 3 winning numbers!! PRIZE: Bubble Gum that sticks to your shoes!" << endl. the greater the number the more fun and expensive prize. for (i = 0. i++) // shuffle the numbers "Ran" many times to have better randomization of numbers ." << endl. You've guessed 4 winning numbers!! PRIZE: Vacation to Siberian Gulag! :P" << endl. if less than 2. we need at least 2 winning numbers to play with { cout << "Invalid ticket request.66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10 0 10 1 10 2 10 3 10 4 10 5 10 6 10 7 correctly guessed numbers.push_back(i+1). break.. You've guessed " << matchingN << " winning numbers!!! PRIZE: Skyscraper in New York City!" << endl. resetting to default: [1-5]" << endl. You've guessed 5 winning numbers!!!! PRIZE: New Yacht filled with naked chicks!" << endl. case 4: cout << "Congratulations. // if range of lottery number max is below 5. default: cout << "You've guessed " << matchingN << " winning number(s)!! Buy more tickets next time. i < Tot. } for (i = 0.2. change it to min allowed . i++) // generate non-random sequence of numbers from 1 to Tot and store it in vector<int> array temp.

sort the numbers 10 return temp.end()). cp16ex8. Mat and Pat want to invite their friends to a party.erase(temp. char> out(cout.begin(). temp. // erase unnecessary numbers from Ran+.begin()+Ran. • Creates a third container that merges the two lists. the max number of elements. // finally. // keyboard input string 1 ostream_iterator<string. temp. // and return the sorted and randomized array 8 } 10 9 void DisplayTicket(const Ticket & t) // function displays victor<int> 110 array or defined as Ticket { for (int i = 0. // output iterator to display 1 .end()). temp. // display the element cout << endl.cpp 1 #include <iostream> 2 #include <set> 3 #include <string> 4 #include <iterator> 5 #include <algorithm> 6 7 8 using namespace std. i++) // go through each element until t. The names are stored in a container and then displayed in sorted order. eliminates duplicates.size(). // I don't like typing std:: all the time 9 1 int main() 0 { 1 string input. • Allows Pat to enter a list of her friends’ names. The names are stored in a second container and then displayed in sorted order.size().begin(). sort(temp. They ask you to write a program that does the following: • Allows Mat to enter a list of his friends’ names. "\n"). is reached cout << t[i] << " ". numbers from 1 to Ran are kept in. and displays the contents of this container. i < t. temp.random_shuffle(temp.end()). } 8.

2 copy(pats_friends. comb_friends.insert(input). pats_friends. pats_friends.is used because it automatically sorts the elements 1 and allows only unique items in it 4 cout << "Enter guest list for Mat (s to stop): \n". // display entered friends for Mat by using iterator out 1 8 2 1 cout << "\nEnter guest list for Pat (s to stop): \n".input) && input != "s") // enter friends until "s" is entered 5 for the name 1 mats_friends.input) && input != "s") // get friends for Pat // set . // store a friend in container set 6 cout << "Mat's friends list: " << endl.end(). 1 while (getline(cin. out).end(). // copy mat's and pat's friends into comb_friends set 2 cout << "\nPat's and Mat's friends who are welcome to Party: \n". // Pat's friend container set 2 0 while (getline(cin. .get().get(). // store them in Pat's container set cout << "Pat's friends list: " << endl. cin. 5 comb_friends. 2 friends // and display Pat's 2 3 set<string>comb_friends. out).end(). 2 pats_friends.begin().insert(input).begin(). // set 6 automatically sorts & removes duplicate values 2 copy(comb_friends.begin().end().begin())). insert_iterator<set<string> >(comb_friends.begin(). // container set to hold list of Mat's friends 1 9 set<string> pats_friends. mats_friends. and unique list of friends of Mat and Pat 2 8 2 9 3 0 3 1 cin.end(). sorted. out).begin(). mats_friends. return 0.2 container elements 1 3 set<string> mats_friends. pats_friends. 1 7 copy(mats_friends. // this set holds both unique and sorted list of 2 Mat's and Pat's friends 4 set_union(mats_friends. // display the final 7 merged.

3 2 3 3 3 4 3 5 3 6 3 7 .