lundi 4 avril 2022

Convert float* to 2D vector std::vector

Follow the discussion from the question: Fastest way to transfer/copy data from float* to vector<float> . I want to extend it to 2D vector. Fistly, I post my original code with 2 for loops.

std::vector<std::vector<float>> ConvertFloatto2DVector(float* input, int size0, int size1) {
  std::vector<std::vector<float>> vector2D;
  for (int sz1 = 0; sz1 < size0; sz1++) {
    std::vector<float> vector1D;

    for (int sz2 = 0; sz2 < size1; sz2++) {
      vector1D.push_back(input[sz1* size1+ sz2]);
    }
    vector2D.push_back(vector1D);
  }
  return vector2D;
}

Based on discussion from the above question, we can use a fastest way to convert float* to 1D vector as std::vector<float> v(input, input + size);. Is it possible to apply the way to 2D vector size of size1x size2 as my above original code ? And How?

Aucun commentaire:

Enregistrer un commentaire