-
October 13th, 2013, 04:15 AM
#1
STL algorithm adapter
Hello,
An external library has the following header file;
Code:
template <typename T>
class MyCollection
{
public:
MyCollection();
const T& operator[](unsigned long index) const;
unsigned long length() const;
private:
std::vector<void*> v;
};
To iterate this collection I can obviously use a loop. However, would it be possible to use STL algorithms functions on this collection? A lot of those methods use an iterator as input parameter, would be possible to make an adapter class to make this work?
Before post, make an effort yourself, try googling or search here.
When posting, give a proper description of your problem, include code* and error messages.
*All code should include code tags
-
October 13th, 2013, 05:39 AM
#2
Re: STL algorithm adapter
Well, it may be easiest if you can convince the library author to augment the class template with iterator support. If that cannot be done, then one way out is to write your own iterator class template, e.g.,
Code:
template <typename T>
class MyCollectionIterator : public std::iterator<std::input_iterator_tag, T>
{
public:
explicit MyCollectionIterator(MyCollection<T>& collection_, unsigned long index_=0) :
collection(&collection_), index(index_) {}
const T& operator*() const
{
return (*collection)[index];
}
const T* operator->() const
{
return &(operator*());
}
MyCollectionIterator<T>& operator++()
{
++index;
return *this;
}
bool operator==(const MyCollectionIterator<T>& other) const
{
return index == other.index && collection == other.collection;
}
private:
MyCollection<T>* collection;
unsigned long index;
};
template <typename T>
MyCollectionIterator<T> operator++(MyCollectionIterator<T>& iterator, int)
{
MyCollectionIterator<T> temp(iterator);
++iterator;
return temp;
}
template <typename T>
bool operator!=(const MyCollectionIterator<T>& lhs, const MyCollectionIterator<T>& rhs)
{
return !(lhs == rhs);
}
template <typename T>
MyCollectionIterator<T> begin(MyCollection<T>& collection)
{
return MyCollectionIterator<T>(collection);
}
template <typename T>
MyCollectionIterator<T> end(MyCollection<T>& collection)
{
return MyCollectionIterator<T>(collection, collection.length());
}
// Example usage:
int main()
{
MyCollection<int> collection;
// ...
std::copy(begin(collection), end(collection),
std::ostream_iterator<int>(std::cout, "\n"));
}
Given the interface of MyCollection, you probably want a random access iterator, not an input iterator, but I simplified for this example. Furthermore, it looks as if the MyCollection class template actually is an immutable type, in which case perhaps it would be simpler to just provide a const_iterator version of the iterator instead of both versions.
Last edited by laserlight; October 13th, 2013 at 05:59 AM.
-
October 13th, 2013, 09:44 AM
#3
Re: STL algorithm adapter
Thank you.
That looks very interesting.
Will study it in more detail the coming week.
Before post, make an effort yourself, try googling or search here.
When posting, give a proper description of your problem, include code* and error messages.
*All code should include code tags
-
October 14th, 2013, 02:02 AM
#4
Re: STL algorithm adapter
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|