8.2

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void print(string& s,vector<int>& v)
{
	cout<<s<<": ";
	int i;
	for(i=0;i<v.size();i++)
	{
		cout<<v[i]<<" ";
	}
	cout<<endl;
}

int main()
{
	string s="hello";
	vector<int> v;
	int i;
	for(i=0;i<5;i++)
	{
		v.push_back(i);
	}
	print(s,v);
	system("pause");
}

8.3
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void print(string& s,vector<int>& v)
{
	cout<<s<<": ";
	int i;
	for(i=0;i<v.size();i++)
	{
		cout<<v[i]<<" ";
	}
	cout<<endl;
}

void fibonacci(int x,int y,vector<int>& v,int n)
{
	v.push_back(x);
	v.push_back(y);
	int z,i;
	for(i=0;i<n;i++)
	{
		z=x+y;
		v.push_back(z);
		x=y;
		y=z;
	}
}

int main()
{
	int x,y,n;
	vector<int> v;
	string s="斐波那契數列為";

	cout<<"請輸入斐波那契數列的前兩個數:"<<endl;
	cin>>x>>y;

	cout<<"請輸入想要產生的斐波那契數列長度:"<<endl;
	cin>>n;

	fibonacci(x,y,v,n);
	print(s,v);
	system("pause");
}

8.7
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<string> name,cope;
	vector<double> age;
	string s;
	double d;
	int i,k;

	cout<<"請輸入名字:"<<endl;
	while(cin>>s)
	{
		name.push_back(s);
		cope.push_back(s);
	}
	cin.clear();    //清空快取
	cout<<"請輸入相應名字的年齡:"<<endl;
	for(i=0;i<name.size();i++)
	{
		cout<<name[i]<<" :";
		cin>>d;
		age.push_back(d);
	}

	sort(name.begin (),name.end ());

	cout<<"排序後為:"<<endl;
	for(i=0;i<name.size ();i++)
	{
		cout<<name[i]<<"	";
		for(k=0;k<name.size ();k++)
		{
			if(cope[k]==name[i]) cout<<age[k]<<endl;
		}
	}
	system("pause");
}

9.2
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Name_pairs
{
public:
	void read_names();
	void read_age();
	void print();
	void sorts();
private:
	vector<string> name,cope;
	vector<double> age,new_age;
};
void Name_pairs::read_names()
{
	string s;
	cout<<"請輸入名字:"<<endl;
	while(cin>>s)
	{
		name.push_back(s);
		cope.push_back(s);
	}
	cin.clear();
}
void Name_pairs::read_age()
{
	int i;
	double d;
	cout<<"請輸入對應名字的的年齡:"<<endl;
	for(i=0;i<name.size();i++)
	{
		cout<<name[i]<<" :";
		cin>>d;
		age.push_back(d);
		new_age.push_back(d);
	}
	cin.clear();
}
void Name_pairs::print()
{
	int i;
	for(i=0;i<name.size();i++)
	{
		cout<<name[i]<<"	"<<new_age[i]<<endl;
	}
}
void Name_pairs::sorts()
{
	int i,k;
	sort(name.begin(),name.end());
	for(i=0;i<name.size();i++)
	{
		for(k=0;k<cope.size();k++)
		{
			if(cope[k]==name[i])
			{
				new_age[i]=age[k];
			}
		}
	}
}

int main()
{
	Name_pairs NP;
	NP.read_names();
	NP.read_age();
	cout<<"排序前:"<<endl;
	NP.print();
	cout<<"排序後:"<<endl;
	NP.sorts();
	NP.print();
	system("pause");
}