開発環境
- OS X Mavericks - Apple, ときどき
Windows 8.1 + Cygwin64, MinGW (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅤ部(言語のその他の機能)の24章(テンプレート)、24.10(プログラミング実習)、実習 24-1.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 24-1.
コード(BBEdit, Emacs)
test_min.cpp
#include <iostream>
#include <cstring>
template<typename kind>
kind min(kind d1, kind d2)
{
if (d1 < d2)
return (d1);
return (d2);
}
const char *const min(const char *const d1, const char *const d2)
{
if (std::strcmp(d1, d2) < 0)
return (d1);
return (d2);
}
int main(int argc, char *argv[])
{
std::cout << "min(1, 2) " << min(1, 2) << std::endl;
std::cout << "min(2, 1) " << min(2, 1) << std::endl;
std::cout << "max(\"able\", \"baker\") " << min("able", "baker") << std::endl;
std::cout << "max(\"baker\", \"able\") " << min("baker", "able") << std::endl;
return (0);
}
Makefile
CC=g++
CFLAGS=-g -Wall
SRC=test_min.cpp
OBJ=test_min.o
all: test_min
test_min: $(OBJ)
${CC} ${CFLAGS} -o test_min $(OBJ)
test_min.o: min.h test_min.cpp
${CC} $(CFLAGS) -c test_min.cpp
clean:
rm test_min test_min.o
入出力結果(Terminal)
$ make
g++ -g -Wall -c test_min.cpp
g++ -g -Wall -o test_min test_min.o
bash-4.3$ ./test_min
min(1, 2) 1
min(2, 1) 1
max("able", "baker") able
max("baker", "able") able
$
0 コメント:
コメントを投稿