subarray through index: how to?
<p>I am trying to implement a simple matlab-like array (now only one dimension actually), what I tried to do is to implement the following matlab codes:</p>

<pre><code>a=1:10;
ind=find(a&gt;5);
a[ind]=5;
</code></pre>

<p>I know that the std has valarray to do this through a slice array. I do not know much details on it. The code is:</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;
template &lt;typename T&gt; class array
{
public:
    int m,n; //two dimensional at most
    T *pdata;
    //construct the array
    array(){m=n=0;pdata=NULL;} //default is empty matrix
    array(T a){m=n=1;pdata=new T[1];*pdata=a;} //array for scalar: array a=10;
    array(int m0,int n0=1) {m=m0;n=1;pdata=new T[m];}
    array(const array&amp; a,int len=-1);
    //destructor
    ~array() {delete []pdata;}
    //operator overloading
    array&lt;T&gt;&amp; operator+=(T s);
    T&amp; operator[](int i) {return pdata[i];}
    array&lt;T&gt;&amp; operator[](array&lt;int&gt; ind);
    array&lt;T&gt;&amp; operator=(const array&lt;T&gt;&amp; a);
    array&lt;T&gt;&amp; operator=(T a) {for(int i=0;i&lt;m;i++) pdata[i]=a;return *this;}
    array&lt;bool&gt; operator&gt;(T a);
    array&lt;bool&gt; operator&lt;(T a);
    array&lt;bool&gt; operator==(T a);
};

//copy a part of the other array
template &lt;typename T&gt; array&lt;T&gt;::array&lt;T&gt;(const array&lt;T&gt;&amp; a,int len)
{
    if(len==-1) len=a.m*a.n;
    if(len==0) {m=0;n=0;pdata=NULL;}
    if(len&gt;0)
    {
        m=len;n=1;
        pdata=new T[len];
        for(int i=0;i&lt;len;i++) pdata[i]=a.pdata[i];
    }
}

template &lt;typename T&gt; array&lt;T&gt;&amp; array&lt;T&gt;::operator +=(T s)
{
    for(int i=0;i&lt;m*n;i++) pdata[i]+=s;
    return *this;
}

//this function does not meet the purpose, it returns a reference to a temp obj
template &lt;typename T&gt; array&lt;T&gt;&amp; array&lt;T&gt;::operator[](array&lt;int&gt; ind)
{
    array&lt;T&gt; ret(ind.m,ind.n);
    for(int i=0;i&lt;ind.m*ind.n;i++)
    {
        ret.pdata[i] = pdata[ind.pdata[i]];
    }
    return ret;
}

template &lt;typename T&gt; array&lt;bool&gt; array&lt;T&gt;::operator&gt;(T a)
{
    array&lt;bool&gt; res(m*n);
    for(int i=0;i&lt;m*n;i++) res.pdata[i]=pdata[i]&gt;a;
    return res;
}

//helper function
array&lt;int&gt; find(array&lt;bool&gt; a)
{
    array&lt;int&gt; ret(a.m,a.n); //first use the same size space
    int len=0;
    for(int i=0;i&lt;a.m*a.n;i++)
    {
        if(a.pdata[i]) {ret.pdata[len]=i;len++;}
    }
    return array&lt;int&gt;(ret,len);
}

/*ostream&amp; operator&lt;&lt;(array&lt;T&gt;&amp; a)
{
    ostream os;
    for(int i=0;i&lt;a.m*a.n;i++) os&gt;&gt;a[i]&gt;&gt;'\t';
    return os;
}*/

int main()
{
    array&lt;float&gt; a(10);
    for(int i=0;i&lt;10;i++) a[i]=i;
    for(i=0;i&lt;10;i++) cout&lt;&lt;a[i]&lt;&lt;'\t';
    cout&lt;&lt;endl;
    array&lt;int&gt; ind=find(a&gt;5);
    for(i=0;i&lt;ind.m;i++) cout&lt;&lt;ind[i]&lt;&lt;'\t';
    cout&lt;&lt;endl;
    a[ind]=5;//this will not work on the original array
    //how do we support this????undefined
    for(i=0;i&lt;10;i++) cout&lt;&lt;a[i]&lt;&lt;'\t';
    cout&lt;&lt;endl;

    return 0;
}
</code></pre>

<p>The final a is not changed at all since we are working on a temp array.
I know the function operator"> is not properly implemented, but I do not know how to do this. Anyone can give me a hint? Thanks</p>
