C++ iterator to const_iterator - c++

How do I acquire a const_iterator (of some container class) from an iterator (of that container class) in C++? What about a const_iterator from an insert_iterator? The resulting iterator should point at the same spot as the original does.

Containers are required to provide iterator as a type convertible to const_iterator, so you can convert implicitly:
Container::iterator it = /* blah */;
Container::const_iterator cit = it;
std::insert_iterators are output iterators. This gives no way to convert them to a regular Container::iterator which must be a forward iterator.
Another kind of insert iterator may allow such a thing, but those obtained from the standard functions don't.
I guess you can write your own wrapper around std::insert_iterator that exposes the protected member iter, though:
template <typename Container>
class exposing_insert_iterator : public std::insert_iterator<Container> {
public:
exposing_insert_iterator(std::insert_iterator<Container> it)
: std::insert_iterator<Container>(it) {}
typename Container::iterator get_iterator() const {
return std::insert_iterator<Container>::iter;
}
};
// ...
std::insert_iterator<Container> ins_it;
exposing_insert_iterator<Container> exp_it = ins_it;
Container::iterator it = exp_it.get_iterator();

You can convert them. Example:
std::vector<int> v;
std::vector<int>::iterator it = v.begin();
std::vector<int>::const_iterator cit = it;
But I guess that is not the answer you are seeking. Show me code. :-)

Related

Custom container traversal with range-based for loop

In C++, some STL containers like vector, map, string can be traversed by for loops with colon in it.
for instance:
for(auto c:v)
When I'm writing a custom container, can I make it traversed that way like Java (which only need to implement Iterable)?
Yes, you need to implement some form of iterator and override std::begin(container) and std::end(container) (might work also if you container has begin and end methods).
Internally the code is equivalent to something like the following (this is just to get the point across, the compiler can write it slightly differently, see here for more details).
auto _end = end(v);
for (auto _it = begin(v); _it != _end; ++_it) {
auto c = *_it;
<the rest of loop code>
}
So if your iterator and overrides work as expected it will work for the for loop as well.
You can have a following simple equivalent to Java Iterable interface:
template <typename T, typename U>
struct iterable {
T _begin;
U _end;
iterable(T begin, U end)
: _begin(begin),
_end(end)
{}
T begin() {
return _begin;
}
U end() {
return _end;
}
};
If you wonder why there are T and U when the begin iterator and end iterator should be the same. The reason is that some containers don't have those two iterators of the same type.
Furthermore, you can implement a helper function make_iterable like:
template <typename T, typename U>
iterable<T, U> make_iterable(T t, U u) {
return iterable<T,U>(t, u);
}

How to implement standard iterators in class

