C++ primer plus第六版课后编程题答案16.8


 

main168.cpp

#include <iostream>
#include <set>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;
//由题知道,键值唯一,可以知道,应该用set
static void output(string str)
{
	cout<<str<<"  ";
}


void main168()
{
	set<string> Mat;
	set<string> Pat;

	string temp;
	cout<<"Mat ,plesase enter your friends name(q to quit):";
	cin>>temp;
	while(temp!="q")
	{
		Mat.insert(Mat.begin(),temp);
		cout<<"\nMat ,plesase enter your friends name continue(q to quit):";
		cin>>temp;
	}
	cout<<"Mat's friend list: ";
	for_each(Mat.begin(),Mat.end(),output);

	cout<<"\nNow ,Pat ,please enter your friend's name:";
	cin>>temp;
	while(temp!="q")
	{
		Pat.insert(Pat.begin(),temp);
		cout<<"\nPat ,plesase enter your friends name continue(q to quit):";
		cin>>temp;
	}
	cout<<"\nPat's friend list: ";
	for_each(Pat.begin(),Pat.end(),output);

	set<string> All;
	//transform(Mat.begin(),Mat.end(),All.begin(),[](set<string>::iterator it){return it;});//
	//set_union(Mat.begin(),Mat.end(),Pat.begin(),Pat.end(),All.begin());//用set_union
	set_union(Mat.begin(),Mat.end(),Pat.begin(),Pat.end(),insert_iterator<set<string> >(All,All.begin()));//p703
	cout<<"\nAll list:";
	for_each(All.begin(),All.end(),output);
	cin.get();
	cin.get();


}

 

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注