Vector-transposing function I profiled a library I'm writing that uses vector transposes and found that I am spending a good bit of time doing the following transpose. I am using a std::vector of std::vector&lt;double&gt;s to represent the column vectors.

What are some ways to optimize this function?

std::vector&lt;double&gt; transpose_vector(const std::vector&lt;std::vector&lt;double&gt;&gt; &amp;column_vec) {
  // take a column vector:
  // |x1|
  // |x2|
  // |x3|
  // and return a row vector |x1, x2, x3|
  std::vector&lt;double&gt; row_vector;
  for (auto c : column_vec) {
    for (auto r : c) {
      row_vector.push_back(r);
    }
  }
  return row_vector;
}
Change your column loop to use a reference.

for (const auto &amp;c : column_vec)


Without the reference, a copy will be made of each vector.  This will involve a memory allocation.  Using the reference you avoid all that, which should save a good deal of time since each c will be a single element vector.

auto r can stay since r will be a double.

Combine this with using reserve on row_vector will eliminate all but one memory allocation.