I have classes which are usually using standard containers as underlying fields. For example, I have a class
template <typename T>
class Vec_3D
{
public:
/* ... */
std::array<T, 3> vec;
/* ... */
};
which has only one variable vec and the rest are just functions I need when working with vectors. I want to be able to use range-based for loop such as
Vec_3D<double> vec;
for (double val : vec) {/*...*/}
which should obviusly iterate over std::array<double, 3>.
How to implement iterators in my class which should in turn call iterators of std::array<T, 3>?
I started with this question and tried to define iterators in my class as
typedef std::iterator<std::random_access_iterator_tag, T, ptrdiff_t, T*, T&> iterator;
typedef std::iterator<std::random_access_iterator_tag, const T, ptrdiff_t, const T*, const T&> const_iterator;
inline iterator begin() noexcept { return vec.begin(); }
inline const_iterator cbegin() const noexcept { return vec.cbegin(); }
inline iterator end() noexcept { return vec.end(); }
inline const_iterator cend() const noexcept { return vec.end(); }
but got compiling errors
error: no match for ‘operator!=’ (operand types are ‘Vec_3D<double>::iterator {aka std::iterator<std::random_access_iterator_tag, double, long int, double*, double&>}’ and ‘Vec_3D<double>::iterator {aka std::iterator<std::random_access_iterator_tag, double, long int, double*, double&>}’)
and operator++, operator*
std::iterator is (was) a helper type to define the typedefs that a typical iterator requires. These typedefs within the class in turn make std::iterator_traits work with your iterator.
It does not, however, actually implement the required operations for you.
It was deprecated, because the std committee didn't like specifying that standard iterators had to have those typedefs, and writing the typedefs was not much bulkier than figuring out what arguments to pass to the std::iterator template.
The easy thing to do here is to just steal your underlying container's iterator. This makes your abstraction leak, but it is efficient and easy.
template <typename T>
struct Vec_3D {
using container=std::array<T, 3>;
using iterator=typename container::iterator;
using const_iterator=typename container::const_iterator;
iterator begin() { return vec.begin(); }
iterator end() { return vec.end(); }
const_iterator begin() const { return vec.begin(); }
const_iterator end() const { return vec.end(); }
private:
/* ... */
container vec;
/* ... */
};
If you don't want to expose your underlying container type, if you are willing to guarantee your underlying container is a contiguous buffer you can do:
template <typename T>
struct Vec_3D {
using iterator=T*;
using const_iterator=T const*;
iterator begin() { return vec.data(); }
iterator end() { return vec.data()+vec.size(); }
const_iterator begin() const { return vec.data(); }
const_iterator end() const { return vec.data()+vec.size(); }
private:
/* ... */
std::array<T,3> vec;
/* ... */
};
as pointers are valid iterators.
If you find you are writing this "I am a modified container" boilerplate too much, you can automate it:
template<class Container>
struct container_wrapper {
using container=Container;
using iterator=typename container::iterator;
using const_iterator=typename container::const_iterator;
iterator begin() { return m_data.begin(); }
iterator end() { return m_data.end(); }
const_iterator begin() const { return m_data.begin(); }
const_iterator end() const { return m_data.end(); }
protected:
Container m_data;
};
and then
template <typename T>
class Vec_3D:private container_wrapper<std::array<T,3>> {
// ...
};
but even that might be a bit much, why not just:
template <typename T>
class Vec_3D:public std::array<T,3> {
// ...
};
It is true that deleting Vec_3D through a pointer to base is undefined behavior, but who deletes pointers to standard containers?
If this worries you:
template <typename T>
class Vec_3D: private std::array<T,3> {
using container = std::array<T,3>;
using container::begin();
using container::end();
// ...
};
lets you inherit privately, then bring certain operations back into scope.
A range-based for loop only requires that your class have begin() and end() methods (or overloads of std::begin() and std::end()) that return iterators. It doesn't care where those iterators come from. So, the simplest solution is to just use the array's own iterators instead of trying to define your own, eg:
template <typename T>
class Vec_3D
{
public:
typedef typename std::array<T, 3> array_type;
typedef typename array_type::iterator iterator;
typedef typename array_type::const_iterator const_iterator;
// or:
// using array_type = std::array<T, 3>;
// using iterator = array_type::iterator;
// using const_iterator = array_type::const_iterator;
...
inline iterator begin() noexcept { return vec.begin(); }
inline const_iterator cbegin() const noexcept { return vec.cbegin(); }
inline iterator end() noexcept { return vec.end(); }
inline const_iterator cend() const noexcept { return vec.cend(); }
...
private:
array_type vec;
};
std::iterator is a base class only, its basically a container for some traits, but if you wanted to use it to implement your own iterator class you'd need to derive from it.
However you don't need to use it, there was a proposal to deprecate it, you could just define those traits directly in an iterator that you write. The following question has info on the proposal and help with implementing an iterator class:-
Preparation for std::iterator Being Deprecated
At the moment you're defining your container's iterator types using that base, not a class that can actually do any iterating, which is why it fails.
You expose the array as a public member. If you're happy to expose that your vec_3d is implemented using an array (whether you continue to expose the member array publicly or not) then you could just use the array's iterators - its not clear from the question that your iterator needs any bespoke behaviour just because your container adds some functionality.

how to define and access custom iterator

