[计算机科学经

Hpp include iostream c templates the complete guide 6

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 和 42.7 都被轉型為 int: ::max('a', 42.7); // 本例㆗只有 non-template 函式才可以接受兩個不同型別的引數 ㆘面是㆒個更有用的例子,為指標型別和 C-style 字串型別重載了 max()template: // basics/max3.cpp #include <iostream> #include <cstring> #include <string> // 傳回兩個任意型別值的較大者 template <typename T> inline T const& max (T const& a, T const& b) { return a < b ? b : a; } // 傳回兩個指標的較大者(所謂較大是指「指標所指之物」較大) template <typename T> inline T* const& max (T* const& a, T* const& b) { return *a < *b ? b : a; } C++ Templates - The Complete Guide 2.4 重載(Overloading)Function Templates 17 // 傳回兩個 C-style 字串的較大者(譯註:C-style 字串必須自行定義何謂「較大」)。 inline char const* const& max (char const* const& a, char const* const& b) { return std::strcmp(a,b) < 0 ? b : a; } int main() { int a=7; int b=42; ::max(...
View Full Document

This note was uploaded on 08/25/2012 for the course EECS 101 taught by Professor Wre during the Fall '12 term at Beacon FL.

Ask a homework question - tutors are online