# vector, copy and add to

When I create a vector and add new calculated Mat to it using for loop, it will add the same reference as my Mat has same name.

for example:

vector<Mat> hists;
for (int row = 0 ; row < 2 ; row++)
for (int col = 0 ; col < 4 ; col++)
{
hist_mat.set_roi(row , col);
hist_mat.run();
hists.push_back(hist_mat.hist_h);
cout << hist_mat.hist_h << endl;
}

for (int i = 0 ; i < 8 ; i++)
{
cout << hists[i] << endl;
}


although the first cout has different lines, The second "cout" will have same lines (The last added element); Because all vector elements has same reference. What is a good way to copy and add to the vector?

edit retag close merge delete

This is not all the code. What is the definition of hist_mat ? My guess is that a clone() might be what you need.