Questa è probabilmente la risposta che stai cercando. Altri hanno suggerito che non è possibile racchiudere un array in un vettore, ma semplicemente non è vero; pensaci, un vettore ha una matrice come contenitore di dati sottostante! Avevo tentato questo e quello per un po 'di tempo prima di trovare una soluzione praticabile. L'avvertenza è che è necessario azzerare i puntatori dopo l'uso al fine di evitare il doppio liberare la memoria.
#include <vector>
#include <iostream>
template <class T>
void wrapArrayInVector(T *sourceArray, size_t arraySize, std::vector<T, std::allocator<T> > &targetVector) {
typename std::_Vector_base<T, std::allocator<T> >::_Vector_impl *vectorPtr =
(typename std::_Vector_base<T, std::allocator<T> >::_Vector_impl *)((void *) &targetVector);
vectorPtr->_M_start = sourceArray;
vectorPtr->_M_finish = vectorPtr->_M_end_of_storage = vectorPtr->_M_start + arraySize;
}
template <class T>
void releaseVectorWrapper(std::vector<T, std::allocator<T> > &targetVector) {
typename std::_Vector_base<T, std::allocator<T> >::_Vector_impl *vectorPtr =
(typename std::_Vector_base<T, std::allocator<T> >::_Vector_impl *)((void *) &targetVector);
vectorPtr->_M_start = vectorPtr->_M_finish = vectorPtr->_M_end_of_storage = NULL;
}
int main() {
int tests[6] = { 1, 2, 3, 6, 5, 4 };
std::vector<int> targetVector;
wrapArrayInVector(tests, 6, targetVector);
std::cout << std::hex << &tests[0] << ": " << std::dec
<< tests[1] << " " << tests[3] << " " << tests[5] << std::endl;
std::cout << std::hex << &targetVector[0] << ": " << std::dec
<< targetVector[1] << " " << targetVector[3] << " " << targetVector[5] << std::endl;
releaseVectorWrapper(targetVector);
}
In alternativa si può solo fare una classe che eredita da vettoriale e nulli i puntatori su di distruzione:
template <class T>
class vectorWrapper : public std::vector<T>
{
public:
vectorWrapper() {
this->_M_impl _M_start = this->_M_impl _M_finish = this->_M_impl _M_end_of_storage = NULL;
}
vectorWrapper(T* sourceArray, int arraySize)
{
this->_M_impl _M_start = sourceArray;
this->_M_impl _M_finish = this->_M_impl _M_end_of_storage = sourceArray + arraySize;
}
~vectorWrapper() {
this->_M_impl _M_start = this->_M_impl _M_finish = this->_M_impl _M_end_of_storage = NULL;
}
void wrapArray(T* sourceArray, int arraySize)
{
this->_M_impl _M_start = sourceArray;
this->_M_impl _M_finish = this->_M_impl _M_end_of_storage = sourceArray + arraySize;
}
};
Ricordate che è possibile ottenere 'std :: VECTOR' per copiare gli elementi restituiti dal array come mostrato di seguito, ma se questa API si aspetta che tu chiami un'altra funzione per liberare la memoria allocata per l'array o per eliminare l'array tu stesso, devi farlo. La creazione del vettore non libererà quella memoria. – Praetorian
La funzione API * restituisce * a 'double *', oppure accetta un puntatore come * argomento * e lo riempie di dati? –
Kerrek SB // buon punto! qualcosa restituisce un doppio * qualcosa prende un puntatore come argomento. – webnoon