📄 mintest.cpp
字号:
//: C05:MinTest.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include <cstring>
#include <iostream>
using std::strcmp;
using std::cout;
using std::endl;
template<typename T> const T& min(const T& a, const T& b) {
return (a < b) ? a : b;
}
const char* min(const char* a, const char* b) {
return (strcmp(a, b) < 0) ? a : b;
}
double min(double x, double y) {
return (x < y) ? x : y;
}
int main() {
const char *s2 = "say \"Ni-!\"", *s1 = "knights who";
cout << min(1, 2) << endl; // 1: 1 (template)
cout << min(1.0, 2.0) << endl; // 2: 1 (double)
cout << min(1, 2.0) << endl; // 3: 1 (double)
cout << min(s1, s2) << endl; // 4: knights who (const
// char*)
cout << min<>(s1, s2) << endl; // 5: say "Ni-!"
// (template)
} ///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -