Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Faster alternative to push_back

Hello,

I have been wondering in case there are faster alternatives to push_back for the code below? Any suggestion would be appreciated.

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

Faster alternative to push_back

Hello,

I have been wondering in case there are faster alternatives to am trying to modify a loop within a loop to make it run faster. I ma doing this by replacing push_back for 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 code below? 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.

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