std :: vector和_variant_t之间的转换 [英] conversion between std::vector and _variant_t
问题描述
我需要在 std :: vector
和 _variant_t
之间进行转换,以避免在将数据写入/文件(数据库,Excel等)
I need to convert between std::vector
and _variant_t
to avoid looping when writing/sending data into some file (database, Excel, etc.)
能否让我知道如何处理?
Could you please let me know how to proceed with that?
我试图得到一个 std :: vector< _variant_t>
并以某种方式放入另一个 _variant_t
变量,不支持转换,即使它完成,也没有任何方法或某种包装器将此向量转换为 _variant_t
变量。
I was trying to get a std::vector<_variant_t>
and somehow put into another _variant_t
variable, but direct conversion is not supported and even if it's done there's no either method or some kind of wrapper to get this vector into a _variant_t
variable.
我不想处理任何循环。
推荐答案
A std ::向量
和 _variant_t
是不兼容的类型。 _variant_t
类型旨在支持COM接口需要支持相同参数的多种类型的值的情况。它的值集合仅限于COM了解如何编组。 std :: vectory
不是这些类型之一。
A std::vector
and _variant_t
are incompatible types. The _variant_t
type is designed to support scenarios where a COM interface needs to support multiple types of values for the same parameters. It's set of values is limited to those for which COM understands how to marshal. std::vectory
is not one of those types.
_variant_t
是一个安全的数组。因此,最简单的解决方案是将 std :: vector
转换为安全数组,并将其存储在变体中。这里真的没有办法避免一个循环
The standard way to store a collection of values into a _variant_t
is to do so as a safe array. So the easiest solution is to convert the std::vector
to a safe array and store that in the variant. There's really no way to avoid a loop here
// Convert to a safe array
CComSafeArary<INT> safeArray;
std::vector<int> col = ...;
for (std::vector<int>::const_iteator it = col.begin(); it != col.end(); it++) {
safeArray.Add(*it);
}
// Initialize the variant
VARIANT vt;
VariantInit(&vt);
vt.vt = VT_ARRAY | VT_INT;
vt.parray = safeArray.Detach();
// Create the _variant_t
_variant_t variant;
variant.Attach(vt);
这篇关于std :: vector和_variant_t之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!