分类: C/C++
2012-05-20 12:09:19
用实例说明C++模板用法
――实例引自《C++ Templates》
一、 函数模板
1. 定义模板
template
inline T const& max (T const& a, T const&
b)
{
// if a < b then use b else use a
return a < b ? b :
a;
}
2. 使用模板
#include
#include
#include "max.hpp"
int main()
{
int i =
42;
std::cout << "max(7,i): " << ::max(7,i) <<
std::endl;
double f1 = 3.4;
double f2 = -6.7;
std::cout <<
"max(f1,f2): " << ::max(f1,f2) << std::endl;
std::string s1 = "mathematics";
std::string s2 = "math";
std::cout << "max(s1,s2): " << ::max(s1,s2) <<
std::endl;
}
3. 重载函数模板
实例一:
// maximum of two int values
inline
int const& max (int const& a, int const& b)
{
return a
< b ? b : a;
}
// maximum of two values of any type
template
inline
T const& max (T const& a, T const& b)
{
return a < b ?
b : a;
}
// maximum of three values of any type
template
inline T const& max (T const& a, T const& b, T const&
c)
{
return ::max (::max(a,b), c);
}
int main()
{
::max(7, 42, 68); // calls the template for three
arguments
::max(7.0, 42.0); // calls max
::max('a', 'b'); // calls max
::max(7, 42); // calls the nontemplate for two
ints
::max<>(7, 42); // calls max
::max
::max('a', 42.7); // calls the nontemplate for
two ints
}
实例二:为指针和普通的C字符串重载
#include
#include
#include
// maximum of two values of any type
template
inline
T const& max (T const& a, T const& b)
{
return a < b ?
b : a;
}
// maximum of two pointers
template
inline T*
const& max (T* const& a, T* const& b)
{
return *a < *b
? b : a;
}
// maximum of two C-strings
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(a,b); //
max() for two values of type int
std::string s="hey";
std::string t="you";
::max(s,t);
// max() for two values of type std::string
int* p1 = &b;
int* p2 = &a;
::max(p1,p2); //
max() for two pointers
char const* s1 = "David";
char const* s2 = "Nico";
::max(s1,s2); // max() for two C-strings
}
二、 类模板
1. 定义模板
#include
#include
template
class Stack {
private:
std::vector
public:
void push(T const&); // push element
void
pop(); // pop element
T top() const; // return
top element
bool empty() const { // return whether the stack is
empty
return elems.empty();
}
};
template
void Stack
{
elems.push_back(elem); // append copy of passed
elem
}
template
void Stack
{
if
(elems.empty()) {
throw std::out_of_range("Stack<>::pop():
empty stack");
}
elems.pop_back(); // remove last
element
}
template
T Stack
{
if
(elems.empty()) {
throw std::out_of_range("Stack<>::top():
empty stack");
}
return elems.back(); // return copy of last
element
}
2. 使用模板
#include
#include
#include
#include "stack1.hpp"
int main()
{
try {
Stack
Stack
// manipulate int stack
intStack.push(7);
std::cout << intStack.top() << std::endl;
// manipulate string stack
stringStack.push("hello");
std::cout << stringStack.top()
<< std::endl;
stringStack.pop();
stringStack.pop();
}
catch (std::exception const& ex)
{
std::cerr << "Exception: " << ex.what() <<
std::endl;
return EXIT_FAILURE; // exit program with ERROR
status
}
}
3. 类模板的特化
#include
#include
#include
#include "stack1.hpp"
template<>
class Stack
private:
std::deque
public:
void push(std::string const&); // push element
void
pop(); // pop element
std::string top() const;
// return top element
bool empty() const { // return whether
the stack is empty
return elems.empty();
}
};
void Stack
{
elems.push_back(elem); // append copy of passed elem
}
void Stack
{
if (elems.empty())
{
throw std::out_of_range
("Stack
}
elems.pop_back(); // remove last element
}
std::string Stack
{
if
(elems.empty()) {
throw std::out_of_range
("Stack
}
return
elems.back(); // return copy of last element
}
三