如何使用 const InputIterators 为通用容器编写函数?

How to use const InputIterators to write a function for generic containers?

我正在尝试了解如何编写一个(独立的)函数,该函数采用 const InputIterators 并在迭代容器中的每个元素时做一些简单的事情。为简单起见,我正在考虑一个简单的打印函数,它将打印 [first..last) 之间范围内的所有元素,但不允许该函数修改容器中的元素。到目前为止,这是我得到的:

#include<iostream>
#include<vector>
#include<list>

using namespace std;

template<class InputIterator>
void print(const InputIterator& first, const InputIterator& last)
{
  while (first != last) {
    cout << *first << " ";
    ++first;
  }
  cout << endl;
}


int main()
{
  vector<int> v;
  v.push_back(1);
  v.push_back(3);
  v.push_back(5);
  v.push_back(7);

  list<float> l;
  l.push_back(11.1);
  l.push_back(33.3);
  l.push_back(55.7);
  l.push_back(77.7);

  print(v.cbegin(), v.cend());
  print(l.cbegin(), l.cend());    
}

当我尝试编译代码(使用带有 c++11 标志的 gcc4.7)时,出现以下错误:

iterator.cpp: In instantiation of 'void print(const InputIterator&, const InputIterator&) [with InputIterator = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >]':
iterator.cpp:32:29:   required from here
iterator.cpp:12:5: error: passing 'const __gnu_cxx::__normal_iterator<const int*, std::vector<int> >' as 'this' argument of '__gnu_cxx::__normal_iterator<_Iterator, _Container>& __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator++() [with _Iterator = const int*; _Container = std::vector<int>; __gnu_cxx::__normal_iterator<_Iterator, _Container> = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >]' discards qualifiers [-fpermissive]
iterator.cpp: In instantiation of 'void print(const InputIterator&, const InputIterator&) [with InputIterator = std::_List_const_iterator<float>]':
iterator.cpp:33:29:   required from here
iterator.cpp:12:5: error: passing 'const std::_List_const_iterator<float>' as 'this' argument of 'std::_List_const_iterator<_Tp>::_Self& std::_List_const_iterator<_Tp>::operator++() [with _Tp = float; std::_List_const_iterator<_Tp>::_Self = std::_List_const_iterator<float>]' discards qualifiers [-fpermissive]

我注意到,如果我删除了 const 要求,将 cbegincend 替换为 beginend,代码工作正常分别。有人可以指出我在这里遗漏了什么吗?

您需要按值传递您的迭代器:

template <class InputIterator>
void print(InputIterator first, InputIterator last)
{
    // as before
}

print 不会修改任何内容的合同是隐含的,您永远不会分配给 *first

do 需要修改迭代器本身(以实际进行任何迭代),这是您不能通过 const& 获取它们(否则,第一件事你必须再次复制它们) - 但修改迭代器与修改底层内容不同。

混淆似乎源于混合迭代器和迭代器指向的数据。

  • 迭代器可以改变,同时保持指向只读。

让我们将这些概念映射到原始指针:

int buffer[ARRAY_SIZE];

const int *ptr2c = &(buffer[0]);

int * const cptr = &(buffer[0]);

考虑使用这些指针中的任何一个遍历 'buffer' 的元素。使用 'ptr2c',应该能够遍历数组;无法通过此变量修改数组元素。

另一方面,'cptr',一个 const-pointer-to-int,允许您修改数组中指向的元素。但是,不能修改 cptr 本身以指向任何其他位置。

映射回您的示例,'const InputIterator' 使迭代器本身不可修改,这不是您想要的。这很粗糙,但是,您可以根据指针来形象化迭代器。当然,之后概念很快就不同了。