【发布时间】:2016-03-05 16:57:29
【问题描述】:
我正在使用的外部 API 需要 C 样式的对象数组:
// Some api function
void doStuff(const Foo* objects, size_t length);
实际上,API 使用int 作为长度,但这只会让它变得更糟。创建对象数组时,我不知道我会有多少,因为有些结果是错误的:
void ObjManager::sendObjectsToApi(const std::list<const std::string>& names)
{
// Create the most suitable type of connection
std::????<Foo> objects;
// Loop names, try to create object for every one of them
for( auto i=names.begin(), l=names.end(); i<l; i++ ) {
Foo obj = createFooWithName(*i);
if( obj.is_valid() ) {
objects.addToCollection( obj );
}
}
// Convert collection to C style array
size_t length = objects.size();
Foo* c_objects = objects.toC_StyleArray();
API::doStuff(c_objects, length);
}
【问题讨论】:
-
std::vector::data()有什么问题吗? -
std::vector
对象; -
使用 std::vector,也可以像数组一样访问
-
因此您需要在运行时确定大小的连续对象集合。矢量有什么问题?
-
@TomášZato 列表不连续