gpt4 book ai didi

c++ - 在 C++ 中调用模板

转载 作者:行者123 更新时间:2023-11-28 03:31:00 25 4
gpt4 key购买 nike

首先,我必须说我是 C++ 的第一步,我来自 PHP,所以请原谅这个基本问题。我一直在寻找和测试许多替代方案,但我似乎无法理解如何执行此操作,在 PHP 中类似于 distance($str1, $str2)。为了阅读任何功能 distance返回。

我想做的是调用一个模板,该模板应返回 2 个字符串之间的 Levenshtein 距离。

这是我的代码

template <class T> unsigned int edit_distance(const T& s1, const T& s2)
{
const size_t len1 = s1.size(), len2 = s2.size();
vector<vector<unsigned int> > d(len1 + 1, vector<unsigned int>(len2 + 1));

d[0][0] = 0;
for(unsigned int i = 1; i <= len1; ++i) d[i][0] = i;
for(unsigned int i = 1; i <= len2; ++i) d[0][i] = i;

for(unsigned int i = 1; i <= len1; ++i)
for(unsigned int j = 1; j <= len2; ++j)

d[i][j] = std::min( std::min(d[i - 1][j] + 1,d[i][j - 1] + 1),
d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1) );
return d[len1][len2];
}

string text1="house";
string text2="houses";
edit_distance <int> (text1, text2);

问题在线edit_distance <int> (text1, text2); Xcode 给出的错误是:C++ requires a type specifier for all declarations

同样,我知道这一定很基础,但我找不到正确的方法。任何正确方向的提示都将不胜感激。

谢谢!

最佳答案

为了测试我的怀疑,我采用了你的代码,添加了最少的包含和使用声明以允许编译器调用 edit_distance。并编译它。这是我编译的文件的完整内容:

#include <vector>
#include <string>
#include <algorithm>
using std::vector;
using std::string;

template <class T> unsigned int edit_distance(const T& s1, const T& s2)
{
const size_t len1 = s1.size(), len2 = s2.size();
vector<vector<unsigned int> > d(len1 + 1, vector<unsigned int>(len2 + 1));

d[0][0] = 0;
for(unsigned int i = 1; i <= len1; ++i) d[i][0] = i;
for(unsigned int i = 1; i <= len2; ++i) d[0][i] = i;

for(unsigned int i = 1; i <= len1; ++i)
for(unsigned int j = 1; j <= len2; ++j)

d[i][j] = std::min( std::min(d[i - 1][j] + 1,d[i][j - 1] + 1),
d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1) );
return d[len1][len2];
}

string text1="house";
string text2="houses";
edit_distance <int> (text1, text2);

我的怀疑被证实了;我收到以下错误:

main.cpp:26:22: error: C++ requires a type specifier for all declarations
edit_distance <int> (text1, text2);
^~~~~

这正是您提到的错误,因此我相信您很可能犯了与我在测试源代码中包含的错误相同的错误。大多数有经验的程序员会假设您的不完整代码是为了简洁而显示的,并且会自动插入一些可以完全避免此错误的周围代码。


您没有理解的事情似乎是,在 C++ 中代码不会随处可见。 C++ 程序不仅仅是解释器在源代码中读取它们时执行的一系列语句。

C++ 是一种编译语言。该程序是一系列函数、类等的声明和定义。语句,即做事的代码,只在定义内。你的函数调用 edit_distance<int>(text1, text2);不在任何定义中,因此编译器不期望有语句,也不知道你在说什么。所以把它放在一个定义里。您可能想使用函数定义:

void foo() {
string text1="house";
string text2="houses";
edit_distance <int> (text1, text2);
}

此外,当执行编译后的代码时,执行从一个特殊的入口点函数 main 开始。只有通过此入口点可访问的代码才会被执行。 (实际上这不是真的,但其他情况目前无关紧要。)

因此,您需要一个最终导致您要执行的代码的主函数。你可以有 main调用以上 foo ,或者您可以直接将代码放在 main 中:

int main() {
string text1="house";
string text2="houses";
edit_distance <int> (text1, text2);
}

这段代码仍然无法编译,因为 <int>部分不正确。尖括号内的类型成为分配给 T 的类型在 edit_distance 函数模板中:

template <class T> unsigned int edit_distance(const T& s1, const T& s2)

edit_distance<int>说设置 T 等于 int:

unsigned int edit_distance(const int& s1, const int& s2)

这不是您想要的。您可能希望将 T 设置为等于字符串,以便 s1 和 s2 参数是字符串。

int main() {
string text1="house";
string text2="houses";
edit_distance<string>(text1,text2);
}

接下来您可能想要实际查看返回值。添加#include <iostream>在源代码的开头加上你的其他包含,然后使用 main 函数:

int main() {
string text1="house";
string text2="houses";
std::cout << edit_distance<string>(text1,text2) << '\n';
}

Complete, compilable program

关于c++ - 在 C++ 中调用模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12655730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com