Make Unreal REAL.
article thumbnail

 

 

Can I convert my 1D vector to a 2D vector faster than this?

The question is quite straightforward. After some trials, here is the most efficient code I found: //For the sake of the example, I initialize every entry as zero. vector<float> vector1D(1024...

stackoverflow.com

 

copy() 함수를 통해 벡터 내부로 관리되는 배열에 직접 값을 복사하면 된다.

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void main()
{
    vector<int> v1D = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    vector<vector<int>> v2D(4, vector<int>(3));
    
    for (int i = 0; i < 4; ++i)
        copy(v1D.data() + i * 3, v1D.data() + (i + 1) * 3, &v2D[i][0]);
        
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 3; ++j)
            cout << v2D[i][j] << " ";
            
        cout << endl;
    }
}

 

출력

1 2 3
4 5 6
7 8 9
10 11 12
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그