mercredi 14 novembre 2018

C++ create an array of size of a vector and then copy vector into C style array

VC++ is giving an error on below code:

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> a;
    a.push_back(10);
    a.push_back(20);
    a.push_back(30);
    int arr[a.size()];
    std::copy(a.begin(), a.end(), arr);
    for(int index = 0 ; index < a.size(); index++)
    {
        std::cout << " The value is " << arr[index] << std::endl;
    }
}

It errors out at the integer array declaration stating that the value of variable 'a' cannot be used as constant?

How can we resolve the issue where my objective is to transfer content of vector into a 'C' style array?

Aucun commentaire:

Enregistrer un commentaire