   casting int* to vectorint
  Well casting int* to vector<int> used to work in VC++6, which doesn't work anymore in VC++7 or 8. I know its probably not the neatest thing to do for a C++ purist but some libraries would take int* as function arguments while we were dealing with vector<int>... is there anyway we can do the casting or do i have to make a seperate malloc and them copy all the content of the vector<int> to the allocated memory and bla bla... (damn, in that case.. it will take lots of time just to covert data)   
void c_api_requiring_int_pointer(int * pointer, unsigned int size);void my_function(){std::vector< int > my_vector;fill(my_vector);assert(!my_vector.empty());c_api_requiring_int_pointer(&my_vector[0], my_vector.size());}Σnigma   ;Quote:Original post by brownyWell casting int* to vector<int> used to work in VC++6, which doesn't work anymore in VC++7 or 8.int array[100];std::vector<int> vec(array, array+100); // copy the contents of the array.Enigma has already demonstrated the vector -> array 'cast', though I prefer &vec.front() to &vec[0]   ;  No, it didn't. Or at least it shouldn't have.Anyway, the casting you want in the described situation is in the other direction. You cast "from" the current type "to" the required type. Anyway, your question has been answered.Enigma, are you sure that assertion is necessary? If .empty(), .size() will be zero, and the C API should have been designed to handle the case of a zero length list...   ;Quote:Enigma, are you sure that assertion is necessary? If .empty(), .size() will be zero, and the C API should have been designed to handle the case of a zero length list...Yeah, but in that case &my_vector[0] would cause a subscript range error. If we don't assert(), we must at least check for zero length and send a NULL pointer to the C function or something.;  This is an interesting dilemma. It would seem that there's no portable way to obtain a pointer to the contents of an empty vector. A proposal has been made, however, to add a data() (both const and non-const) member functions to std::vector, which will take care of this problem. (There's also the extremely handy at() member function for std::maps that throws if the given key was not found.) Technically, the addition is not yet part of the draft Standard, but its inclusion is pretty much certain. libstdc++, for example, already supports those functions.[Edited by - Sharlin on June 5, 2006 2:06:00 PM];Quote:Original post by SharlinThere's also the extremely handy at() member function for std::maps that throws if the given key was not found.Which std::vector also has.   ;  Hey look, it gives a pointer to the vector's data if it exists:template<typename Vector>Vector::value_type* beginof( Vector& v ) {  if (v.empty()) return 0;  return &v.front();}template<typename Vector>Vector::value_type const* beginof<const Vector>( Vector const& v ) {  if (v.empty()) return 0;  return &v.front();}Improvements would include making sure you are passed a vector and not some other standard container.