Hi everyone, i'm new in DaniWeb, so forgive me by some mistake, x)

I need create a iterator, it can receive any kind of struct of STL, like map, vector, list, ...

How i do that?
Someone have any idea?

Thanks by your attention.

Best regards,
Valter Henrique.

Dani AI

Generated

— two broad options depending on what you mean by "a single iterator for any container":

  • If you want generic code that can work with vector, list, map, etc., prefer compile-time templates. The STL iterators plus templates let you write one function/class that accepts any container type. This is the simplest, fastest, and idiomatic approach (see range-based for: cppreference — range-for).

Example (generic function):

template<typename Container>
void for_each_item(const Container& c) {
    for (auto const& item : c) {
        // item type depends on container (map yields pair<const K,V>)
        std::cout << /* handle item */ << '\n';
    }
}

Notes: map elements are pairs, so use item.first/item.second or structured bindings (C++17). Templates require the element type to meet whatever operations you perform (e.g., streamable for <<).

  • If you need a single runtime object that can hold iterators from different container types (for example store them in the same variable or container), use type erasure. This is more complex and slower (heap allocation, virtual calls), and it only works cleanly when all iterators expose a compatible value type and a common minimal iterator category. For production use prefer a tested library (Boost.Range any_range / Boost.TypeErasure). A minimal custom skeleton looks like this:
    template<typename T>
    class any_iterator {
    struct concept { virtual ~concept(){}; virtual concept* clone() const =0; virtual void inc()=0; virtual T& get() const=0; virtual bool equal(const concept*) const=0; };
    template<typename It> struct model : concept { It it; model(It i):it(i){} concept* clone() const override { return new model(it); } void inc() override { ++it; } T& get() const override { return const_cast<T&>(*it); } bool equal(const concept* o) const override { if(auto p = dynamic_cast<const model*>(o)) return it==p->it; return false; } };
    std::unique_ptr<concept> p;
    public:
    template<typename It> any_iterator(It i): p(new model<It>(i)) {}
    any_iterator(const any_iterator& o): p(o.p?o.p->clone():nullptr) {}
    any_iterator& operator++(){ p->inc(); return *this; }
    T& operator*() const { return p->get(); }
    bool operator==(const any_iterator& o) const { return p->equal(o.p.get()); }
    };

Recommendation: follow — use STL iterators with templates unless you specifically need runtime polymorphism. For a robust ready-made solution, see Boost.Range/any_range or Boost.TypeErasure. Links: cppreference iterators, Boost.Range docs.

Recommended Answers

All 2 Replies

Why create it when stl already has one for you? Maybe you should give
more information on what you are trying to do.

Why create it when stl already has one for you? Maybe you should give
more information on what you are trying to do.

Hi, i need create an iterator that supports any kind of struct, like map, vector, list... etc.

In STL, we have to be specific, we have to tell the struct that we gonna use.
Each struct already have your own iterator, but i need an iterator to any kind of structure, a single iterator to acess vector, map, list, .. etc.

Thanks by your attention.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.