Skip to content

Instantly share code, notes, and snippets.

@chomado
Last active August 29, 2015 14:04
Show Gist options
  • Save chomado/19d76fd56e9aee8a9aac to your computer and use it in GitHub Desktop.
Save chomado/19d76fd56e9aee8a9aac to your computer and use it in GitHub Desktop.
クラステンプレートについて
#ifndef ___Class_IntTwin
#define ___Class_IntTwin
#include <utility>
#include <algorithm>
class IntTwin
{
int v1;
int v2;
public:
IntTwin(int f=0, int s=0) : v1(f), v2(s) {} // コンストラクタ
int first() const { return v1; }
int& first() { return v1; }
int second() const { return v2; }
int& second() { return v2; }
void set(int f, int s) {v1 = f; v2 = s;}
int min() const { return v1 < v2 ? v1 : v2; }
bool ascending() const {return v1 < v2; }
void sort() { if (!(v1 < v2)) std::swap(v1, v2); }
};
#endif
#include <string>
#include <iostream>
#include "Twin.h"
using namespace std;
int main()
{
const Twin<int> t1(15, 37);
cout << "t1 = " << t1 << endl;
Twin<string> t2("ABC", "XYZ");
cout << "t2 = " << t2 << endl;
cin >> t2.first() >> t2.second(); // input: 34 21
if (!t2.ascending()) {
t2.sort();
cout << "t2: " << t2 << endl;
}
}
/* 実行結果
t1 = [15, 37]
t2 = [ABC, XYZ]
t2: [21, 34]
*/
#ifndef ___Class_Twin
#define ___Class_Twin
#include <utility>
#include <algorithm>
template <class Type> class Twin {
Type v1;
Type v2;
public:
Twin(const Type& f = Type(), const Type& s = Type()) : v1(f), v2(s) {}
Twin(const Twin<Type>& t) : v1(t.first()), v2(t.second()) {}
Type first() const { return v1; }
Type& first() { return v1; }
Type second() const { return v2; }
Type& second() { return v2; }
void set(const Type f, const Type& s) {v1 = f; v2 = s;}
Type min() const { return v1 < v2 ? v1 : v2; }
bool ascending() const {return v1 < v2; }
void sort() { if (!(v1 < v2)) std::swap(v1, v2); }
};
// 挿入子
template <class Type> inline std::ostream& operator<<(std::ostream& os, const Twin<Type>& t)
{
return os << "[" << t.first() << ", " << t.second() << "]";
}
#endif
#include <string>
#include <iostream>
#include "Twin.h"
using namespace std;
int main()
{
Twin<Twin<int> > t1(Twin<int>(36, 57), Twin<int>(23, 48));
cout << "t1 = " << t1 << endl;
Twin<Twin<string> > t2(Twin<string>("ABC", "XYZ"), Twin<string>("ABC", "ZZZ"));
cout << "t2 = " << t2 << endl;
cout << "t2の値を変更します" << endl;
cout << "新しい第1値の第1値" << endl; cin >> t2.first().first(); // GHI
cout << "新しい第1値の第2値" << endl; cin >> t2.first().second(); // XXX
cout << "新しい第2値の第1値" << endl; cin >> t2.second().first(); // C++
cout << "新しい第2値の第2値" << endl; cin >> t2.second().second(); // Flag
cout << "t2 = " << t2 << endl;
if (!t2.ascending()) {
cout << "第一値<第二値 が成立しませんのでソートします" << endl;
t2.sort();
cout << "t2は" << t2 << "に変更されました" << endl;
}
}
/* 実行結果
t1 = [[36, 57], [23, 48]]
t2 = [["ABC", "XYZ"], ["ABC", "ZZZ"]]
t2の値を変更します
新しい第1値の第1値
新しい第1値の第2値
新しい第2値の第1値
新しい第2値の第2値
t2 = [["GHI", "XXX"], ["C++", "Flag"]]
第一値<第二値 が成立しませんのでソートします
t2は[["C++", "Flag"], ["GHI", "XXX"]]に変更されました
0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment