将默认构造的迭代器与运算符==进行比较

发布于 2024-07-29 10:41:20 字数 479 浏览 3 评论 0原文

C++ 标准是否规定我应该能够比较两个默认构造的 STL 迭代器是否相等? 默认构造的迭代器是否可相等比较?

我想要以下内容,例如使用 std::list:

void foo(const std::list<int>::iterator iter) {
    if (iter == std::list<int>::iterator()) {
        // Something
    }
}

std::list<int>::iterator i;
foo(i);

我在这里想要的是类似迭代器的 NULL 值,但我不确定它是否合法。 在 Visual Studio 2008 附带的 STL 实现中,它们在 std::list 的运算符 ==() 中包含断言,以排除这种用法。 (他们检查每个迭代器是否由同一个容器“拥有”,并且默认构造的迭代器没有容器。)这将暗示它不合法,或者可能他们过于热心。

Does the C++ Standard say I should be able to compare two default-constructed STL iterators for equality? Are default-constructed iterators equality-comparable?

I want the following, using std::list for example:

void foo(const std::list<int>::iterator iter) {
    if (iter == std::list<int>::iterator()) {
        // Something
    }
}

std::list<int>::iterator i;
foo(i);

What I want here is something like a NULL value for iterators, but I'm not sure if it's legal. In the STL implementation included with Visual Studio 2008, they include assertions in std::list's operator==() that preclude this usage. (They check that each iterator is "owned" by the same container and default-constructed iterators have no container.) This would hint that it's not legal, or perhaps that they're being over-zealous.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

同展鸳鸯锦 2024-08-05 10:41:39

规范说默认构造函数的后置条件是迭代器是单数。 相等的比较未定义,因此在某些实现中可能会有所不同。

Specification says that the postcondition of default constructor is that iterator is singular. The comparison for equality are undefined, so it may be different in some implementation.

韶华倾负 2024-08-05 10:41:37

我相信您应该将范围传递给该函数。

void fun(std::list<int>::iterator beg, std::list<int>::iterator end)
{
    while(beg != end)
    {
        // do what you want here.
        beg++;
    }
}

I believe you should pass a range to the function.

void fun(std::list<int>::iterator beg, std::list<int>::iterator end)
{
    while(beg != end)
    {
        // do what you want here.
        beg++;
    }
}
明媚殇 2024-08-05 10:41:34

这将在 C++14 中改变。 [forward.iterators] N3936 的 24.2.5p2 说

但是,值初始化的迭代器可以进行比较并且应该比较
等于相同类型的其他值初始化迭代器。

This is going to change in C++14. [forward.iterators] 24.2.5p2 of N3936 says

However, value-initialized iterators may be compared and shall compare
equal to other value-initialized iterators of the same type.

人│生佛魔见 2024-08-05 10:41:32

好吧,我来一刺。 C++ 标准,第 24.1/5 节:

迭代器也可以有单数
不相关的值
任何容器。 [示例:之后
未初始化的声明
指针 x(与 int* x; 一样),x 必须
总是被假设有一个单数
指针的值。 ] 大多数结果
单数表达式未定义
价值观; 唯一的例外是
分配一个非奇异值
持有单数的迭代器
值。

所以,不,它们不能进行比较。

OK, I'll take a stab. The C++ Standard, Section 24.1/5:

Iterators can also have singular
values that are not associated with
any container. [Example: After the
declaration of an uninitialized
pointer x (as with int* x;), x must
always be assumed to have a singular
value of a pointer. ] Results of most
expressions are undefined for singular
values; the only excep- tion is an
assignment of a non-singular value to
an iterator that holds a singular
value.

So, no, they can't be compared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文