Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1034834
  • 博文数量: 160
  • 博客积分: 4132
  • 博客等级: 中校
  • 技术积分: 2073
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-06 21:56
文章分类

全部博文(160)

文章存档

2012年(25)

2011年(120)

2010年(15)

分类: 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 (by argument deduction)
::max('a', 'b'); // calls max (by argument deduction)
::max(7, 42); // calls the nontemplate for two ints
::max<>(7, 42); // calls max (by argument deduction)
::max(7, 42); // calls max (no argument deduction)
::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 elems; // elements

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::push (T const& elem)
{
elems.push_back(elem); // append copy of passed elem
}

template
void Stack::pop ()
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::pop(): empty stack");
}
elems.pop_back(); // remove last element
}

template
T Stack::top () const
{
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 intStack; // stack of ints
Stack stringStack; // stack of strings

// 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 elems; // elements

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::push (std::string const& elem)
{
elems.push_back(elem); // append copy of passed elem
}

void Stack::pop ()
{
if (elems.empty()) {
throw std::out_of_range
("Stack::pop(): empty stack");
}
elems.pop_back(); // remove last element
}

std::string Stack::top () const
{
if (elems.empty()) {
throw std::out_of_range
("Stack::top(): empty stack");
}
return elems.back(); // return copy of last element
}

阅读(1433) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~