比较C++中的迭代器
comparing iterators in C++
我对比较C++中的迭代器感到困惑。带有以下代码:
std::iterator< std::forward_iterator_tag, CP_FileSystemSpec> Iter1;
std::iterator< std::forward_iterator_tag, CP_FileSystemSpec> Iter2;
while( ++Iter1 != Iter2 )
{
}
错误为:
error: no match for 'operator++' in '++Iter1'
我似乎还记得,你不能做上面代码正在做的事情。但我不太知道如何进行比较。
std::iterator
本身不是迭代器,而是其他迭代器可以继承的基类,以获得一些标准的typedef。
template<class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&>
struct iterator
{
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
typedef Category iterator_category;
};
这个错误与比较无关——它告诉您特定的迭代器不支持递增。
您应该从std::iterator
派生——直接实例化它毫无意义。
要使该示例工作,请使用由序列支持的迭代器,例如vector
:
std::vector<int> foo(10); // 10 times 0
std::vector<int>::iterator it1 = foo.begin();
std::vector<int>::iterator it2 = foo.end();
while(++it1 != it2) {
// do stuff
}
请注意,这不是迭代集合的规范方式。它也很棘手,因为它跳过了序列的第一个元素。使用此:
for(std::vector<int>::iterator it = foo.begin(); it != foo.end(); it++) {
// do stuff
}
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- 为什么output_editor Concept不需要output_e迭代器标记
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用迭代器时如何访问对象在向量中的位置?
- std::vector::迭代器是否可以合法地作为指针
- 跟随整数索引列表的自定义类迭代器
- 不明白迭代器,引用和指针失效,一个例子
- 我可以使用反向迭代器作为ForwardIt吗
- ESP8266单片机矢量迭代器的C++问题
- 如何在C++中将迭代器作为函数参数传递
- 是否应避免从非常量迭代器转换为常量迭代器?
- 如何在 c++ 中将字符串迭代器变量传递给函数?
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 为什么 C++ std::unordered_map 从 emplace/ 找到返回一个迭代器?