简体   繁体   English

如何比较 c++ 中的两个 map 迭代器?

[英]How to compare two map iterator in c++?

#include <iostream>
#include<vector>
#include<map>
using namespace std;
int main() {
   vector<int> v1{1,2,3,4,5};
    auto it1 = v1.begin();
    auto it2 = v1.end();
    if(it1<it2){
        cout<<"TRUE"<<endl;             // Output: TRUE
    }

    map<int,int> m;
    m.insert(make_pair(1,2));
    m.insert(make_pair(5,7));
    auto it3 = m.begin();
    auto it4 = m.end();
    if(it3<it4){
        cout<<"TRUE"<<endl;           
    }   
    /*
    error: no match for 'operator<' (operand types are 'std::_Rb_tree_iterator<std::pair<const int, int> >' and 'std::_Rb_tree_iterator<std::pair<const int, int> >')
   18 |     if(it3<it4){
      |        ~~~^~~~
    */

}

Line (it1<it2) works fine when using vector but (it3<it4) does not work when using map?线 (it1<it2) 在使用矢量时工作正常,但 (it3<it4) 在使用 map 时不起作用? Please explain this concept.请解释一下这个概念。

Line (it1<it2) works fine when using vector but (it3<it4)线 (it1<it2) 使用矢量时工作正常,但 (it3<it4)

Vector iterators are random access iterators.向量迭代器是随机访问迭代器。 Random access iterators can be compared for order.可以比较随机访问迭代器的顺序。

but (it3<it4) does not work when using map?但是(it3<it4)在使用 map 时不起作用?

Map iterators are not random access iterators. Map 迭代器不是随机访问迭代器。 They cannot be compared for order.它们无法按顺序进行比较。

However, if you know the comparator of the map, then you can indirect through both iterators, and compare the keys with the comparator.但是,如果您知道 map 的比较器,那么您可以间接通过两个迭代器,并将键与比较器进行比较。 You must check for end iterator first since you cannot get a key from it (but you do know that it's after all other iterators).您必须首先检查结束迭代器,因为您无法从中获取密钥(但您确实知道它在所有其他迭代器之后)。 This only works with ordered maps such as std::map .这仅适用于有序映射,例如std::map It does not work with hashmaps such as std::unordered_map .它不适用于诸如std::unordered_map之类的哈希图。 It also won't find out the relative order of two iterators to identical elements in a multi map.它也不会找出两个迭代器与多 map 中相同元素的相对顺序。

In general, given a pair of forward iterators and the end iterator, you can find out the pair of iterators by doing linear search starting from one iterator, until you find either second iterator or the end of the container.一般来说,给定一对前向迭代器和结束迭代器,您可以通过从一个迭代器开始进行线性搜索来找到这对迭代器,直到找到第二个迭代器或容器的末尾。 If you find end before the other iterator, then other is first;如果你在另一个迭代器之前找到 end,那么 other 是第一个; If you do find the other iterator then it's after.如果您确实找到了另一个迭代器,那么它就在之后。 This of course has linear worst case complexity.这当然具有线性的最坏情况复杂性。

See here for an overview of iterator categories: https://en.cppreference.com/w/cpp/iterator .有关迭代器类别的概述,请参见此处: https://en.cppreference.com/w/cpp/iterator

std::map has bidirectional iterators while std::vector has randomaccess iterators. std::map具有双向迭代器,而std::vector具有随机访问迭代器。 The latter can be comapred via < , the first not.后者可以通过<进行比较,第一个不能。

If you want to know which element comes first in the map, you can use the fact that a std::map s elements are sorted with respect to keys:如果您想知道 map 中哪个元素首先出现,您可以使用std::map的元素相对于键进行排序的事实:

if (it3->first < it4->first) { ...

In general you need to take into account that the map might use a custom comparator, hence the more generic way would be一般来说,您需要考虑到 map 可能使用自定义比较器,因此更通用的方法是

if ( m.key_comp()(it3->first,it4->first) ) { ...

However, either way will fail when one of the iterators equals the end of the map, because you cannot dereference it to get a key.但是,当其中一个迭代器等于 map 的end时,任何一种方式都会失败,因为您无法取消引用它来获取密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM