jeudi 5 juillet 2018

range-based for loop for private map values

I have the following code:

#include "stdafx.h"
#include <map>
#include <string>
#include <iostream>

class MyObject
{
public:
    MyObject()
        : m_Items{ { 1, "one" },{ 2, "two" },{ 3, "three" } }
    {}

    RETURNTYPE GetStringIterator() const
    {
        IMPLEMENTATION
    }

private:
    std::map<int, std::string> m_Items;
};


int main()
{
    MyObject o;
    for (auto& s : o.GetStringIterator())
    {
        std::cout << s;
    }
}

What should RETURNTYPE and IMPLEMENTATION be in order to allow any client of MyObject (in this case the main() function), to iterate over the values of the m_Items map, without copying any data? It seems that this should be possible with c++11 range based for loops and iterators. but I have not been able to figure out how.

Aucun commentaire:

Enregistrer un commentaire