zoukankan      html  css  js  c++  java
  • 04737_C++程序设计_第7章_类模板与向量

    例7.1

    使用类模板的实例。

    例7.2

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

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 template <class T>
     6 
     7 class Max4
     8 {
     9     T a, b, c, d;
    10     T Max(T a, T b)
    11     {
    12         return (a > b) ? a : b;
    13     }
    14 public:
    15     Max4(T, T, T, T);
    16     T Max(void);
    17 };
    18 
    19 template <class T>//定义成员函数必须再次声明模板
    20 Max4<T>::Max4(T x1, T x2, T x3, T x4) :a(x1), b(x2), c(x3), d(x4)
    21 {
    22 
    23 }
    24 
    25 template <class T>//定义成员函数必须再次声明模板
    26 T Max4<T>::Max(void)//定义时要将Max4<T>看做整体
    27 {
    28     return Max(Max(a, b), Max(c, d));
    29 }
    30 
    31 void main()
    32 {
    33     Max4<char>C('W', 'w', 'a', 'A');//比较字符
    34     Max4<int>A(-25, -67, -66, -256);//比较整数
    35     Max4<double>B(1.25, 4.3, -8.6, 3.5);//比较双精度实数
    36 
    37     cout << C.Max() << " " << A.Max() << " " << B.Max() << endl;//输出 w -25 4.3
    38 
    39     system("pause");
    40 }

    例7.4

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

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Point//非模板类Point
     6 {
     7     int x, y;
     8 public:
     9     Point(int a, int b)
    10     {
    11         x = a;
    12         y = b;
    13     }
    14     void display()
    15     {
    16         cout << x << "," << y << endl;
    17     }
    18 };
    19 
    20 template <typename T>//类模板
    21 
    22 class Line :public Point
    23 {
    24     T x2, y2;
    25 public:
    26     Line(int a, int b, T c, T d) :Point(a, b)
    27     {
    28         x2 = c;
    29         y2 = d;
    30     }
    31     void display()
    32     {
    33         Point::display();
    34         cout << x2 << "," << y2 << endl;
    35     }
    36 };
    37 
    38 void main()
    39 {
    40     Point a(3, 8);//对象a是整数坐标
    41     a.display();
    42 
    43     Line<int>ab(4, 5, 6, 7);//线段ab的两个坐标均是整数
    44     ab.display();
    45 
    46     Line<double>ad(4, 5, 6.5, 7.8);//线段ad的一个坐标是整数,另一个是实数
    47     ad.display();
    48 
    49     system("pause");
    50 }

    例7.5

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

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 template <typename T>
     6 class Point
     7 {
     8     T x, y;
     9 public:
    10     Point(T a, T b)
    11     {
    12         x = a;
    13         y = b;
    14     }
    15     void display()
    16     {
    17         cout << x << "," << y << endl;
    18     }
    19 };
    20 
    21 template <typename T>
    22 class Line :public Point<T>
    23 {
    24     T x2, y2;
    25 public:
    26     Line(T a, T b, T c, T d) :Point<T>(a, b)
    27     {
    28         x2 = c;
    29         y2 = d;
    30     }
    31     void display()
    32     {
    33         Point<T>::display();
    34         cout << x2 << "," << y2 << endl;
    35     }
    36 };
    37 
    38 void main()
    39 {
    40     Point<double>a(3.5, 8.8);
    41     a.display();
    42 
    43     Line<int>ab(4, 5, 6, 7);//全部使用整数
    44     ab.display();
    45 
    46     Line<double>ad(4.5, 5.5, 6.5, 7.5);//全部使用实数
    47     ad.display();
    48 
    49     system("pause");
    50 }

    例7.6

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

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <vector>
     4 #include <iterator>
     5 
     6 using namespace std;
     7 
     8 void main() 
     9 {
    10     double a[] = { 1.1,4.4,3.3,2.2 };
    11     vector<double>va(a, a + 4), vb(4);//定义并初始化向量va
    12     typedef vector<double>::iterator iterator;//自定义一个正向泛型指针标识符iterator
    13     iterator first = va.begin();//定义正向泛型指针first并指向va的首元素
    14 
    15     for (first; first < va.end(); first++)//循环正向输出va
    16     {
    17         cout << *first << " ";
    18     }
    19     for (--first; first > va.begin() - 1; first--)//循环逆向输出va
    20     {
    21         cout << *first << " ";
    22     }
    23     copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//整体正向输出va
    24     cout << endl;
    25 
    26     typedef vector<double>::reverse_iterator reverse_iterator;//自定义一个逆向泛型指针标识符
    27     reverse_iterator last = va.rbegin();//定义逆向泛型指针last并指向va的尾元素
    28 
    29     for (last; last < va.rend(); last++)//使用逆向指针循环从尾到首输出va
    30     {
    31         cout << *last << " ";
    32     }
    33     for (--last; last > va.rbegin() - 1; last--)//使用逆向指针循环从首到尾输出va
    34     {
    35         cout << *last << " ";
    36     }
    37     copy(va.rbegin(), va.rend(), ostream_iterator<double>(cout, " "));//整体从尾到首输出va
    38 
    39     system("pause");
    40 }

    例7.7

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

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <functional>
     4 #include <vector>
     5 #include <iterator>
     6 
     7 using namespace std;
     8 
     9 void main() 
    10 {
    11     double a[] = { 1.1,4.4,3.3,2.2 };
    12     vector<double>va(a, a + 4), vb(4);//定义实数向量va
    13     
    14     copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//正向输出va
    15     cout << endl;
    16 
    17     reverse_copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//逆向输出va
    18     cout << endl;
    19 
    20     reverse_copy(va.begin(), va.end(), vb.begin());//va逆向复制给vb
    21 
    22     copy(vb.begin(), vb.end(), ostream_iterator<double>(cout, " "));//正向输出vb
    23     cout << endl;
    24 
    25     sort(va.begin(), va.end());//va升幂排序
    26     sort(vb.begin(), vb.end(), greater<double>());//vb降幂排序
    27 
    28     copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//正向输出va
    29     cout << endl;
    30 
    31     copy(vb.begin(), vb.end(), ostream_iterator<double>(cout, " "));//逆向输出vb
    32     cout << endl;
    33 
    34     va.swap(vb);//交换va和vb的内容
    35 
    36     copy(va.begin(), va.end(), ostream_iterator<double>(cout, " "));//正向输出va
    37     cout << endl;
    38 
    39     copy(vb.begin(), vb.end(), ostream_iterator<double>(cout, " "));//正向输出vb
    40     cout << endl;
    41 
    42     cout << *find(va.begin(), va.end(), 4.4);//在va中查找4.4
    43 
    44     system("pause");
    45 }

    例7.8

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

     1 #include <iostream>
     2 #include <complex>
     3 #include <vector>
     4 
     5 using namespace std;
     6 
     7 struct st
     8 {
     9     int a, b;
    10 }a[] = { {2,5},{4,8} };
    11 
    12 void main() 
    13 {
    14     complex<float>num[] = { complex<float>(2,3),complex<float>(3.5,4.5) };
    15     vector<complex<float>*>vnum(2);//复数类的指针作为向量的数据类型
    16 
    17     vnum[0] = &num[0];
    18     vnum[1] = &num[1];
    19 
    20     for (int i = 0; i < 2; i++)
    21     {
    22         cout << "real is " << vnum[i]->real() << ",imag is" << vnum[i]->imag() << endl;
    23     }
    24 
    25     vector<st *>cp(2);//结构指针作为向量的数据类型
    26     cp[0] = &a[0];
    27     cp[1] = &a[1];
    28 
    29     for (int i = 0; i < 2; i++)
    30     {
    31         cout << "a=" << cp[i]->a << ",b=" << cp[i]->b << endl;
    32     }
    33 
    34     system("pause");
    35 }

    例7.11

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

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 #include <iterator>
     5 
     6 using namespace std;
     7 
     8 void main() 
     9 {
    10     char st[11] = "abcdefghij";
    11 
    12     vector<char>a(st, st + 10);//不复制标志""
    13     vector<char>::iterator p;//定义泛型指针p
    14 
    15     p = a.begin();//p指向第1个元素的指针
    16 
    17     a.insert(p + 3, 'X');//a[3]='X'
    18 
    19     copy(a.begin(), a.end(), ostream_iterator<char>(cout, " "));//输出向量a的内容
    20     cout << endl;
    21 
    22     p = a.begin();//p返回首位值
    23 
    24     a.insert(p, 3, 'A');//在a[0]前插入3个A
    25 
    26     copy(a.begin(), a.end(), ostream_iterator<char>(cout, " "));//输出增加A A A后的内容
    27     cout << endl;
    28 
    29     a.erase(p + 8);//删除a[8],即第9个元素e
    30 
    31     copy(a.begin(), a.end(), ostream_iterator<char>(cout, " "));//输出删除e之后的内容
    32     cout << endl;
    33 
    34     system("pause");
    35 };

    例7.12

    演示双向访问的例子。

     1 #include <iostream>
     2 #include <vector>
     3 
     4 using namespace std;
     5 
     6 void main() 
     7 {
     8     char st[11] = "abcdefghij";
     9 
    10     vector<char>a(st, st + 10);
    11     vector<char>::iterator p = a.begin();//定义正向泛型指针并初始化
    12     vector<char>::reverse_iterator ps;//定义逆向泛型指针
    13 
    14     for (p = a.begin(); p != a.end(); ++p)//正向访问
    15     {
    16         cout << *p << " ";//输出a b c d e f g h i j
    17     }
    18     cout << endl;
    19 
    20     for (p = a.end() - 1; p != a.begin() - 1; --p)//使用正向泛型指针逆向访问
    21     {
    22         cout << *p << " ";//输出j i h g f e d c b a
    23     }
    24     cout << endl;
    25 
    26     for (ps = a.rbegin(); ps != a.rend(); ++ps)//使用逆向泛型指针正向访问,使用++运算
    27     {
    28         cout << *ps << " ";//输出j i h g f e d c b a
    29     }
    30     cout << endl;
    31 
    32     for (--ps; ps != a.rbegin() - 1; --ps)//使用逆向泛型指针逆向访问,使用--运算
    33     {
    34         cout << *ps << " ";//输出a b c d e f g h i j
    35     }
    36     cout << endl;
    37 
    38     system("pause");
    39 };

    出圈游戏

     1 #include <iostream>
     2 #include <vector>
     3 
     4 using namespace std;
     5 
     6 class SeqList
     7 {
     8     char name[10];
     9 public:
    10     void DispName()
    11     {
    12         cout << name;
    13     }
    14     void SetName(char b[])
    15     {
    16         strcpy_s(name, b);
    17     }
    18     void Joseph(vector<SeqList>&);
    19 };
    20 //Joseph函数
    21 void SeqList::Joseph(vector<SeqList>&c)
    22 {
    23     int m, star, i, j, k;
    24 
    25     cout << "请输入间隔数m(m<=20)";
    26     cin >> m;//间隔数
    27     while (m > 20)//间隔数大于20,重新输入
    28     {
    29         cout << "间隔太大,请重新输入:";
    30         cin >> m;
    31     }
    32 
    33     cout << "从第几个人的位置开始报数(不能大于" << c.size() << "):";
    34     cin >> star;
    35     while (star > c.size())
    36     {
    37         cout << "开始位置大于人数,重新输入:";
    38         cin >> star;
    39     }
    40 
    41     cout << "准备输入名字" << endl;
    42     getchar();//消除回车干扰
    43     //输入参加游戏人的名字
    44     char s[10];
    45     for (i = 0; i < c.size(); i++)
    46     {
    47         cout << "" << i + 1 << "个人的名字:";
    48         gets_s(s);
    49         c[i].SetName(s);
    50     }
    51 
    52     i = star - 2;//为方便编程,从规定开始报数处再减1作为计数依据
    53     vector<SeqList>::iterator p;
    54     p = c.begin();
    55     int length = c.size();
    56     for (k = 1; k <= length; k++)
    57     {
    58         j = 0;//报数
    59         while (j < m)
    60         {
    61             i++;
    62             if (i == c.size())//到终点,返回第一个位置计数
    63             {
    64                 i = 0;
    65             }
    66             j++;
    67         }
    68         if (k == length)
    69         {
    70             break;
    71         }
    72         c[i].DispName();//输出出圈人的信息
    73         cout << ",";
    74         c.erase(p + i);//删除出圈人的记录
    75         --i;//调整计数位置初始值
    76     }
    77     //break语句跳转至此处,输出最后出列的编号
    78     c[i].DispName();
    79     cout << endl;
    80 }
    81 
    82 void main()
    83 {
    84     int length = 0;
    85 
    86     cout << "请输入人数:";
    87     cin >> length;
    88 
    89     vector<SeqList>c(length);
    90     SeqList game;
    91     game.Joseph(c);
    92 
    93     system("pause");
    94 };
  • 相关阅读:
    luogu P2685 [USACO07OPEN]抓牛Catch That Cow
    codevs 2021 中庸之道
    1018. 锤子剪刀布 (20)
    1017. A除以B (20)
    1016. 部分A+B (15)
    1013. 数素数 (20)
    1011. A+B和C (15)
    《C语言程序设计(第四版)》阅读心得(三)
    《C语言程序设计(第四版)》阅读心得(二)
    1008. 数组元素循环右移问题 (20)
  • 原文地址:https://www.cnblogs.com/denggelin/p/5592534.html
Copyright © 2011-2022 走看看