2013-04-23 82 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