jeudi 14 février 2019

how to obtain a C array from an std::vector

I'm new to C++11 and I'm confused about the usage of std::refence_wrapper class.

let's consider the example shown in https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper ,which shuffles the elements contained in a standard std::vector, i.e:

std::list<int> l(10); 
std::iota(l.begin(), l.end(), -4);
std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});

Now, if consider the original vector l, I have that l.data() returns a pointer to the internal array, which can I use in a C application.

Instead, it's not clear to me instead what is returned by v.data(). I tried various combinations of surely wrong casts, such as int* p = (int*)(v.data()->get()) without obtaining correct results (the swapped values).

My goal is to interface a C++ application (which gives me a vector of reference wrappers) with an old C library. Can you point me which is the most correct way to obtain a C-like array from the vector v, after the shuffle shown in the example? Do I need to copy all the elements one-by-one in another vector? Is there any C++11 class which can help me in the job? Thank you

Aucun commentaire:

Enregistrer un commentaire