第2章 类模板:2.7 默认类模板实参

2.7 Default Class Template Arguments

2.7 默认类模板实参

 

As for function templates, you can define default values for class template parameters. For example, in class Stack<> you can define the container that is used to manage the elements as a second template parameter, using std::vector<> as the default value:

就像函数模板一样,你也可以为类模板参数定义默认值。比如,在类Stack<>里,你可以把用于管理元素的容器定义为第2个模板参数,并且使用std::vector<>作为它的默认值:

#include <vector>
#include <cassert>

template<typename T, typename Cont = std::vector<T>>
class Stack {
 private:
    Cont elems; // elements
 public:
    void push(T const& elem); // push element
    void pop(); // pop element
    T const& top() const; // return top element
    bool empty() const { // return whether the stack is
        emptyreturn elems.empty();
    }
};

template<typename T, typename Cont>
void Stack<T,Cont>::push (T const& elem)
{
    elems.push_back(elem); // append copy of passed elem
}

template<typename T, typename Cont>
void Stack<T,Cont>::pop ()
{
    assert(!elems.empty());
    elems.pop_back(); // remove last element
}

template<typename T, typename Cont>
T const& Stack<T,Cont>::top () const
{
    assert(!elems.empty());
    return elems.back(); // return copy of last element
}

Note that we now have two template parameters, so each definition of a member function must be defined with these two parameters:

可以看到:我们的类模板含有两个模板参数,因此每个成员函数的定义必须具有这两个参数:

template<typename T, typename Cont>
void Stack<T,Cont>::push (T const& elem)
{
    elems.push_back(elem); // append copy of passed elem
}

You can use this stack the same way it was used before. Thus, if you pass a first and only argument as an element type, a vector is used to manage the elements of this type:

你仍然可以像前面例子一样使用这个栈(stack)。就是说,如果你只传递每一个类型实参给这个类模板,那么将会利用vector来管理stack的元素:

template<typename T, typename Cont = std::vector<T>>
class Stack {
private:
    Cont elems; // elements
    …
};

In addition, you could specify the container for the elements when you declare a Stack object in your program:

另外,当在程序中声明Stack对象的时候,你还可以指定容器的类型:

#include "stack3.hpp"
#include <iostream>
#include <deque>

int main()
{
    // stack of ints:
    Stack< int> intStack;

    // stack of doubles:使用std::deque<>来管理元素
    Stack< double,std::deque< double>> dblStack;

    // 操作 int stack
    intStack.push(7);
    std::cout << intStack.top() << ’\n’;
    intStack.pop();

    // 操作 double stack
    dblStack.push(42.42);
    std::cout << dblStack.top() << ’\n’;
    dblStack.pop();
}

With Stack< double,std::deque< double>>

you declare a stack for doubles that uses a std::deque<> to manage the elements internally.

使用Stack< double,std::deque< double>>可以声明一个“元素类型为double类型,并且使用std::deque<>来管理内部元素”的栈。

原文链接: https://www.cnblogs.com/5iedu/p/12709151.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    第2章 类模板:2.7 默认类模板实参

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/392638

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月3日 下午2:54
下一篇 2023年4月3日 下午2:54

相关推荐