I have a simple matrix of 50x40, I fill its elements with scalar values from 0.0f-50.0f as can be seen in code. Then, I truncate the matrix scalar values based on lowerbound and upperbound as per the code. The code works great, no problem.
#include "boost\numeric\ublas\matrix.hpp"
typedef boost::numeric::ublas::matrix<float> matrix;
template <typename T1>
class Truncate{ // Functor used for truncation
public:
Truncate(T1 lbIn, T1 ubIn) : lb(lbIn), ub(ubIn){}
float operator()(const float &val) const {
if (val <= lb)
return lb;
else if (val >= ub)
return ub;
else
return val;
}
private:
T1 lb; // lowerBound
T1 ub; // upperBound
};
void TestMinMax(){
matrix m1(50, 40);
for (size_t i = 0; i < m1.size1(); i++) {
for (size_t j = 0; j < m1.size2(); j++) {
m1(i, j) = i + j*0.01f;
}
}
Truncate<float> chop(10.4f, 30.55f);
transform(m1.begin2(), m1.begin2() + m1.size1()*m1.size2(), m1.begin2(), chop); // Works!
// transform(m1.begin1(), m1.begin1() + m1.size1()*m1.size2(), m1.begin1(), chop); // Gives run-time error
for (size_t i = 0; i < m1.size1(); i++) {
for (size_t j = 0; j < m1.size2(); j++) {
cout << m1(i, j) << "\t";
}
cout << endl;
}
}
But, I would like to draw your attention to a line I have commented out in my code -
// transform(m1.begin1(), m1.begin1() + m1.size1()*m1.size2(), m1.begin1(), chop); // Gives run-time error
If I use this line, instead of the one currently active, I get run-time error. Interestingly, the run-time error is only encountered when the iterator range exceeds 40. What could be the reason for the run-time error?
Aucun commentaire:
Enregistrer un commentaire