0

I'm novice in c++ programming and i need to create iterator but i'm having problems with the loop (in c++11) because it doesn't recognize the elements, i explain:

class myclass{
    std::string str;
    myclass();
    std::iterator<(what do i have to put here?), char, diffptr_t, char*, char&> begin(){
       return str.begin();
    }
}

This the method that reads the class:

 void func(myclass& m){
     for(char a: m){ //Here's the problem, i don't know why it doesn't work
         //do function  
     }

Can any body tell which is the best method for do that?? and what's wrong here???

DrkDeveloper
  • 939
  • 7
  • 17
  • just as a note, std::iterator is for helping to implement iterators not for actually using as the type of an iterator. – Jake Aug 17 '13 at 16:36

2 Answers2

1

At least if I understand your question correctly, you want something like:

class myclass { 
    std::string str;
public:
    std::string::iterator begin() { return str.begin(); }
    std::string::iterator end() { return str.end(); }
};

void func(myclass &m) { 
    for (auto a : m)
        // ...
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • yeah, i had thought in that but, it's for learn, how is iterator created? (sorry for the question here) – DrkDeveloper Aug 17 '13 at 16:06
  • @user2688108: an iterator is a class that overloads comparison, `operator*`, `operator++`, and possibly more (e.g., `operator--` if it's a bidirectional iterator). [Here's](http://stackoverflow.com/a/3479326/179910) one example. – Jerry Coffin Aug 17 '13 at 16:15
  • the iterator should also inherit from `std::iterator` or provide a bunch of nested typedefs itself – TemplateRex Aug 17 '13 at 18:02
1

If you're just returning the iterators from std::string, then you should be able to do something like this:

auto begin() -> decltype(str.begin())
{
    return str.begin();
}

A simple iterator can be very simple indeed. It needs to be able to be compared to another iterator of its own type, and it needs at least the prefix-increment operator (for the range-based for loop to work) and of course the dereference operation. That's about it.

In you case it could be something like

class iterator
{
    friend myclass;  // So that only `myclass` can construct this

public:
    bool operator==(const iterator& other) const
    {
        // Implement comparison
    }

    iterator& operator++()
    {
        // Increment
        return *this;
    }

    char& operator*() const
    {
        // Return a reference to the "current" character
    }

    friend bool operator!=(const iterator& i1, const iterator& i2)
    {
        return !(i1 == i2);
    }

private:
    iterator(...) { ... }

    // Put the actual iterator here (e.g. `std::string::iterator`)
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • for the moment i will use these methods (comments 1 and 2) but i need to know to create iterator. Thanks for all – DrkDeveloper Aug 17 '13 at 16:07
  • you should inherit from `std::iterator` with the appropriate value_type or provide these nested types yourself in order for this iterator to be used with the standard algorithms. – TemplateRex Aug 17 '13 at 17:59
  • @TemplateRex There's no need to inherit from [`std::iterator`](http://en.cppreference.com/w/cpp/iterator/iterator), as that class doesn't really implement anything or even have an interface to override, it just have a few useful type aliases. – Some programmer dude Aug 17 '13 at 18:07
  • The type aliases are necessary to satisfy the Iterator Requirements. How are you gonna pass an iterator without those nested types to standard algorithms? – TemplateRex Aug 17 '13 at 18:11