转换的std ::列表到C型友好 [英] Converting std::list to C friendly type
问题描述
什么是最优雅的方式从一个共享库函数(由C ++ code实现)为C消费者返回的std ::列表
对象吗?我知道的std ::矢量
,我们可以返回矢量的第一个元素的地址,并有消费者把它当作一个数组,但性病::清单实施作为链接LIS。
What's the most elegant way to return a std::list
object from a shared lib function (implemented by C++ code) to a C consumer? I know for std::vector
, we can return the address of the 1st element of the vector and have the consumer treat it as an array, but std::list is implemented as a linked lis.
推荐答案
的的std ::列表
复制到的std ::矢量
,返回的第一个元素的地址,因为你已经提到。
Copy the std::list
to a std::vector
and return the address of the first element, as you already mentioned.
(当然,这可能意味着你不想使用摆在首位一个的std ::列表
来定。)
(Of course, this may mean that you don't want to be using a std::list
in the first place.)
(该解决方案假设所访问的对象是的拥有的由C ++库 - 如果不是这种情况,你可能需要考虑从C code分配内存并将指针传递到C ++库来复制数据。)
(This solution assumes that the object being accessed is owned by the C++ library -- If this isn't the case, you may need to consider allocating the memory from your C code and passing a pointer into the C++ library to copy the data.)
这篇关于转换的std ::列表到C型友好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!