04737_C++程序设计_第7章_类模板与向量

时间:2023-03-09 19:19:28
04737_C++程序设计_第7章_类模板与向量

例7.1

使用类模板的实例。

例7.2

求4个数中最大值的类模板程序。

 #include <iostream>

 using namespace std;

 template <class T>

 class Max4
{
T a, b, c, d;
T Max(T a, T b)
{
return (a > b) ? a : b;
}
public:
Max4(T, T, T, T);
T Max(void);
}; template <class T>//定义成员函数必须再次声明模板
Max4<T>::Max4(T x1, T x2, T x3, T x4) :a(x1), b(x2), c(x3), d(x4)
{ } template <class T>//定义成员函数必须再次声明模板
T Max4<T>::Max(void)//定义时要将Max4<T>看做整体
{
return Max(Max(a, b), Max(c, d));
} void main()
{
Max4<char>C('W', 'w', 'a', 'A');//比较字符
Max4<int>A(-, -, -, -);//比较整数
Max4<double>B(1.25, 4.3, -8.6, 3.5);//比较双精度实数 cout << C.Max() << " " << A.Max() << " " << B.Max() << endl;//输出 w -25 4.3 system("pause");
}

例7.4

设计一个非模板类Point类,然后设计一个继承Point类的类模板Line。

 #include <iostream>

 using namespace std;

 class Point//非模板类Point
{
int x, y;
public:
Point(int a, int b)
{
x = a;
y = b;
}
void display()
{
cout << x << "," << y << endl;
}
}; template <typename T>//类模板 class Line :public Point
{
T x2, y2;
public:
Line(int a, int b, T c, T d) :Point(a, b)
{
x2 = c;
y2 = d;
}
void display()
{
Point::display();
cout << x2 << "," << y2 << endl;
}
}; void main()
{
Point a(, );//对象a是整数坐标
a.display(); Line<int>ab(, , , );//线段ab的两个坐标均是整数
ab.display(); Line<double>ad(, , 6.5, 7.8);//线段ad的一个坐标是整数,另一个是实数
ad.display(); system("pause");
}

例7.5

设计一个模板类Point,然后公有派生一个模板类Line。

 #include <iostream>

 using namespace std;

 template <typename T>
class Point
{
T x, y;
public:
Point(T a, T b)
{
x = a;
y = b;
}
void display()
{
cout << x << "," << y << endl;
}
}; template <typename T>
class Line :public Point<T>
{
T x2, y2;
public:
Line(T a, T b, T c, T d) :Point<T>(a, b)
{
x2 = c;
y2 = d;
}
void display()
{
Point<T>::display();
cout << x2 << "," << y2 << endl;
}
}; void main()
{
Point<double>a(3.5, 8.8);
a.display(); Line<int>ab(, , , );//全部使用整数
ab.display(); Line<double>ad(4.5, 5.5, 6.5, 7.5);//全部使用实数
ad.display(); system("pause");
}

例7.6

演示泛型指针和copy函数的例子。

 #include <iostream>
#include <algorithm>
#include <vector>
#include <iterator> using namespace std; void main()
{
double a[] = { 1.1,4.4,3.3,2.2 };
vector<double>va(a, a + ), vb();//定义并初始化向量va
typedef vector<double>::iterator iterator;//自定义一个正向泛型指针标识符iterator
iterator first = va.begin();//定义正向泛型指针first并指向va的首元素 for (first; first < va.end(); first++)//循环正向输出va
{
cout << *first << " ";
}
for (--first; first > va.begin() - ; first--)//循环逆向输出va
{
cout << *first << " ";
}
copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//整体正向输出va
cout << endl; typedef vector<double>::reverse_iterator reverse_iterator;//自定义一个逆向泛型指针标识符
reverse_iterator last = va.rbegin();//定义逆向泛型指针last并指向va的尾元素 for (last; last < va.rend(); last++)//使用逆向指针循环从尾到首输出va
{
cout << *last << " ";
}
for (--last; last > va.rbegin() - ; last--)//使用逆向指针循环从首到尾输出va
{
cout << *last << " ";
}
copy(va.rbegin(), va.rend(), ostream_iterator<double>(cout, " "));//整体从尾到首输出va system("pause");
}

例7.7

演示向量使用实数类型的例子。

 #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <iterator> using namespace std; void main()
{
double a[] = { 1.1,4.4,3.3,2.2 };
vector<double>va(a, a + ), vb();//定义实数向量va copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//正向输出va
cout << endl; reverse_copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//逆向输出va
cout << endl; reverse_copy(va.begin(), va.end(), vb.begin());//va逆向复制给vb copy(vb.begin(), vb.end(), ostream_iterator<double>(cout, " "));//正向输出vb
cout << endl; sort(va.begin(), va.end());//va升幂排序
sort(vb.begin(), vb.end(), greater<double>());//vb降幂排序 copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//正向输出va
cout << endl; copy(vb.begin(), vb.end(), ostream_iterator<double>(cout, " "));//逆向输出vb
cout << endl; va.swap(vb);//交换va和vb的内容 copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//正向输出va
cout << endl; copy(vb.begin(), vb.end(), ostream_iterator<double>(cout, " "));//正向输出vb
cout << endl; cout << *find(va.begin(), va.end(), 4.4);//在va中查找4.4 system("pause");
}

