我已经编写了如下代码。我收到分段错误错误。调试器显示错误来自“some_order”。我检查了一个特定示例的变量值。取 n = 26:然后, Var = {0,...,25} 这意味着传递给 'some_order' 的 u 和 v 必须在 (0-25) 范围内,但我得到其中一个的一些大值,例如 7785654或-1549259(类似的东西)。我不明白为什么。分段错误是不可避免的。
//TNT: template numeric toolkit
#include "tnt.h"
//contains includes to all files in http://math.nist.gov/tnt/tnt_doxygen/files.html
//all other necessary stl and standard c++ libaray includes are there
class global_data{
public:
static TNT::Matrix<double>* Value_matrix;
};
TNT::Matrix<double>* global_data::Value_matrix = NULL;
bool some_order(const int& u ,const int& v) {
return (*global_Data::Value_matrix)[v][u] == 0.0;
}
void some_function(int n){
std::vector<int> Var(n);
for(int i=0; i<n; i++){
Var[i] = i;
}
std::sort(Var.begin(), Var.end(), some_order );
}
int main(){
//assume we have n;
//nxn matrix, initialised with 0.0
global_data::Value_matrix = new TNT::Matrix<double>(n,n,0.0) ;
//global_data::Value_matrix is then filled with values
some_function(n);
delete[] global_data::Value_matrix
}