如何在C++/CX中迭代IIterable <T>?

Jed*_*dja 4 windows-runtime c++-cx

(这也可以表述为"如何迭代从C++/CX中的C#Windows运行时组件返回的集合?")

我尝试使用std::for_each,IIterable<T>但得到以下编译时错误

错误C2664:'std :: begin':无法将参数1从'my_collection_type ^'转换为'Platform :: String ^'没有可用的用户定义转换运算符,或者指向的类型不相关; 转换需要reinterpret_cast,C风格的转换或函数式转换

我如何迭代收藏?

Jed*_*dja 10

为此,您需要添加

#include "collection.h"
Run Code Online (Sandbox Code Playgroud)

(和可选的)

using namespace Windows::Foundation:Collections
Run Code Online (Sandbox Code Playgroud)

到您的源文件.

然后,您可以按如下方式迭代集合

for_each (begin(my_collection), 
          end(my_collection), 
          [&](my_collection_type^ value) {
          // code goes here...
});
Run Code Online (Sandbox Code Playgroud)

注意:您可能还需要using namespace std(用于for_each).


The*_*ser 9

这也应该有效

for (IIterator<T> iter = iterable->First(); iter->HasCurrent; iter->MoveNext())
{
   T item = iter->Current;
}
Run Code Online (Sandbox Code Playgroud)