STL iterators and templates [closed]?

If you really want to be able to declare the template dependent only in the type of the data contained in the container, and at the same time you want to be able to store any type of iterator (i.e. Iterator into any type of container), the solution is performing type erasure on the iterators. If you can bring some external library, that has been done in adobe libs as any_iterator.

Basically you define a base class that defines the iterator interface that you need with dynamic polymorphism (i.e. Virtual methods), and then implement that interface with a template that takes the appropriate type: template struct any_iterator { // all typedefs for an iterator here, including: typedef T value_type; virtual ~any_iterator() {} virtual any_iterator& operator++() = 0; virtual value_type& operator*() = 0; // ... rest of the methods }; template class any_iterator_impl : any_iterator { iterator it; public: // all the typedefs typedef Iterator iterator; typedef typename iterator::value_type value_type; // actual implementation of the interface any_iterator_impl( Iterator it ) : it(it) {} virtual any_iterator_impl& operator++() { ++it; return *this; } virtual value_type& operator*() { return *it; } // ... and all the rest of the interface }; And then use that in your class: template class Foo { std::unique_ptr it, end; // in real code use smart pointers here public: template Foo( Iterator b, Iterator e ) { static_assert( is_same::value ); it = new any_iterator_impl( begin ); end = new any_iterator_impl( end ); } // rest of the class }; The code snippets are only for exhibition, they are not production code, have not been compiled and have a fair amount of don't do's (raw pointers, iterators can become invalidated at any point during the lifetime of the object...), but is intended to give you an idea of the work that you would need to perform type erasure on the iterators. Then again, take a look at the linked adobe library for a more thought out proposal.

If you are curious about it, it is the same approach taken in std::function or boost::any. One of the advantages is that you can actually use the same Foo class with different families of iterators in different situations. On the other hands, it requires dynamic dispatch, but that should not be problematic in most situations.

That is, if you were store an iterator in your class at all. Most of the time, you would not. Aside from the fact that in many cases an iterator can become invalidated at least-expected times, it's just a clumsy design.

Your code doesn't seem to make much sense. I'd tend to agree with GMan that what you're trying to do probably isn't useful. There are, however, cases when vaguely similar things make sense.

When they do, you typically use the iterator type as the template argument: template class Foo { inIt begin; inIt end; public: Foo(inIt b, inIt e) : begin(b), end(e) {} bool find(typename inIt::value_type v) { while (b! =e) { if (*b == v) return true; ++b; } return false; } }; This does depend on the iterator type containing a typedef for its value_type, which is true of the iterators for standard containers like vector and list. You can (could) write your own iterators that don't do that though, which would make code like this next to impossible (which, of course, is why the standard library works the way it does).

The STL implements five different types of iterators. These are input iterators (that can only be used to read a sequence of values), output iterators (that can only be used to write a sequence of values), forward iterators (that can be read, written to, and move forward), bidirectional iterators (that are like forward iterators, but can also move backwards) and random access iterators (that can move freely any number of steps in one operation). It is possible to have bidirectional iterators act like random access iterators, as moving forward ten steps could be done by simply moving forward a step at a time a total of ten times.

However, having distinct random access iterators offers efficiency advantages. For example, a vector would have a random access iterator, but a list only a bidirectional iterator. Iterators are the major feature that allow the generality of the STL.

For example, an algorithm to reverse a sequence can be implemented using bidirectional iterators, and then the same implementation can be used on lists, vectors and deques. User-created containers only have to provide an iterator that implements one of the five standard iterator interfaces, and all the algorithms provided in the STL can be used on the container. This generality also comes at a price at times.

For example, performing a search on an associative container such as a map or set can be much slower using iterators than by calling member functions offered by the container itself. This is because an associative container's methods can take advantage of knowledge of the internal structure, which is opaque to algorithms using iterators. A large number of algorithms to perform activities such as searching and sorting are provided in the STL, each implemented to require a certain level of iterator (and therefore will work on any container that provides an interface by iterators).

Searching algorithms like binary_search and lower_bound use binary search and like sorting algorithms require that the type of data must implement comparison operator < or custom comparator function must be specified; such comparison operator or comparator function must guarantee strict weak ordering. Apart from these, algorithms are provided for making heap from a range of elements, generating lexicographically ordered permutations of a range of elements, merge sorted ranges and perform union, intersection, difference of sorted ranges.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions