<legend id='zaV6w'><style id='zaV6w'><dir id='zaV6w'><q id='zaV6w'></q></dir></style></legend>

    <i id='zaV6w'><tr id='zaV6w'><dt id='zaV6w'><q id='zaV6w'><span id='zaV6w'><b id='zaV6w'><form id='zaV6w'><ins id='zaV6w'></ins><ul id='zaV6w'></ul><sub id='zaV6w'></sub></form><legend id='zaV6w'></legend><bdo id='zaV6w'><pre id='zaV6w'><center id='zaV6w'></center></pre></bdo></b><th id='zaV6w'></th></span></q></dt></tr></i><div id='zaV6w'><tfoot id='zaV6w'></tfoot><dl id='zaV6w'><fieldset id='zaV6w'></fieldset></dl></div>

      • <bdo id='zaV6w'></bdo><ul id='zaV6w'></ul>

      <small id='zaV6w'></small><noframes id='zaV6w'>

    1. <tfoot id='zaV6w'></tfoot>

    2. 比较来自不同容器的迭代器

      comparing iterators from different containers(比较来自不同容器的迭代器)

    3. <i id='WRrVT'><tr id='WRrVT'><dt id='WRrVT'><q id='WRrVT'><span id='WRrVT'><b id='WRrVT'><form id='WRrVT'><ins id='WRrVT'></ins><ul id='WRrVT'></ul><sub id='WRrVT'></sub></form><legend id='WRrVT'></legend><bdo id='WRrVT'><pre id='WRrVT'><center id='WRrVT'></center></pre></bdo></b><th id='WRrVT'></th></span></q></dt></tr></i><div id='WRrVT'><tfoot id='WRrVT'></tfoot><dl id='WRrVT'><fieldset id='WRrVT'></fieldset></dl></div>
      <legend id='WRrVT'><style id='WRrVT'><dir id='WRrVT'><q id='WRrVT'></q></dir></style></legend>
        <tbody id='WRrVT'></tbody>

            <small id='WRrVT'></small><noframes id='WRrVT'>

              • <bdo id='WRrVT'></bdo><ul id='WRrVT'></ul>
                <tfoot id='WRrVT'></tfoot>
                本文介绍了比较来自不同容器的迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                比较来自不同容器的迭代器是否合法?

                std::vector富;std::vector酒吧;

                表达式 foo.begin() == bar.begin() 是否会产生错误或未定义的行为?

                (我正在编写一个自定义迭代器,在实现 operator== 时偶然发现了这个问题.)

                解决方案

                如果考虑 C++11 标准 (n3337):

                <块引用>

                §24.2.1 —[iterator.requirements.general#6]

                迭代器 j 被称为可从迭代器 i 到达当且仅当存在表达式 ++i 这使得 i == j.如果 j 可从 i 到达,则它们引用相同序列的元素.

                <块引用>

                §24.2.5 —[forward.iterators#2]

                前向迭代器的==域是相同底层序列上的迭代器的域.

                鉴于 RandomAccessIterator 必须满足 ForwardIterator 强加的所有要求,未定义比较来自不同容器的迭代器.

                LWG 问题 #446 专门讨论这个问题,建议在标准中添加以下文本(感谢 @Lightness在轨道上进行比赛以引起人们的注意):

                <块引用>

                直接或间接评估任何比较函数或二元运算符的结果,其中两个迭代器值作为从两个不同范围 r1 和 r2(包括它们的最后值)获得的参数 一个公共范围的子范围未定义,除非另有明确说明.

                Is it legal to compare iterators from different containers?

                std::vector<int> foo;
                std::vector<int> bar;
                

                Does the expression foo.begin() == bar.begin() yield false or undefined behavior?

                (I am writing a custom iterator and stumbled upon this question while implementing operator==.)

                解决方案

                If you consider the C++11 standard (n3337):

                § 24.2.1 — [iterator.requirements.general#6]

                An iterator j is called reachable from an iterator i if and only if there is a finite sequence of applications of the expression ++i that makes i == j. If j is reachable from i, they refer to elements of the same sequence.

                § 24.2.5 — [forward.iterators#2]

                The domain of == for forward iterators is that of iterators over the same underlying sequence.

                Given that RandomAccessIterator must satisfy all requirements imposed by ForwardIterator, comparing iterators from different containers is undefined.

                The LWG issue #446 talks specifically about this question, and the proposal was to add the following text to the standard (thanks to @Lightness Races in Orbit for bringing it to attention):

                The result of directly or indirectly evaluating any comparison function or the binary - operator with two iterator values as arguments that were obtained from two different ranges r1 and r2 (including their past-the-end values) which are not subranges of one common range is undefined, unless explicitly described otherwise.

                这篇关于比较来自不同容器的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                Adding types to the std namespace(将类型添加到 std 命名空间)
                Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)

                    • <legend id='TXqyt'><style id='TXqyt'><dir id='TXqyt'><q id='TXqyt'></q></dir></style></legend>

                        <tbody id='TXqyt'></tbody>
                      <i id='TXqyt'><tr id='TXqyt'><dt id='TXqyt'><q id='TXqyt'><span id='TXqyt'><b id='TXqyt'><form id='TXqyt'><ins id='TXqyt'></ins><ul id='TXqyt'></ul><sub id='TXqyt'></sub></form><legend id='TXqyt'></legend><bdo id='TXqyt'><pre id='TXqyt'><center id='TXqyt'></center></pre></bdo></b><th id='TXqyt'></th></span></q></dt></tr></i><div id='TXqyt'><tfoot id='TXqyt'></tfoot><dl id='TXqyt'><fieldset id='TXqyt'></fieldset></dl></div>
                    • <tfoot id='TXqyt'></tfoot>
                          <bdo id='TXqyt'></bdo><ul id='TXqyt'></ul>

                          <small id='TXqyt'></small><noframes id='TXqyt'>