I have a template class below that depends on 2 classes U
and T
and have implemented the iterator function for both classes (see the end of this post).
I can iterate over the respective vectors of the class using iterators,
but I was wondering if it was possible to do the same using the for range loop syntax instead of using iterators. With something like
for (auto x : myclass <double>)
{
std::cout << x << std::endl ;
}
I know it does not work but I could not manage to find the syntax if any, Thanks in advance for your answer
#include<iostream>
#include<string>
#include<vector>
template<class U,class T>
class MyClass
{
public:
MyClass(
const std::vector<U> & vect_u ,
const std::vector<T> & vect_t )
{
m_vect_u = vect_u;
m_vect_t = vect_t;
}
~MyClass(){}
// begin()
template<class Z>
typename std::enable_if<std::is_same<Z,T>::value, typename std::vector<Z>::iterator>::type
begin() noexcept { return m_vect_t.begin(); }
template<class Z>
typename std::enable_if<std::is_same<Z,U>::value, typename std::vector<Z>::iterator>::type
begin() noexcept { return m_vect_u.begin(); }
// end()
template<class Z>
typename std::enable_if<std::is_same<Z,T>::value, typename std::vector<Z>::iterator>::type
end() noexcept { return m_vect_t.end(); }
template<class Z>
typename std::enable_if<std::is_same<Z,U>::value, typename std::vector<Z>::iterator>::type
end() noexcept { return m_vect_u.end(); }
// cbegin()
template<class Z>
typename std::enable_if<std::is_same<Z,T>::value, typename std::vector<Z>::const_iterator>::type
cbegin() const noexcept { return m_vect_t.cbegin(); }
template<class Z>
typename std::enable_if<std::is_same<Z,U>::value, typename std::vector<Z>::const_iterator>::type
cbegin() const noexcept { return m_vect_u.cbegin(); }
// cend()
template<class Z>
typename std::enable_if<std::is_same<Z,T>::value, typename std::vector<Z>::const_iterator>::type
cend() const noexcept { return m_vect_t.cend(); }
template<class Z>
typename std::enable_if<std::is_same<Z,U>::value, typename std::vector<Z>::const_iterator>::type
cend() const noexcept { return m_vect_u.cend(); }
private:
std::vector<U> m_vect_u ;
std::vector<T> m_vect_t ;
};
int main()
{
std::vector<double> vect_double = {1.5,2.5,3.5} ;
std::vector<int> vect_int = {-1,-2,-3} ;
MyClass<double,int> myclass( vect_double, vect_int);
std::cout << "iteration over int" << std::endl ;
for(auto itr = myclass.begin<int>(); itr < myclass.end<int>() ; ++itr)
{
std::cout << *itr << std::endl ;
}
std::cout << "iteration over double" << std::endl ;
for(auto itr = myclass.begin<double>(); itr < myclass.end<double>() ; ++itr)
{
std::cout << *itr << std::endl ;
}
return 0 ;
}
Aucun commentaire:
Enregistrer un commentaire