「第9章 テンプレート」Win32コンソールアプリケーション版
<岡本安晴「大学生のための心理学VC++プログラミング入門」>の第9章のサンプルプログラムのWin32版をVisual C++2008で作成した。プログラムファイルは圧縮ファイルとしてまとめた。圧縮ファイルはクリックしてダウンロードしたものをマウスの右ボタンでクリックして表示されるメニュから「解凍」あるいは「展開」を選んで解凍することができる。解凍したプログラムファイルはVisual C++2010でも開くことができる。
リスト9.1-1のWin32版
圧縮ファイルsample9_1_1win32.zip
===================================================
#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename T>
void qSort( T * d, int Lb, int Ub ){
        if (Lb < Ub){
                int p = (Lb + Ub) / 2;
                T
c = d[p];
                int i = Lb;
                int j = Ub;
                do {
                        while (d[i] < c) i++;
                        while (c < d[j]) j--;
                        if (i <= j){
                                T
v = d[i];
                                d[i]
= d[j];
                                d[j]
= v;
                                i++;
                                j--;
                        }
                }
while (i <= j);
                qSort(d,
Lb, j);
                qSort(d,
i, Ub);
        }
        return;
}
template <class T>
void showArray( T * d, int n ){
        for (int i = 0; i
< n; i++)
                cout
<< "\t" << d[i];
        cout
<< endl;
        return;
}
int _tmain(int argc, _TCHAR* argv[])
{
        double x[5] = { 1.1, 5.5, 2.2, 4.4, 3.3 };
        cout
<< "x =" << endl;
        showArray(
x, 5 );
        qSort(
x, 0, 4 );
        cout
<< "整列後, x = " << endl;
        showArray(
x, 5 );
        int y[5] = { 1, 5, 2, 4, 3 };
        cout
<< "y =" << endl;
        showArray<int>( y, 5 );
        qSort<int>( y, 0, 4 );
        cout
<< "整列後, y =" << endl;
        showArray<int>( y, 5 );
        cout
<< endl << "何か文字を入力して終了。" << endl;
        char ck;
        cin
>> ck;
        return 0;
}
リスト9.2-1のWin32版
圧縮ファイルsample9_2_1win32.zip
==============================================
#include "stdafx.h"
#include <iostream>
using namespace std;
template<typename T> class
DataArray{
        public:
                int n;
                T
* d;
                DataArray():
d(NULL), n(0) { }
                DataArray(
int vn ){
                        if (vn < 1){
                                d
= NULL;
                                n
= 0;
                        }else{
                                n
= vn;
                                d
= new T[n];
                        }
                }
                virtual ~DataArray(){
                        if (d != NULL) delete
[] d;
                }
                void ShowMem();
};
template<typename T>void
DataArray<T>::ShowMem(){
                if (d != NULL){
                        for (int i = 0; i
< n; i++)
                                cout
<< "\t" << d[i];
                        cout
<< endl;
                }
}
int _tmain(int argc, _TCHAR* argv[])
{
        int n = 3;
        DataArray<int> data(n);
        for (int i = 0; i
< data.n; i++)
                data.d[i]
= 1 + i;
        data.ShowMem();
        DataArray<char> cdata(n);
        for (int i = 0; i
< cdata.n; i++)
                cdata.d[i]
= char(int('a') + i);
        cdata.ShowMem();
        cout
<< endl << "何か文字を入力して終了。" << endl;
        char ck;
        cin
>> ck;
        return 0;
}