[C++]スラスラわかるC++ 関数テンプレート 10_3 cpp function Template Standard Template Library (STL)

関数テンプレートとクラステンプレートの構文

■関数テンプレート(Tはプレースホルダ)
template <Class T>T 関数名(T 引数名){
        Tをデータ型とした変数を使った処理;
}
template <class T1,class T1> T1 関数名(T1 引数名,T2 引数名){
        T1,T2をデータ型とした変数を使った処理;
}


■クラステンプレート(Tはプレースホルダ)
template <class Ty class クラス名{
       private:
       T メンバ変数名;
       public:
       Tメンバ関数名(T 引数名){
               Tをデータ型とした変数を使った処理;
       }
}

list10_3.cpp

#include <iostream>
using namespace std;

// 関数テンプレートで定義したgetMin関数
//template <class T> T 関数名(T 引数名)
template <class T> T getMin(T a, T b) {	
  //return a < b ? a : b;
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
//テンプレートをオーバーロード
// 関数テンプレートで定義したgetMin関数(配列の要素の最小値を返す)
//template <class T1,class T1> T1 関数名(T1 引数名,T2 引数名)
template <class T1, class T2> T1 getMin(T1 a[], T2 length) {
  T1 ans = a[0];
  for (T2 i = 1; i < length; i++) {
    if (ans > a[i]) {
      ans = a[i];
    }
  }
  return ans;
}

// main関数
int main() {
  // int型のgetMin関数を呼び出す
  int a, b, c;
  a = 123;
  b = 456;
  c = getMin(a, b);
  cout << c << endl;

  // double型のgetMin関数を呼び出す
  double x, y, z;
  x = 1.23;
  y = 4.56;
  z = getMin(x, y);
  cout << z << endl;

  // char型のgetMin関数(配列用)を呼び出す
  char cArray[] = { 'c', 'b', 'f', 'a', 'e' };
  int cLength = 5;
  char cAns = getMin(cArray, cLength);
  cout << cAns << endl;

  // float型のgetMin関数(配列用)を呼び出す
  float fArray[] = { 2.22F, 3.33F, 1.11F, 5.55F, 4.44F };
  int fLength = 5;
  float fAns = getMin(fArray, fLength);
  cout << fAns << endl;

  return 0;
}

cd_samples10_3_template.cmd


C:\samples\chapter10>cd C:\samples\chapter10\

C:\samples\chapter10>g++ -o list10_3.exe list10_3.cpp

C:\samples\chapter10>list10_3.exe
123
1.23
a
1.11

C:\samples\chapter10>cmd /k
C:\samples\chapter10>

[C++]スラスラわかるC++ クラステンプレート 10_4 cpp class Template Standard Template Library (STL)

■クラステンプレート(Tはプレースホルダ)
template <class Ty class クラス名{
       private:
       T メンバ変数名;
       public:
       Tメンバ関数名(T 引数名){
               Tをデータ型とした変数を使った処理;
       }
}

list10_4.cpp

#include <iostream>
using namespace std;

// MyTriangleクラスの定義と実装
template <class T> class MyTriangle {
  private:
    T bottom;	// 底辺
    T height;	// 高さ
  public:
    // コンストラクタ
    MyTriangle(T bottom, T height) {
      this->bottom = bottom;
      this->height = height;
    }

    // 面積を返すメンバ関数
    T getArea() {
      return this->bottom * this->height / 2;
    }
};

// main関数
int main() {
  // int型でMyTriangleクラスをインスタンス化する際にコンストラクタ引数として30、40渡す
  MyTriangle<int> iObj(10, 20);
  //クラスメソッドgetArea()を呼ぶ
  cout << "int型の三角形の面積:" << iObj.getArea() << endl;

  // double型でMyTriangleクラスををインスタンス化する際にコンストラクタ引数として30、40渡す
  MyTriangle<double> dObj(30.0, 40.0);
  //クラスメソッドgetArea()を呼ぶ
  cout << "double型の三角形の面積:" << dObj.getArea() << endl;

  return 0;
}

cd_samples10_4.cmd


C:\samples\chapter10>cd C:\samples\chapter10\

C:\samples\chapter10>g++ -o list10_4.exe list10_4.cpp

C:\samples\chapter10>list10_4.exe
int型の三角形の面積:100
double型の三角形の面積:600

C:\samples\chapter10>cmd /k
C:\samples\chapter10>

[C++]江添亮さんのC++入門を動かしてみた。コンパイル済みヘッダー(precompiled header)

すとんりぱーさんのすすめてた

https://ezoeryou.github.io/cpp-intro

江添亮さんのC++入門を動かしてみた。コンパイル済みヘッダー(precompiled header)

all.h

#include <cstddef>
#include <limits>
#include <climits>
#include <cfloat>
#include <cstdint>
#include <cstdlib>
#include <new>
#include <typeinfo>
#include <exception>
#include <initializer_list>
#include <cstdalign>
#include <stdexcept>
#include <cassert>
#include <cerrno>
#include <system_error>
#include <string>

#if __has_include(<string_view>)
#   include <string_view>
#endif

#include <array>
#include <deque>
#include <forward_list>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <iterator>
#include <algorithm>
#include <cfenv>
#include <random>
#include <numeric>
#include <cmath>
#include <iosfwd>
#include <iostream>
#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>
#include <iomanip>
#include <sstream>
#include <fstream>

#if __has_include(<filesystem>)
#   include <filesystem>
#endif

#include <cstdio>
#include <cinttypes>


#include <regex>
#include <atomic>
#include <thread>
#include <mutex>
#include <shared_mutex>
#include <condition_variable>
#include <future>

using namespace std::literals ;

main.cpp

#include <iostream>

int main()
{
    std::cout << "hello C++ World" ;
}

cd_use_compiled_header.cmd    timeでの時間計測は自分の環境ではうまくいかない。

cd %~dp0
dir
g++ -std=c++17 -Wall --pedantic-errors -x c++-header -o all.h.gch all.h
dir
g++ -std=c++17 -Wall --pedantic-errors -include all.h -o program.exe main.cpp

program.exe

cmd /k

結果


C:\samples\ehararyu_cpp_sample\cpp>cd C:\samples\ehararyu_cpp_sample\cpp\

C:\samples\ehararyu_cpp_sample\cpp>dir
 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は 9699-BA6D です

 C:\samples\ehararyu_cpp_sample\cpp のディレクトリ

2024/09/22  05:45    <DIR>          .
2024/09/22  04:37    <DIR>          ..
2024/09/22  04:51             1,217 all.h
2024/09/22  05:14               195 cd_use_compiled_header.cmd
2024/09/22  04:41                79 main.cpp
2024/09/22  05:14    <DIR>          step1
2024/09/22  05:14    <DIR>          step2
2024/09/22  05:45    <DIR>          step3
               3 個のファイル               1,491 バイト
               5 個のディレクトリ  3,792,160,215,040 バイトの空き領域

C:\samples\ehararyu_cpp_sample\cpp>g++ -std=c++17 -Wall --pedantic-errors -x c++-header -o all.h.gch all.h

C:\samples\ehararyu_cpp_sample\cpp>dir
 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は 9699-BA6D です

 C:\samples\ehararyu_cpp_sample\cpp のディレクトリ

2024/09/22  05:47    <DIR>          .
2024/09/22  04:37    <DIR>          ..
2024/09/22  04:51             1,217 all.h
2024/09/22  05:47        45,898,292 all.h.gch
2024/09/22  05:14               195 cd_use_compiled_header.cmd
2024/09/22  04:41                79 main.cpp
2024/09/22  05:14    <DIR>          step1
2024/09/22  05:14    <DIR>          step2
2024/09/22  05:45    <DIR>          step3
               4 個のファイル          45,899,783 バイト
               5 個のディレクトリ  3,792,114,200,576 バイトの空き領域

C:\samples\ehararyu_cpp_sample\cpp>g++ -std=c++17 -Wall --pedantic-errors -include all.h -o program.exe main.cpp

C:\samples\ehararyu_cpp_sample\cpp>program.exe
hello C++ World
C:\samples\ehararyu_cpp_sample\cpp>cmd /k
C:\samples\ehararyu_cpp_sample\cpp>




[C++] 江添亮さんのC++Windowsで入門する方法(MinGW)(64bit Windows11) Make編 

https://ezoeryou.github.io/cpp-intro

に基づいて書いてます。

MinGWに付属する GNU make ですが、ファイル名が mingw32-make.exe となっていると思います。 このままでは使いにくいのでファイルをコピーして make.exe にリネームした方がよいでしょう。

windowsで、timeコマンド touchコマンドをつかうには gitbashが必要だ

(1) https://gitforwindows.org/ にアクセスしてください。

(2) [Download]をクリックしてください。

あとは適当にインストールで。C:\Program Files\Git\git-bash.exe

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$ make
makefile:2: *** missing separator. Stop.

はmakefile の改行のあとが space だから Tab にする。

program : source.cpp
    cat source.cpp > program

source : source01 source02 source03
    cat source01 source02 source03 > source

program : source
	cat source > program

source : source01 source02 source03
	cat source01 source02 source03 > source

うまくいった。

GitBash実行結果


furcr@furcraea_built MINGW64 /
$ cd C:/samples/ehararyu_cpp_sample/cpp

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp
$ cd source010203

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$ make
makefile:2: *** missing separator.  Stop.

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$ ^C

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$ make
cat source01 source02 source03 > source
cat source > program

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$ make
make: 'program' is up to date.

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$ make
cat source > program

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$ make
make: 'program' is up to date.

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$ make
make: 'program' is up to date.

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$ program
bash: program: command not found

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/source010203
$




Makefile

D : A B C
	cat A B C > D

A : a
	cat a > A

B : b
	cat b > B

C : c
	cat c > C

GitBash

furcr@furcraea_built MINGW64 ~
$ cd C:\samples\ehararyu_cpp_sample\cpp~
bash: cd: C:samplesehararyu_cpp_samplecpp~: No such file or directory

furcr@furcraea_built MINGW64 ~
$ cd C:\samples\ehararyu_cpp_sample\cpp
bash: cd: C:samplesehararyu_cpp_samplecpp: No such file or directory

furcr@furcraea_built MINGW64 ~
$ cd C:/samples/ehararyu_cpp_sample/cpp

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp
$ touch source01 source02 source03

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp
$ make
makefile:2: *** missing separator.  Stop.

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp
$ make
makefile:2: *** missing separator.  Stop.

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp
$ make
makefile:2: *** missing separator.  Stop.

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp
$ cd abc

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$ touch a b c

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$ make
Makefile:2: *** missing separator.  Stop.

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$ make
cat A B C > D

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$ touch b

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$ make
cat A B C > D

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$ time make
make: 'D' is up to date.

real    0m0.080s
user    0m0.015s
sys     0m0.000s

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$ ^C

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$

furcr@furcraea_built MINGW64 /c/samples/ehararyu_cpp_sample/cpp/abc
$