gpt4 book ai didi

c++ - 子集程序之和

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:51 25 4
gpt4 key购买 nike

我是 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));
}

最佳答案

这个错误:

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

表明您的 vector 调试版本正在抛出异常,但没有捕获到它。发生这种情况时,程序会立即终止而不展开堆栈,这会使调试变得更加困难。

更改为:

// 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 中添加一个断点,并检查回溯以查看您的代码的哪一部分出错了。

关于c++ - 子集程序之和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16179720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com