首页 > 解决方案 > 模板类的 C++ for range 循环

问题描述

我在下面有一个模板类,它依赖于 2 个类UT 并且已经为这两个类实现了迭代器函数(见本文末尾)。

我可以使用迭代器迭代类的各个向量,
但我想知道是否可以使用 for range 循环语法而不是使用迭代器来做同样的事情。有类似的东西

for (auto x : myclass  <double>)
{
  std::cout << x << std::endl ; 
}

我知道它不起作用,但我无法找到语法(如果有的话),提前感谢您的回答

#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 ; 
}

标签: c++for-loopc++11templates

解决方案


您可以提供返回“范围”std::vector的函数(直接或包装器):

template <typename Z> // Or SFINAE or if constexpr or any other implementation
auto& getVector() { return std::get<std::vector<Z>&>(std::tie(m_vect_u, m_vect_t)); }

template <typename Z>
const auto& getVector() const { return std::get<const std::vector<Z>&>(std::tie(m_vect_u, m_vect_t)); }

接着

std::cout << "iteration over int" << std::endl ; 
for (auto e : myclass.getVector<int>())
{
    std::cout << e << std::endl ; 
}

演示


推荐阅读