locked
How to - Iterate through a Platform::Collections Vector? Visual C++ WinRT

    Question

  • I've read up on the new Collections classes, but the documentation has no examples on how to iterate through the Map or Vector collection types.

    It would help me out a lot if somebody could help fill in this for loop:

    for(Platform::Collections::VectorIterator^ it = ...

    I am not seeing any "begin" or "end" functions in the Platform::Collections::Vector class..


    Land Of Simeria - http://www.stormsonggames.com/los



    Tuesday, October 16, 2012 10:44 PM

Answers

  • 2 ways to loop in a Platform::Collections::Vector

    Vector<int> ^vect;
    for (int item:vect)
    {
        sum+=item;
    }
    
    // or :
    for (IIterator<int> ^it = vect->First(); it->HasCurrent; it->MoveNext())
    {
        int item = it->Current;
        sum+=item;
    }


    I don't like the 2nd one, so I use the 1st one.

    But I guess the generated code is the same in the 2 cases.

    You can't use "++it" because ^it is not a C++ pointer.

    Tuesday, October 16, 2012 11:28 PM

All replies

  • The Platform::Collection::Vector and std::vector method names are different, but are similar.

    For example, instead of using std::vector::begin(), use Platform::Collection::Vector::First()

    See Vector methods here http://msdn.microsoft.com/EN-US/library/hh441570(v=VS.110,d=hv.2).aspx

    Alternatively, you can use ranged base for loops. Same syntax for std::vector and Platform::Collection::Vector

    Platform::Collections::Vector<int> ^vect;
    vect = ref new Vector<int>();
    vect->Append(1);
    vect->Append(7);
    for (int i:vect)
        sum+=i;


    Note that you can store ^ pointers in a std::vector:

    std::vector<myClass^> vect;
    vect.push_back(ref new myClass());
    

    Tuesday, October 16, 2012 11:09 PM
  • I saw that there is a First() function, but no Last, or End.  How can you setup a for loop?

    Is this the only way?

    unsigned int vectorSize = myVector->Size;
    for(unsigned int i = 0; i < vectorSize; ++i)
    {
    	//... myVector->GetAt(i);
    }

    I find it really odd that you cannot walk through a container using iterators.

    for(Platform::Collections::VectorIterator^ it = myVector->First(); it != ???; ++it)
    {
    ...
    }

    What should be written in place of "???"

    Also, I'm really curious what your code does:

    for (int i:vect)
        sum+=i;
    I've never seen this syntax before. What is happening here?


    Land Of Simeria - http://www.stormsonggames.com/los



    Tuesday, October 16, 2012 11:18 PM
  • 2 ways to loop in a Platform::Collections::Vector

    Vector<int> ^vect;
    for (int item:vect)
    {
        sum+=item;
    }
    
    // or :
    for (IIterator<int> ^it = vect->First(); it->HasCurrent; it->MoveNext())
    {
        int item = it->Current;
        sum+=item;
    }


    I don't like the 2nd one, so I use the 1st one.

    But I guess the generated code is the same in the 2 cases.

    You can't use "++it" because ^it is not a C++ pointer.

    Tuesday, October 16, 2012 11:28 PM
  • Wow, thanks Pierre!  That is awesome!!

    One related question, do you know how we can create a Vector and initialize it with contents from another Vector?

    I tried this, but doesn't work:

    Platform::Collections::Vector<int>^ vector2 = ref new Platform::Collections::Vector<int>(vector1);
    Also, you must have a ton of experience with Visual C++ and WinRT.. because the documentation has basically no examples of iteration. :P

    Land Of Simeria - http://www.stormsonggames.com/los


    Tuesday, October 16, 2012 11:36 PM
  • Also, I'm really curious what your code does:

    for (int i:vect)
        sum+=i;
    I've never seen this syntax before. What is happening here?

    It is the new C++11 "range based for" statement. You can use it to loop through enumerable types like std::vector or Platform::Collections::Vector. See http://msdn.microsoft.com/en-us/library/jj203382.aspx or search on the web for "range based for".

    Tuesday, October 16, 2012 11:40 PM
  • You are welcome !

    As far as I know, you cannot create a Vector from another Vector. But you can create a Vector from a Platform::Array. And you can create a Platform::Array from a Vector.

    So, using a temporary Array, you can create a Vector and initialize it with content from another Vector:

    	Vector<int> ^vect1;
    	vect1 = ref new Vector<int>();
    	vect1->Append(1);
    	vect1->Append(7);
    	Platform::Array<int, 1> ^tmpArray = ref new Array<int, 1>(10);
    	vect1->GetMany(0, tmpArray);
    	Vector<int> ^vect2 = ref new Vector<int>(tmpArray);
    	// or vect2->ReplaceAll(tmpArray);
    

    Of course, you can also copy Vector items one by one. I don't know which is faster.

    Alternatively you can use plain old std::vector (vector<reftype^>) instead of Vector...

    Wednesday, October 17, 2012 12:04 AM