zoukankan      html  css  js  c++  java
  • 自考新教材-p341

    源程序:

    #include<iostream>
    #include<string>
    using namespace std;
    template<class T1, class T2>
    class Pair
    {
    public:
    T1 first;
    T2 second;
    Pair(T1 k, T2 v) :first(k), second(v) {}
    bool operator<(const Pair<T1, T2>&p)const;
    template <class T>
    void print(T x)
    {
    cout << x << endl;
    }
    };
    template <class T1, class T2>
    bool Pair<T1, T2>::operator <(const Pair<T1, T2>& p)const
    {
    return first<p.first;
    }
    int main()
    {
    Pair<string, int>student1("Tom", 19);
    Pair<string, int>student2("Jim", 21);
    Pair<int, int>coordinate(10, 20);
    Pair<string, string>dic("word", "单词");
    cout << "学生:" << student1.first << "" << student1.second << endl;
    cout << "学生:" << student2.first << "" << student2.second << endl;
    cout << "坐标:" << coordinate.first << "" << coordinate.second << endl;
    cout << "字典顶:" << dic.first << "" << dic.second << endl;
    bool a = student1<student2;
    if (a == 0) cout << student1.first << "位于" << student2.first << "之后" << endl;
    else cout << student1.first << "位于" << student2.first << "之前" << endl;
    student1.print<string>(student1.first);
    coordinate.print<int>(coordinate.first);
    system("pause");
    return 0;
    }

    运行结果:

  • 相关阅读:
    pandas isin 和not in
    游戏开发需要学什么?
    打开页面,数字会自增的效果怎么弄?
    jq 导航栏点击添加/删除类(a标签跳转页面)
    bootstrap+jq分页
    2020/12/18
    2020/12/17
    2020/12/16
    2020/12/15
    2020/12/14
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12266590.html
Copyright © 2011-2022 走看看