andreas-h
andreas-h

Reputation: 11100

how to pass a stl vector to a function which takes a const [] (c++)

i have a 3d stl vector,

vector<vector<vector<double> > > mdata;

i also have a function

myfun(const double ya[]);

to be more precise, it's a function from the GNU Scientific Library,

gsl_spline_init(gsl_spline * spline, const double xa[], const double ya[], size_t size);

but this is not related to my problem.

so now i want to pass the 'last' dimension of data to myfun. i've been trying this:

for (int s = 0; s < msize; s++) {
    accelerators = new gsl_interp_accel*[msize];
    splines = new gsl_spline*[msize];
    for (int i = 0; i < msize; i++) {
        accelerators[i] = gsl_interp_accel_alloc();
        splines[i] = gsl_spline_alloc(gsl_interp_cspline_periodic, msize+1);
        gsl_spline_init(splines[i], &(*mgrid.begin()), &(*mdata[s][i].begin()), msize+1);
    }
}

But the compiler (g++, 64bit, Ubuntu), complains:

In member function ‘std::vector<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, std::allocator<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > > > SimpleAmfCalculator::interp_m(int)’: Calculator.cpp:100: error: cannot convert ‘std::vector<double, std::allocator<double> >*’ to ‘const double*’ for argument ‘3’ to ‘int gsl_spline_init(gsl_spline*, const double*, const double*, size_t)’ make: *** [Calculator.o] Error 1

Any help is greatly apprecitated!

Upvotes: 2

Views: 3618

Answers (4)

andreas-h
andreas-h

Reputation: 11100

So, the following seems to work for me:

#include <vector>

void fun(const double data[])
{

}

int main()
{
    std::vector<std::vector<std::vector<double> > > data3d;
    ....
    fun(&(data3d[0][0].front()));
}

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308402

This cries out for a general solution.

template<typename T, typename A>
T* PointerOf(std::vector<T,A> & vec)
{
    return &vec.at(0);
}

template<typename T, typename A>
const T* ConstPointerOf(const std::vector<T,A> & vec)
{
    return &vec.at(0);
}

myfun(ConstPointerOf(mdata[s][i]));

Edit: I added the template parameter for the vector allocator as suggested in the comments; I also used at() instead of [] so I wouldn't have to check for an empty vector, and I added a second version of the function for a const pointer.

Upvotes: 5

jscharf
jscharf

Reputation: 5899

I think

&(*mdata[s][i].begin());

is returning a std:vector of type double.

Upvotes: 0

Khaled Alshaya
Khaled Alshaya

Reputation: 96879

You could pass the address of the first element, for example:

#include <vector>

void fun(const double data[])
{

}

int main()
{
    std::vector<std::vector<std::vector<double> > > data3d;
    ....
    fun(&data3d[0][0][0]);
}

The elements of vector are stored contiguously. So this way is standard as I hope :)

23.2.4 Class template vector

1 A vector is a kind of sequence that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity:

&v[n] == &v[0] + n for
all 0 <= n < v.size().

Upvotes: 8

Related Questions