lundi 9 août 2021

Dynamically update range inside loop by overload end() method

everyone! I want to create class to store data also dynamically - inside range based loop. But I face problem to overload end() method. The idea is something like:

template<typename T>
class ContainerClass
{
public:
    typename std::vector<T>::iterator begin()
    {
        return _v.begin();
    }

    typename std::vector<T>::iterator end()
    {
        return _v.end();
    }

    void add(T a)
    {
        _v.push_back(a);
    }

private:
    std::vector<T> _v;
};

int main()
{
    ContainerClass<int> c;
    
    c.add(1);
    c.add(2); 

    for (auto e : c)
    {
        std::cout << e << std::endl;

        if(input())
           c.add(3);
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire