扩展阅读
当前位置: 编程语言>c/c++
c++模板(template)常见用法代码实例
发布时间:2013-10-22
本文导语: C++编程语言中的模板应用在一定程度上大大提高了程序开发的效率。我们在这篇文章中为大家详细讲解一下有关C++模板的基本概念,希望初学者们可以通过本文介绍的内容充分掌握这方面的知识。前段时间重新学习C++...
c++编程语言中的模板应用在一定程度上大大提高了程序开发的效率。我们在这篇文章中为大家详细讲解一下有关c++模板的基本概念,希望初学者们可以通过本文介绍的内容充分掌握这方面的知识。前段时间重新学习c++,主要看c++编程思想和c++设计新思维。对模版的使用有了更进一层的了解,特总结如下:
一、 函数模板
1. 定义模板
template <typename T>
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 <iostream>
#include <string>
#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 <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
// maximum of three values of any type
template <typename T>
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<double> (by argument deduction)
::max('a', 'b'); // calls max<char> (by argument deduction)
::max(7, 42); // calls the nontemplate for two ints
::max<>(7, 42); // calls max<int> (by argument deduction)
::max<double>(7, 42); // calls max<double> (no argument deduction)
::max('a', 42.7); // calls the nontemplate for two ints
}#include <iostream>
#include <cstring>
#include <string>
// maximum of two values of any type
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
// maximum of two pointers
template <typename T>
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 <vector>
#include <stdexcept>
template <typename T>
class Stack {
private:
std::vector<T> 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 <typename T>
void Stack<T>::push (T const& elem)
{
elems.push_back(elem); // append copy of passed elem
}
template<typename T>
void Stack<T>::pop ()
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::pop(): empty stack");
}
elems.pop_back(); // remove last element
}
template <typename T>
T Stack<T>::top () const
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::top(): empty stack");
}
return elems.back(); // return copy of last element
}2. 使用模板
#include <iostream>
#include <string>
#include <cstdlib>
#include "stack1.hpp"
int main()
{
try {
Stack<int> intStack; // stack of ints
Stack<std::string> 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 <deque>
#include <string>
#include <stdexcept>
#include "stack1.hpp"
template<>
class Stack<std::string> {
private:
std::deque<std::string> 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<std::string>::push (std::string const& elem)
{
elems.push_back(elem); // append copy of passed elem
}
void Stack<std::string>::pop ()
{
if (elems.empty()) {
throw std::out_of_range
("Stack<std::string>::pop(): empty stack");
}
elems.pop_back(); // remove last element
}
std::string Stack<std::string>::top () const
{
if (elems.empty()) {
throw std::out_of_range
("Stack<std::string>::top(): empty stack");
}
return elems.back(); // return copy of last element
}1. C++模板类静态成员
template < typename T> struct testClass
{
static int _data;
};
template< > int testClass< char>::_data = 1;
template< > int testClass< long>::_data = 2;
int main( void ) {
cout < < boolalpha < < (1==testClass< char>::_data) < < endl;
cout < < boolalpha < < (2==testClass< long>::_data) < < endl;
}2. C++模板类偏特化
template < class I, class O> struct testClass
{
testClass() { cout < < "I, O" < < endl; }
};
template < class T> struct testClass< T*, T*>
{
testClass() { cout < < "T*, T*" < < endl; }
};
template < class T> struct testClass< const T*, T*>
{
testClass() { cout < < "const T*, T*" < < endl; }
};
int main( void )
{
testClass< int, char> obj1;
testClass< int*, int*> obj2;
testClass< const int*, int*> obj3;
}3.类模版+函数模版
template < class T> struct testClass
{
void swap( testClass< T>& ) { cout < < "swap()" < < endl; }
};
template < class T> inline void swap( testClass< T>& x,
testClass< T>& y )
{
x.swap( y );
}
int main( void )
{
testClass< int> obj1;
testClass< int> obj2;
swap( obj1, obj2 );
}4. 类成员函数模板
struct testClass
{
template < class T> void mfun( const T& t )
{
cout < < t < < endl;
}
template < class T> operator T()
{
return T();
}
};
int main( void )
{
testClass obj;
obj.mfun( 1 );
int i = obj;
cout < < i < < endl;
}5. 缺省C++模板参数推导
template < class T> struct test
{
T a;
};
template < class I, class O=test< I> > struct testClass
{
I b;
O c;
};
void main()
{
}6. 非类型C++模板参数
template < class T, int n> struct testClass {
T _t;
testClass() : _t(n) {
}
};
int main( void ) {
testClass< int,1> obj1;
testClass< int,2> obj2;
}7. 空模板参数
template < class T> struct testClass;
template < class T> bool operator==( const testClass< T>&,
const testClass< T>& )
{
return false;
};
template < class T> struct testClass
{
friend bool operator== < >
( const testClass&, const testClass& );
};
void main()
{
}8. template template 类
struct Widget1
{
template< typename T>
T foo(){}
};
template< template< class T>class X>
struct Widget2
{
};
void main()
{
cout< < 3 < < 'n';
}以上就是对C++模板的一些常用方法的介绍。
c++模板(Template)介绍及模板参数中包含模版 C++模板库 C++ B-tree - 移动开发
iis7站长之家
C++元模板语言 Metacza C++ STL标准模板库类String成员详细列表参考及示例代码 C++反射模板库 Template Reflection Library c++通用模板类(template class)定义实现详细介绍 哪里有C++标准库(包含模板库)的文档可以下载? c++模板(template)中的class和typename关键字异同比较 常用 C++ 数学模板定义 libefgy C++模板库 libsigc++ C++模板库 Standard Portable Library uClinux下如何才能支持标准C++类库,如(string, list)等模板库 线性算术的C++模板库 Eigen C++模板特例化应用实例 C++类模板与模板类深入详解 C++函数模板与类模板实例解析 C++中函数模板的用法详细解析 C++可变参数的函数与模板实例分析 C++模板之特化与偏特化详解