Ask Your Question

Revision history [back]

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);
 }

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

This program has same lines; Because all vector elements has same reference. What is a good way to copy and add to the vector?

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;
}

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