我已經編寫了類似於下面的代碼。我得到分段錯誤錯誤。調試器顯示錯誤來自'some_order'。我檢查了一個特定示例的變量值。取n = 26:然後,Var = {0,...,25}這意味着傳遞給'some_order'的u和v必須在範圍內(0-25),但是我得到一些很大的值,例如7785654或-1549259(像這樣的)。我不明白爲什麼。 Segementation故障是不可避免的。基於矩陣引起的排序對矢量進行排序時的分段錯誤
//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
}
你的排序函數應該返回兩個參數中的哪一個更大,並且它看起來什麼都不做。 – jcoder
你的'some_function'沒有什麼可觀察的。它創建一個矢量並對其進行分類,然後將其丟棄。 – TemplateRex
@jcoder:這是根據Value_matrix的值進行排序的不同類型 –