如何将 stl 向量传递给采用 const [] 的函数 (c++)

发布于 2024-08-03 02:03:31 字数 1545 浏览 2 评论 0 原文

我有一个 3d stl 矢量,

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

我还有一个

myfun(const double ya[]);

更精确的函数,它是来自 GNU 科学库的函数,

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

但这与我的问题无关。

所以现在我想将数据的“最后”维度传递给 myfun。我一直在尝试这个:

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);
    }
}

但是编译器(g++,64位,Ubuntu)抱怨:

在成员函数中 'std::向量 >, std::分配器 > > >, std::分配器 >, std::分配器 > > > > > SimpleAmfCalculator::interp_m(int)': Calculator.cpp:100: 错误:不能 转换 'std::vector >*' 到 'const double*' 对于参数 '3' 到 'int gsl_spline_init(gsl_spline*, 常量 double*, const double*, size_t)' make: *** [Calculator.o] 错误 1

​​非常感谢任何帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷夜 2024-08-10 02:03:31

您可以传递第一个元素的地址,例如:

#include <vector>

void fun(const double data[])
{

}

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

vector 的元素是连续存储的。所以正如我希望的那样,这种方式是标准的:)

23.2.4 类模板向量

1 向量是一种
支持随机访问的序列
迭代器。此外,它还支持
(摊销)恒定时间插入和
最后进行擦除操作;插入
并在中间擦除采取线性
时间。存储管理已处理
自动,尽管提示可以
给予提高效率。这
存储向量的元素
连续,这意味着如果v
向量,其中 T 是某个
输入 bool 以外的类型,则它遵循
身份:

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

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().
春庭雪 2024-08-10 02:03:31

这就迫切需要一个通用的解决方案。

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]));

编辑:我按照评论中的建议添加了向量分配器的模板参数;我还使用了 at() 而不是 [],这样我就不必检查空向量,并且我为 const 指针添加了该函数的第二个版本。

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.

机场等船 2024-08-10 02:03:31

我认为

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

返回的是 double 类型的 std:vector

I think

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

is returning a std:vector of type double.

執念 2024-08-10 02:03:31

所以,以下似乎对我有用:

#include <vector>

void fun(const double data[])
{

}

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

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()));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文