문제

I use blitz library regularly in c++. It has quite a few nice facilities like to print a 2-dimensional array one just needs to

cout<<A<<endl;

However, the output comes out like (for 2x2 arrays)

2 x 2
[ 4  5 
  2  1]

Now, I wish to get rid of the 2x2 dimension and the brackets it places, because it sometimes creates problem when I wish to directly use the file to plot something. How do I do this?

도움이 되었습니까?

해결책

You need to write your own output routine.

Probably the simplest way to do that is to create a proxy class template:

template <typename blitzarray>
struct printer
{
    printer (const blitzarray& ba_) : ba(ba_) {}
    const blitzarray& ba;
};

which outputs the array it has just the way you like:

template <typename blitzarray, typename stream>
stream& operator<< (stream& s, printer<blitzarray> pb)
{
    // <-- print the array here <--
    return s;
}

and then have a little helper function that helps deducing the template argument:

template <typename blitzarray>
printer<blitzarray> myprint(const blitzarray& ba)
{
    return printer<blitzarray>(ba);
}

The use is just:

cout << myprint(A) << endl;

You probably can borrow from the original blitz++ output routine.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top