我试图在C ++中实现mergesort树,但我收到了错误
Traceback (most recent call last):
File "C:/Users/L31101/PycharmProjects/FYP/Trial&Error.py", line 24, in <module> first_list[k] = i
IndexError: list assignment index out of range
Process finished with exit code 1
以下是代码:
note: template argument deduction/substitution failed:
C:\Users\IT Services\Documents\mergetree.cpp:15:38: note: candidate expects 6 arguments, 2 provided
tree[k]=merge(tree[2*k],tree[2*k+1]);
显然,合并功能不需要6个参数。为什么编译器认为它呢?
[另外,请忽略编写代码的可怕方式。我正在为即将到来的比赛进行练习,目前还没有优先考虑清晰的编码惯例。]
答案 0 :(得分:1)
标准算法库中有一个std :: merge API(http://en.cppreference.com/w/cpp/algorithm/merge)。它的味道有6个参数。第15行的合并调用对于其中一个函数来说可能是giong。
您可以通过检查错误说明后列出的候选人来确保这一点。
您应该在计划使用它的行之前声明您的合并方法。而不是using namespace std;
,而是在您想要使用std方法的地方明确地解析范围。在第15行,将merge调用为:: merge。如果您只是希望您的合并具有文件范围,请将其放在未命名的命名空间中。