Hi List Members
I have data stored in an matrix. Now, i want to acces the data of the matrix
row-wise. because i want to access it in a sorted manner (without moving the
matrix entries around), i make a stl-vector of matrix_rows. This works fine.
But if i now use the stl algorithms to sort the stl-vector by a certain
criteria, then it happens, that the algorithm is making several copies of a
singe vector entry and overwriting other entries.
Can anyone give me a tipp, what i'm doing wrong?
----------------------------
Here is an example code:
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include
#include
namespace num=boost::numeric::ublas;
template <int N>
class Ordering
{
public:
bool operator()(num::matrix_row A,
num::matrix_row B)
{
if (A(N) < B(N) ) return true;
else return false;
}
};
int main(int argc, char* argv[])
{
const int N = 10000;
const int M = 4;
num::matrix<double> Matrix(N, M);
for (int i = 0; i < Matrix.size1(); i++)
{
for (int j = 0; j < Matrix.size2(); j++)
{
Matrix(i, j) = drand48();
}
}
std::vector > MyAccess;
for (int i = 0; i < Matrix.size1(); i++)
{
MyAccess.push_back(num::matrix_row(Matrix, i) );
}
for (int i = 0; i < Matrix.size1(); i++)
{
std::cout << MyAccess[i] << std::endl;
}
std::sort(MyAccess.begin(), MyAccess.end(), Ordering<0>() );
for (int i = 0; i < Matrix.size1(); i++)
{
std::cout << MyAccess[i] << std::endl;
}
}