I am trying to make a custom iterator and I am struggling with syntax part of how to define/declare and eventually access it. Below is my attempt, which results in below error:
Error C2440 'initializing': cannot convert from 'int' to 'int *'
If someone can point me to right way to define/declare it - it would be great, for I believe my access method is standard.
Declaration
template <typename T>
class ddeque
{
public:
typedef T* iterator;
T& begin();
T& end();
}
Definition
template<typename T>
T& ddeque<T>::begin()
{
iterator = &(this->unified_array());
return iterator;
}
template<typename T>
T& ddeque<T>::end()
{
iterator = &(this->unified_array + this->size());
return iterator;
}
Access part --
In test.cpp file
// Comparing values from custom template with standard template
typename ddeque<T>::iterator itt = a.begin();
typename deque<T>::iterator ittg = g.begin();
while ((itt != a.end()) && (ittg != g.end()))
{
if (display)
{
cout << *itt << " ";
}
++itt;
++ittg;
}
P.S : I have just kept relevant part of iterator - please let me know if additional code snippet is required.
As pointed by SO member АндрейБеньковский I was returning reference instead of pointer- correct solution for definition would be ( what got it working )
Declaration:
template <typename T>
class ddeque
{
public:
typedef T* iterator;
iterator begin();
iterator end();
}
Definition
template
T* ddeque::begin()
{
iterator = &(this->unified_array());
return iterator;
}
template<typename T>
T* ddeque<T>::end()
{
iterator = &(this->unified_array + this->size());
return iterator;
}
In general:
how to define and access custom iterator
You should make a contaner class and an iterator class. The latter should be derived from std::iterator<>. You have to specify iterator tag, return value, ect. Depending on the iterator tag, you should be implementing the operators (increment, decrement, ect.). (Or better, implement everything you can which has a maximum of logarithmic complexity, then set best iterator tag.)
So begin, and end should be returning your iterator. Take a look at some stl container implementations, they are not so hard to read.
In your code
ou have made a typdef T* iterator;. This line makes another name for the pointer to T type. For readability, and to make it easier to change later, you should be returning iterator with begin and end, so:
iterator begin();
iterator end();
In the definition of the above functions, you cannot assign a value to the iterator type, I suggest returning the pointer (or iterator), for example in end():
return this->unified_array + this->size();
I think unified_array is a pointer, so I used it that way.

Declare a container const or non-const depending on iterator

Suppose that I have the following function:
template <typename Iterator>
void f()
{
std::list<int> numList;
Iterator it = numList.begin();
...
}
Iterator can be either std::list<int>::iterator or std::list<int>::const_iterator.
The above function compiles for std::list<int>::iterator, but for std::list<int>::const_iterator the list should be declared as const std::list<int>.
Is there a way to declare the type of the list either std::list<int> or const std::list<int> depending on whether Iterator is iterator or const_iterator?
It should be possible by using std::iterator_traits, <type_traits> and std::conditional, something similar to
using pointer_type = typename std::iterator_traits<Iterator>::pointer;
using list_type = typename std::conditional<std::is_const<pointer_type>::value, const std::list<int>, std::list<int>>::type;
list_type numList;
Mind this is untested so you should probably adjust it a little bit, it's just to give you the basic idea behind this. Please check this answer too.

Implementing own begin() and end()

I've got this template:
template<typename C,class F>
class filtered_cont{
C& container;
F filter;
class const_iterator{
//my iterator implementation
}
const_iterator begin(){
//...
}
}
C- container and F is filter. I'd like to ask how to implement my own begin() method that it will return first valid element. I got something like this, but I get errors
const_iterator begin() const{
for (const_iterator it=cont.begin(); it!=const_iterator(); it++) {
if(pred(*it)) return it;
}
return const_iterator();
}
I think the wrong part is it=cont.begin(), but I don't really know how to get begin() iterator.
Error:
no viable conversion from 'iterator' (aka '__list_iterator<value_type, __void_pointer>') to 'filter_view<std::__1::list<unsigned char,
std::__1::allocator<unsigned char> >, is_even>::const_iterator
You'll need to use the container's iterator, not your own. Something like this:
const_iterator begin() const{
for (typename C::const_iterator it=cont.begin(); it!=cont.end(); it++) {
if(pred(*it)) return const_iterator(it);
}
return const_iterator();
}
The error message states what is missing: your const_iterator needs an [implicit] constructor from typename Container::const_iterator:
template <typename C, typename F>
class filtered_cont<C, F>::const_iterator {
public:
const_iterator(typename C::const_iterator it);
// ...
};
If you change your initialization slightly, you can make the conversion explicit which is probably a good idea:
for (const_iterator it(cont.begin()); ... ) {
...
}
Personally, I would expect to use the filtering logic to be contained by the iterator as it needs to have this logic anyway, e.g., when moving to the next element. The logic itself should probably be
std::find_if(it, cont.end(), filter);
Your begin function looks fine (in pri).
Instead of defining a const_iterator type, you may want to define a iterator type, and then define the const_iterator type as the const version of it.
But, regardless, your class will need to provide a way to get from an arbitrary input iterator, to the desired type of iterator. That means you needs constructor, and a general approach. I am not certain how one should do that, but I would guess your own iterator stores the incoming iterator (in a C::iterator field), then passes on any calls to it.