zoukankan      html  css  js  c++  java
  • 第2章 类模板:2.2 类模板Stack的使用

    2.2 Use of Class Template Stack

    2.2 类模板Stack的使用

    To use an object of a class template, until C++17 you must always specify the template arguments explicitly. The following example shows how to use the class template Stack<>:

    在C++17之前,为了使用类模板对象,你总是必须显式指定模板参数。下面的例子演示了如何使用类模板Stack<>:

    #include "stack1.hpp"
    #include <iostream>
    #include <string>
    
    int main()
    {
        Stack< int> intStack; // stack of ints
        Stack<std::string> stringStack; // stack of strings
    
        // 操作intStack
        intStack.push(7);
        std::cout << intStack.top() << ’
    ’;
    
        // 操作stringStack
        stringStack.push("hello");
        std::cout << stringStack.top() << ’
    ’;
        stringStack.pop();
    }

    By declaring type Stack<int>, int is used as type T inside the class template. Thus, intStack is created as an object that uses a vector of ints as elements and, for all member functions that are called, code for this type is instantiated. Similarly, by declaring and using Stack<std::string>, an object that uses a vector of strings as elements is created, and for all member functions that are called, code for this type is instantiated.

    通过声明类型Stack<int>,在类模板内部就可以用int实例化T。因此,intStack是一个创建自Stack<int>的对象,它的元素储存于vector,且类型为int。对于所有被调用的成员函数,都会实例化出基于int类型函数代码。类似,如果声明和使用Stack<std::string>,将会创建一个Stack<std::string>对象,它的元素储存于vector,且类型为std::string,而对于所有被调用的成员函数,也会实例化出基于std::string的函数代码。

    Note that code is instantiated only for template (member) functions that are called. For class templates, member functions are instantiated only if they are used. This, of course, saves time and space and allows use of class templates only partially, which we will discuss in Section 2.3 on page 29.

    注意,只有那些被调用的模板(成员)函数,才会产生这些函数的实例化代码。对于类模板,成员函数只有在被使用时才会被实例化。显然,这样可以节省时间和空间,并且允许部分使用类模板,我们将在第29页2.3节中加以讨论。

    In this example, the default constructor, push(), and top() are instantiated for both int and strings. However, pop() is instantiated only for strings. If a class template has static members, these are also instantiated once for each type for which the class template is used. An instantiated class template’s type can be used just like any other type. You can qualify it with const or volatile or derive array and reference types from it.

    在这个例子中,默认构造函数、push()和top()都被实例为int和string两个版本。而pop()仅被实例化了一个string版本。如果类模板中含有静态成员,那么对于使用该类模板的每种类型,这些成员也只会被实例化一次。你可以像使用其他任何类型一样地使用实例化后的类模板类型。可以用const或volatile限定,或者派生出数组或引用类型。

    You can also use it as part of a type definition with typedef or using (see Section 2.8 on page 38 for details about type definitions) or use it as a type parameter when building another template type. For example:

    你可以将其作为typedef或using类型定义的一部分(有类型定义的详细信息,请参阅第38页2.8节),或者在构造另一个模板类型时将其用作类型参数。例如:

    void foo(Stack <int> const& s) // 参数Stack<int>类型。
    {
        using IntStack = Stack <int>; // IntStack是Stack<int>的别名
        Stack< int> istack[10]; // istack 是个有 10 个元素的stacks<int>数组
        IntStack istack2[10]; // istack2与istack类型相同
        …
    }

    Template arguments may be any type, such as pointers to floats or even stacks of ints:

    模板参数可以是任意类型,比如浮点型指针,甚至是Stack<int>类型。

    Stack< float*> floatPtrStack; // stack of float pointers
    Stack<Stack< int>> intStackStack; // stack of stack of ints

    The only requirement is that any operation that is called is possible according to this type.

    唯一的要求就是:该类型必须提供被调用的所有操作。

    Note that before C++11 you had to put whitespace between the two closing template brackets:

    另外,在C++11之前,你需要在两个靠在一起的模板尖括号(即>)之间留一个空格:

    Stack<Stack< int> > intStackStack; // OK with all C++ versions

    If you didn’t do this, you were using operator >>, which resulted in a syntax error:

    如果你不这样做,它将变成使用operator>>,这会产生一个语法错误:

    Stack<Stack< int>> intStackStack; // ERROR before C++11

    The reason for the old behavior was that it helped the first pass of a C++ compiler to tokenize the source code independent of the semantics of the code. However, because the missing space was a typical bug, which required corresponding error messages, the semantics of the code more and more had to get taken into account anyway. So, with C++11 the rule to put a space between two closing template brackets was removed with the “angle bracket hack” (see Section 13.3.1 on page 226 for details).

    出现这种旧行为的原因是,它有助于帮助C++编译器首次通过独立的代码语义来标记源代码。但是由于缺少空格是一个典型的bug(需要相应的错误信息),所以越来越需要考虑代码的语义。因此,C++11使用“尖括号对付法”删除了在两个模板尖括号之间放置空格的规则。

  • 相关阅读:
    心理学安全威胁
    设计模式是在运用构造定律
    分形理论
    构造定律
    [SOA]REST与SOA两种架构的异同比较
    加法是自然之道
    ES : 软件工程学的复杂度理论及物理学解释
    软件架构的灵活设计
    软件复杂度与结构:(影响复杂度的因素)
    socket 的通信过程
  • 原文地址:https://www.cnblogs.com/5iedu/p/12708940.html
Copyright © 2011-2022 走看看