• 自考新教材-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;
    }

    运行结果:

  • 相关阅读:
    LINX中的各种alloc
    Enea LINX代码分析之二(ECM_RX)
    Enea LINX代码分析之一
    看代码和写代码还是很不同的
    sctp bind
    sockaddr和sockaddr_in
    pthread条件变量
    System V IPV & Posix IPC(摘自Unix网络编程卷2)
    [宽度优先搜索] FZU-2150 Fire Game
    [字符哈希] POJ 3094 Quicksum
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12266590.html
Copyright © 2020-2023  润新知