比较迭代器,C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5842647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:00:31  来源:igfitidea点击:

Compare iterators, C++

c++iteratorcompare

提问by Johnas

Is it possible to compare two iterators? A comparision using std::min

是否可以比较两个迭代器?使用 std::min 的比较

void change ( typename TList <Item *>::Type ::iterator it_begin, typename TList <Item*>::Type ::iterator it_end )
{
   ....
this->items.resize ( index );
   std::sort ( it_begin, std::min (it_end, it_begin += index - 1); //Compare two iterators, exception
....
}

throws the following exception:

抛出以下异常:

Assertion failed: Vector iterators  incompatible... 

Is there any other way of the comparision?

有没有其他的比较方式?

回答by Nawaz

Yes. But I doubt if you can do that with std::min.

是的。但我怀疑你是否可以用std::min.

You can use std::distancefunction to calculate the distance between two iterators. And then you can use the distance to determine which iterator is the smaller one. Once you know the smaller iterator, you can pass that to std::sortfunction.

您可以使用std::distance函数来计算两个迭代器之间的距离。然后您可以使用距离来确定哪个迭代器较小。一旦知道较小的迭代器,就可以将其传递给std::sort函数。

Here is small illustration how to calculate distance:

这是如何计算距离的小插图

#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> v(100); //vector of size 100
    std::cout <<(std::distance(v.begin(), v.begin() + 10))<< std::endl;
    std::cout <<(std::distance(v.begin() +25, v.begin() +10))<< std::endl;
}

Output:

输出:

10
-15

Hope that gives you enough idea how to proceed to do what you want to.

希望能给你足够的想法如何继续做你想做的事。

回答by c-urchin

To answer the question, std::distance() can be used to measure the distance to the begin() iterator, and these distances can then be compared. However, as pointed out by Ben, there are other problems with your code. See http://www.cplusplus.com/reference/std/iterator/distance/

为了回答这个问题,可以使用 std::distance() 来测量到 begin() 迭代器的距离,然后可以比较这些距离。但是,正如 Ben 所指出的,您的代码还有其他问题。见http://www.cplusplus.com/reference/std/iterator/distance/

回答by user3731622

In the book C++ Primer 5th Ed. on p.111 section 3.4.2 Iterator Arithmetic says,

在 C++ Primer 5th Ed 一书中。在 p.111 部分 3.4.2 迭代器算术上说,

we can use == and != to compare to valid iterators into any of the library containers.

我们可以使用 == 和 != 将有效迭代器与任何库容器进行比较。

The section also tells us that iterators for string and vector support relational operators (aka iterator arithmetic) which include >, >=, <, <=.

该部分还告诉我们字符串和向量的迭代器支持关系运算符(又名迭代器算术),包括 >、>=、<、<=。

回答by Ben Voigt

After calling resize, all your existing iterators are invalid.

调用后resize,您现有的所有迭代器都无效。

Furthermore, that line invokes undefined behavior, since you're both changing it_beginand reading from it, in an undetermined order.

此外,该行调用未定义的行为,因为您正在it_begin以未确定的顺序更改和读取它。