Squaring numbers in an array I want to know if there is a better way to square numbers in an array. This is what I wrote:

#include &lt;iostream&gt;
using std::cout;
using std::endl;


void square(int []);


int main()
{
    int array[] = {2,4,6,8,10};

    square(array);
    for(auto i : array)
        cout &lt;&lt; i &lt;&lt; " ";

}



void square(int array[])
{
    int size = sizeof(array)/sizeof(array[0]);
    for(int i=0; i&lt; size; i++)
        array[i] *= array[i];
}
int size = sizeof(array)/sizeof(array[0]);

Just so you know, sizeof() returns size_t.
You can use a vector instead of an array. 

vector&lt;int&gt; data = {2,4,6,8,10};

Simplify squaring using std::transform() and use a lambda expression
    in place of a function object:




#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;functional&gt;

using std::cout;
using std::transform;
using std::vector;

int main()
{

    vector&lt;int&gt; data = {2,4,6,8,10};

    transform(data.begin(), data.end(), data.begin(), [](int x){return x*x;});


    for(auto i : data)
        cout &lt;&lt; i &lt;&lt; " ";

}