例7.8

演示使用复数类和结构作为向量数据元素的例子。

 #include <iostream>
#include <complex>
#include <vector> using namespace std; struct st
{
int a, b;
}a[] = { {,},{,} }; void main()
{
complex<float>num[] = { complex<float>(,),complex<float>(3.5,4.5) };
vector<complex<float>*>vnum();//复数类的指针作为向量的数据类型 vnum[] = &num[];
vnum[] = &num[]; for (int i = ; i < ; i++)
{
cout << "real is " << vnum[i]->real() << ",imag is" << vnum[i]->imag() << endl;
} vector<st *>cp();//结构指针作为向量的数据类型
cp[] = &a[];
cp[] = &a[]; for (int i = ; i < ; i++)
{
cout << "a=" << cp[i]->a << ",b=" << cp[i]->b << endl;
} system("pause");
}

例7.11

演示使用泛型指针进行插入和删除实例。

 #include <iostream>
#include <vector>
#include <algorithm>
#include <iterator> using namespace std; void main()
{
char st[] = "abcdefghij"; vector<char>a(st, st + );//不复制标志"\0"
vector<char>::iterator p;//定义泛型指针p p = a.begin();//p指向第1个元素的指针 a.insert(p + , 'X');//a[3]='X' copy(a.begin(), a.end(), ostream_iterator<char>(cout, " "));//输出向量a的内容
cout << endl; p = a.begin();//p返回首位值 a.insert(p, , 'A');//在a[0]前插入3个A copy(a.begin(), a.end(), ostream_iterator<char>(cout, " "));//输出增加A A A后的内容
cout << endl; a.erase(p + );//删除a[8],即第9个元素e copy(a.begin(), a.end(), ostream_iterator<char>(cout, " "));//输出删除e之后的内容
cout << endl; system("pause");
};

例7.12

演示双向访问的例子。

 #include <iostream>
#include <vector> using namespace std; void main()
{
char st[] = "abcdefghij"; vector<char>a(st, st + );
vector<char>::iterator p = a.begin();//定义正向泛型指针并初始化
vector<char>::reverse_iterator ps;//定义逆向泛型指针 for (p = a.begin(); p != a.end(); ++p)//正向访问
{
cout << *p << " ";//输出a b c d e f g h i j
}
cout << endl; for (p = a.end() - ; p != a.begin() - ; --p)//使用正向泛型指针逆向访问
{
cout << *p << " ";//输出j i h g f e d c b a
}
cout << endl; for (ps = a.rbegin(); ps != a.rend(); ++ps)//使用逆向泛型指针正向访问,使用++运算
{
cout << *ps << " ";//输出j i h g f e d c b a
}
cout << endl; for (--ps; ps != a.rbegin() - ; --ps)//使用逆向泛型指针逆向访问,使用--运算
{
cout << *ps << " ";//输出a b c d e f g h i j
}
cout << endl; system("pause");
};

出圈游戏

 #include <iostream>
#include <vector> using namespace std; class SeqList
{
char name[];
public:
void DispName()
{
cout << name;
}
void SetName(char b[])
{
strcpy_s(name, b);
}
void Joseph(vector<SeqList>&);
};
//Joseph函数
void SeqList::Joseph(vector<SeqList>&c)
{
int m, star, i, j, k; cout << "请输入间隔数m(m<=20)";
cin >> m;//间隔数
while (m > )//间隔数大于20,重新输入
{
cout << "间隔太大,请重新输入:";
cin >> m;
} cout << "从第几个人的位置开始报数(不能大于" << c.size() << "):";
cin >> star;
while (star > c.size())
{
cout << "开始位置大于人数,重新输入:";
cin >> star;
} cout << "准备输入名字" << endl;
getchar();//消除回车干扰
//输入参加游戏人的名字
char s[];
for (i = ; i < c.size(); i++)
{
cout << "第" << i + << "个人的名字:";
gets_s(s);
c[i].SetName(s);
} i = star - ;//为方便编程,从规定开始报数处再减1作为计数依据
vector<SeqList>::iterator p;
p = c.begin();
int length = c.size();
for (k = ; k <= length; k++)
{
j = ;//报数
while (j < m)
{
i++;
if (i == c.size())//到终点,返回第一个位置计数
{
i = ;
}
j++;
}
if (k == length)
{
break;
}
c[i].DispName();//输出出圈人的信息
cout << ",";
c.erase(p + i);//删除出圈人的记录
--i;//调整计数位置初始值
}
//break语句跳转至此处,输出最后出列的编号
c[i].DispName();
cout << endl;
} void main()
{
int length = ; cout << "请输入人数:";
cin >> length; vector<SeqList>c(length);
SeqList game;
game.Joseph(c); system("pause");
};