Frage

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?

War es hilfreich?

Lösung

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());

Andere Tipps

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!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top