如何在 C++ 中使用模板函数

How to use template functions in C++

我目前正在编写一个使用模板计算 scalarProduct(点积)的程序,我 运行 遇到了两个问题。

一个是:如何声明模板并从 main 调用它。 两个是:如何将数组传递给函数。

感谢任何帮助,谢谢

// scalarProduct.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <math.h>       /* sqrt */

using namespace std;

template<typename T> 
void scalarProduct(T a[], T b[])
{
    T a[3];
    T b[3];

    this->a[] = a;
    this->b[] = b;

    T axb;
    T roota;
    T rootb;
    T result;

    axb = ((a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]));
    roota = sqrt((a[0] * a[0]) + (a[1] * a[1]) + (a[2] * a[2]));    // the formula for the Euclidean length of the vector A.
    rootb = sqrt((b[0] * b[0]) + (b[1] * b[1]) + (b[2] * b[2]));    // the formula for the Euclidean length of the vector B.
    result = axb / roota * rootb;

    cout << "Result: " << result;
};

int main()
{
    int a[3];
    int b[3];

    cout << "Enter A: " << endl;        // Vector A Input
    cout << "X:";
    cin >> a[0];
    cout << "Y:";
    cin >> a[1];
    cout << "Z:";
    cin >> a[2];

    cout << "Ennter B: " << endl;       // Vector B Input
    cout << "X:";
    cin >> b[0];
    cout << "Y:";
    cin >> b[1];
    cout << "Z:";
    cin >> b[2];

    scalarProduct(a[], b[]);


    system("pause");
    return 0;

}

题目模板部分:

声明和定义模板函数的格式如下:

template<compileTimeParameters>
returnType functionName(functionParameters) {
    ...;
}

并像这样调用它:

functionName<compileTimeParameters>(functionParameters);

例如,如果你想要一个用 cout 打印你给它的任何东西的函数,像这样:

template<typename T>
void println(T value) {
    std::cout << value << std::endl;
}

并像这样调用:

int main() {
    //string
    println<std::string>("yo yo yo print a newline");

    //integer
    println<int>(420);

    //some other random type
    println<myType>(someInstanceOfMyType);

}

如果编译器可以通过函数参数推导出模板参数,则您不必在 <> 括号内明确指定任何内容。例如:

int main() {
    //compiler deduces that you are passing a const char[]
    println("the compiler can figure out what I mean");
}

在 scalarProduct() 中去掉本地数组 a 和 b -- 您是从传递给函数的参数中获取它们的。还有,scalarProduct是一个函数,不是class的方法,所以没有"this"可以参考。

在 main() 中调用:

scalarProduct(a, b);

那应该可以了。

您的问题不在于模板,而在于 C++ 基础知识,例如将数组传递给函数。

// scalarProduct.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <math.h>       /* sqrt */

using namespace std;

template<typename T> 
void scalarProduct(T a[], T b[])
{
    /* 
    * remove this
    T a[3];
    T b[3];

    this->a[] = a;
    this->b[] = b;
    */

    T axb;
    T roota;
    T rootb;
    T result;

    axb = ((a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]));
    roota = sqrt((a[0] * a[0]) + (a[1] * a[1]) + (a[2] * a[2]));    // the formula for the Euclidean length of the vector A.
    rootb = sqrt((b[0] * b[0]) + (b[1] * b[1]) + (b[2] * b[2]));    // the formula for the Euclidean length of the vector B.
    result = axb / roota * rootb;

    cout << "Result: " << result;
};

int main()
{
    int a[3];
    int b[3];

    cout << "Enter A: " << endl;        // Vector A Input
    cout << "X:";
    cin >> a[0];
    cout << "Y:";
    cin >> a[1];
    cout << "Z:";
    cin >> a[2];

    cout << "Ennter B: " << endl;       // Vector B Input
    cout << "X:";
    cin >> b[0];
    cout << "Y:";
    cin >> b[1];
    cout << "Z:";
    cin >> b[2];

    //scalarProduct(a[], b[]);
    scalarProduct(a, b);

    system("pause");
    return 0;

}

此外,如果您处理三维点,我认为创建 struct Pointclass Point 是个好主意。如果你想让你的函数处理向量,那么使用指针或者更好 std::vector.