vector, copy and add to

asked 2015-01-26 00:24:38 -0500

updated 2015-01-26 00:28:19 -0500

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 flag offensive close merge delete

Comments

1

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

boaz001 gravatar imageboaz001 ( 2015-01-26 10:46:15 -0500 )edit

You are right, I repaired the code. and it's now corrected by clone() method.

Mohammad Etemaddar gravatar imageMohammad Etemaddar ( 2015-01-26 22:48:33 -0500 )edit