Compare vectorlt;Tgt;::iterator with vectorlt;Tgt;::reverse_iterator(比较向量lt;Tgt;::iterator 和向量lt;Tgt;::reverse_iterator)
问题描述
我正在做一个练习,我有一个向量,我正在编写自己的反向算法,方法是使用反向和正常(正向)迭代器来反转向量的内容.但是,我无法比较迭代器.
I am working on an exercise where I have a vector and I am writing my own reverse algorithm by using a reverse and a normal (forward) iterator to reverse the content of the vector. However, I am not able to compare the iterators.
int vals[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
vector<int> numbers(vals, vals + 10);
vector<int>::iterator start = numbers.begin();
vector<int>::reverse_iterator end = numbers.rend();
我有一个先前的算法,用于通过使用两个迭代器来反转向量,但是在这个任务中,我无法使用它们之间的 != 运算符来比较它们.我的猜测是获取向量中的底层指针或索引,但我如何获取指针/索引?
I have a previous algorithm for reversing the vector by using two iterators, however in this task I am not able to compare them using the != operator between them. My guess would be to get the underlying pointers or indexes in the vector with each other but how do I get the pointers/index?
推荐答案
使用 base()
: it == rit.base() - 1
.
这篇关于比较向量<T>::iterator 和向量<T>::reverse_iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:比较向量<T>::iterator 和向量<T>::reverse_iterator


- XML Schema 到 C++ 类 2022-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 将 hdc 内容复制到位图 2022-09-04
- 如何提取 __VA_ARGS__? 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- GDB 不显示函数名 2022-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
- DoEvents 等效于 C++? 2021-01-01