Ask Your Question

Faster alternative to push_back

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

Iona gravatar image

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


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;
for (int i =0; i < rows; i++) 
            for (int j=0;j < cols;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



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 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

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

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


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

Seen: 724 times

Last updated: Aug 29 '14