Ask Your Question
-1

Faster alternative to push_back

asked 2014-08-28 15:09:11 -0600

Iona gravatar image

updated 2014-08-28 16:18:13 -0600

Hello,

I am trying to modify a loop within a loop to make it run faster. I ma doing this by replacing push_back with insert. However, insert does not seem to work for me as it is giving build errors.

for (int i =0; i < rows; i++) 
{
    vector<double> temp1;
    temp1.insert(temp1.end(), signal.begin(), signal.end());
}

This does not seem to work. It's push_back which works is:

vector<double> temp1;
temp1.reserve(512);
for (int i =0; i < rows; i++) 
{
            for (int j=0;j < cols;j++) 
            {
                    temp1.push_back(signal[i][j]);
            }
  }

I believe the error is occurring because signal is of type vector < vector < double > > while temp1 is vector < double > And since signal is further being passed into another function, I would not want to change the type. Any suggestion would be appreciated.

edit retag flag offensive close merge delete

Comments

3

This has nothing to do with OpenCV but is just a regular C++-Problem. You recognize your problem correctly, temp1 and signal have to have the same type if you want to use insert. One thing you could do is to insert each signal[i] into temp.

FooBar gravatar imageFooBar ( 2014-08-29 02:31:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-08-29 06:21:01 -0600

thdrksdfthmn gravatar image

Because signal is a vector<vector<double>>, if you want to insert a (as one) signal in temp1, then you shall insert jut that one; so use:

temp1.insert(temp1.end(), signal[i].begin(), signal[i].end());
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-08-28 15:09:11 -0600

Seen: 1,203 times

Last updated: Aug 29 '14