문제

I have an array of elements which I want to copy into another array and shift it by 1 position. So something like that:

void Blubb(){

    std::vector<double> array1(n);
    std::vector<double> array2(n+1);

    while(//a condition){
    //do some stuff

        for(int i = 0; i < (n+1); i ++){

            array1[i] = array2[i+1];

        }

        //do some more stuff
    }
}

The first element in array2 is intentionally not part of array1.

Is there any "easy" way to do that? Does the container class already provide something similar? Or are there any other classes I should try?

도움이 되었습니까?

해결책

Looks like you forgot the template argument for std::vector. Anyway, try something like:

std::vector<int> array2(n+1);
std::vector<int> array1(array2.begin()+1, array2.end());

다른 팁

Firstly, you need a template argument: replace all occurrences of std::vector with std::vector<int>, replacing int with whichever type you're using. First copy the vector:

std::vector<int> array2(n+1);
// fill the vector
std::vector<int> array1(array2);

(Note that array2 has to be declared first.)

Secondly, delete the first element:

array1.erase(array1.begin());

Hope this helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top