| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
custom iterator
I have a class which holds some objects.
To be more specific the class MyClass has an array of 9 "Obj". like this: Code:
#include"Obj.h"
class MyClass
{
Obj _objects[9];
}
http://www.cplusplus.com/reference/s...erator_traits/ According to this, I would try to create a class that inherits from iterator_traits<Obj>, but at the same time, a want to write "typedef Obj* iterator" How should I do this? |
|
#2
|
|||
|
|||
|
Re: custom iterator
Apparently, you're talking about two related but different things.
Providing access to your internal array through an iterator style is simple. What I usually do is to return a pair with the begin and end iterators. Since you're using a C array your iterators are raw pointers. That's is fine because raw pointers model the STL iterators concepts. (I just suggest you to take a look at std::vector or boost::array). Code:
#include <utility>
#include <iostream>
typedef int Obj;
class MyClass
{
public:
typedef Obj const* const_iterator;
std::pair<const_iterator, const_iterator> get_objects() const
{ return std::make_pair(objects_, objects_ + 9); }
private:
Obj objects_[9]; //I preffer using a trailling underscore. Leading underscores are reserved in several cases.
};
int main()
{
MyClass m;
typedef MyClass::const_iterator const_iterator;
std::pair<const_iterator, const_iterator> ci = m.get_objects();
for (; ci.first != ci.second; ++ci.first)
std::cout << *(ci.first) << " ";
std::cout << std::endl;
return 0;
}
Does that help you? |
|
#3
|
|||
|
|||
|
Re: custom iterator
Quote:
Are pointers compatible with all stl algorithms? I will also need to create an iterator class for another class of mine. For reasons I will not dwell upon, it needs a random access iterator that returns only the even elements of my array. so it++ will actually increment the internal pointer by two, or it+=4 will increment the internal pointer by four. according to http://www.cplusplus.com/reference/s...erator_traits/ , since my iterator is a pointer type, I shouldn't need to redefine all those types. I just have to redefine the operators. will this work, or how should it be done? Code:
class iterator : public std::iterator_traits<obj>
{
public:
iterator& operator++();
etc...
private:
obj* _internalPointer;
}
|
|
#4
|
|||
|
|||
|
Re: custom iterator
The most powerfull iterator concept is Random Access Iterator. It refines Bidirectional Iterator, which then refines Forward Iterator, and this goes on...
If you take a look here you'll notice that raw pointers are models of Random Access Iterators. So you can assume they work with all STL algorithms. Quote:
When writing your very_peculiar_iterator class you have two options. The first (which is usually the best when you control the implementation of the iterator class) is to place nested types inside it (using typedefs) for everything that is expected by std::iterator_traits. In this approach, std::iterator_traits is naturally instantiated with your type. For example: Code:
class very_peculiar_iterator
{
public:
typedef something value_type;
typedef something* pointer;
//Other required types...
private:
//Implementation...
};
Code:
class very_peculiar_iterator
{
private:
//Implementation...
};
template <>
struct iterator_traits<very_peculiar_iterator>
{
typedef typename very_peculiar_iterator::something value_type;
typedef typename very_peculiar_iterator::something* pointer;
//Other required types...
};
|
|
#5
|
|||
|
|||
|
Re: custom iterator
Quote:
I just wasn't sure how to implement an iterator and/or traits, or if a pointer can be used in any algorithm. After all, std::distance, for example, uses iterator traits to choose how to work. pointers don't define iterator traits typedefs, so I thought that might pose a problem. I understand you can re-define all the typdefs yourself, or just do a total template specialisation for your iterator. But why can't you inherit? After all, isn't that how you implement a binary function, you inherit from binary_function<T,U>, you don't redefine the typedefs manually? Thank you very much for your input. It helps a lot. I don't want to make it look like I don't agree or apreciate your help. I'm just the kind of person who likes to understand how things work. I want more than the solution
|
|
#6
|
||||
|
||||
|
Re: custom iterator
Quote:
__________________
Cheers, D Drmmr Please put [code][/code] tags around your code to preserve indentation and make it more readable. As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky |
|
#7
|
|||
|
|||
|
Re: custom iterator
Quote:
|
|
#8
|
|||||
|
|||||
|
Re: custom iterator
Quote:
Quote:
Quote:
Quote:
Quote:
|
|
#9
|
|||
|
|||
|
Re: custom iterator
Thank you very much ltcmelo. I haven't had time to read all the documentation, or look at the links, so I'll just sit back and take my time, try things out, and see if I understand everything or not. I'll come back later if I still have questions
I must admit I did not fully understand the page I was linking to. However, it is clearer now. Thanks. |
|
#10
|
||||
|
||||
|
Re: custom iterator
Quote:
I understood monarch_dodra's remark as wanting to inherit his iterator class, rather than providing typedefs or specializing std::iterator_traits.
__________________
Cheers, D Drmmr Please put [code][/code] tags around your code to preserve indentation and make it more readable. As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky |
|
#11
|
|||
|
|||
|
Re: custom iterator
Quote:
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|