2013-04-23 83 views
1

我是C++新手,正在編寫一組子集程序,它接受用戶定義的一組數字,其中第一個數字被認爲是總數。我曾嘗試使用DDD來調試此程序,但是我仍然遇到了出界錯誤。我似乎無法找出發生這種情況的原因。任何線索?謝謝。 以下是錯誤:子集總和程序

terminate called after throwing an instance of 'std::out_of_range' 
what(): vector::_M_range_check 

代碼:

#include <iostream> 
#include <vector> 
#include <cassert> 
#include <iomanip> 
#include <climits> 
#include <math.h> 
#include <algorithm> 

typedef unsigned int uint; 
using namespace std; 

//////// Function declerations ////////// 
void sum_of_subsets(uint index, 
        uint weight, 
        uint total, 
        vector<bool> &include, 
        vector<uint> &w, 
        uint W); 

bool promising (uint index, 
       uint weight, 
       uint W, 
       vector<uint> w, 
       uint total); 

/////////////// Main ////////////////// 
int main() 
{ 
    //string sortingCode = "-as"; 
    vector<uint> w;    // vector of weights 
    vector<bool> include;  
     uint W;      // the total 
    uint index = 0; 
    uint weight = 0;   // the current weight of subsets 
    uint total = 0;    // the superset total weight 
    while(! cin.eof()) 
    { 
    uint value; 
    if(cin >> value && ! cin.eof()) 
     w.push_back(value); 
    } 

    W = w.front(); 
    w.erase(w.begin()); 
    // instantiate the include vector to false 
    for(uint k = 0; k <= w.size(); k++) 
     include.push_back(0); 
    // calculate the superset total 
    for(uint k = 0; k <= w.size()-1; k++) 
     total += w.at(k); 
    // calculate the sum of subsets accordig to CL argument 
    sum_of_subsets(index, weight, total, include, w, W); 

    // report success 
    return 0; 
}  

////////// Function Bodies /////////// 
void sum_of_subsets(uint index, 
        uint weight, 
        uint total, 
        vector<bool> &include, 
        vector<uint> &w, 
        uint W) 
{  
    cout << "inside sumb_of_subsets" << endl; 
    if(promising(index, weight, W, w, total)) 
    { 
     cout << "promising is true, continue" << endl; 
     if(weight == W) 
     { 
      for(uint k = 0; k <= index; k++) 
      { 
       if(include.at(k)) 
        cout << w.at(k) << " ";; 
      } 
      cout << endl; 
     } 
     else 
     { 
      include.at(index + 1) = 1; 
      cout << "index1 = " << index << endl; 
      sum_of_subsets(index + 1, 
          weight + w.at(index + 1), 
          total - w.at(index + 1), 
          include, w, W) ; 
      include.at(index + 1) = 0; 
      cout << "index2 = " << index << endl; 
      sum_of_subsets(index + 1, 
          weight, 
          total - w.at(index + 1), 
          include, w, W); 
     } 
    } 
} 

bool promising (uint index, 
       uint weight, 
       uint W, 
       vector<uint> w, 
       uint total) 
{  
    cout << "inside promising" << endl; 
    cout << "W = " << W << endl; 
    cout << "weight = " << weight << endl; 
    return (weight + total >= W) 
     && ((weight == W) || (weight + w.at(index+1) <= W)); 
} 
+0

「但是我繼續得到一個越界錯誤」 - 請分享您的錯誤消息。 – Bill 2013-04-23 21:47:35

+0

我編輯了我的問題以上包括錯誤 – Busch 2013-04-23 21:50:34

+0

ddd說這發生了什麼? – Bill 2013-04-23 21:52:16

回答

1

此錯誤:

terminate called after throwing an instance of 'std::out_of_range' 
what(): vector::_M_range_check 

表明,載體的調試版本拋出異常,並沒有抓住它。發生這種情況時,程序會立即終止而不解開堆棧,這會使調試更加困難。

改成這樣:

// calculate the sum of subsets accordig to CL argument 
try 
{ 
    sum_of_subsets(index, weight, total, include, w, W); 
} 
catch (...) 
{ 
    cout << "put breakpoint here!" << endl; 
} 

添加在catch一個斷點,並檢查回溯,看看你的代碼的一部分出了問題。

+0

謝謝你,但是for循環索引不會導致我的問題。我相信這與sum_of_subsets的遞歸調用有關,但我無法確定調試時的位置和原因。 – Busch 2013-04-23 21:54:33

+0

在ddd中,當你在崩潰後鍵入bt(或backtrace)時會得到什麼? – Bill 2013-04-23 21:57:00

+0

@SeanHellebusch很確定這是你要走出界限。 – 2013-04-23 21:59:50