以下代码段产生'index out of bounds'错误。我知道这个错误的来源。我想知道是否有一种方法可以使程序显示index [I],normpar [I]和normxpar [I]的值,当产生'index out of bounds'错误时。这对缩小错误来源有很大帮助。
for (I = 0; I < 100; I++)
norm2par[I] = (normxpar[I] - discvec[indices[I]]);
答案 0 :(得分:1)
void foo(std::vector<int>& norm2par,
std::vector<int>& normxpar,
std::vector<std::size_t>& indices,
std::vector<int>& discvec)
{
std::size_t I;
try
{
for (I = 0; I < 100; I++)
{
norm2par.at(I) = (normxpar.at(I) - discvec.at(indices.at(I)));
}
}
catch(...)
{
std::ostringstream ss;
ss << __func__ << " throw with I=" << I;
std::throw_with_nested(std::logic_error(ss.str()));
}
}