c++ - 什么是开始()==结束()?

标签 c++ arrays c++11

array.zero-2说:

In the case that N == 0, begin() == end() == unique value. The return value of data() is unspecified.

这个独特的值(value)是什么意思?这是否意味着表达式 begin() == end() 的结果是唯一值?我不明白这有什么意义,因为 bool 只能是 true 或 false,这不是很独特。我运行了以下测试:

#include <array>
#include <iostream>
#include <iomanip>
#include <type_traits>

int main()
{
    std::array<int, 0> test;
    auto b = test.begin();
    auto e = test.end();
    std::cout << std::boolalpha 
              << (test.begin() == test.end())
              << std::is_same<bool, decltype(test.begin() == test.end())>::value
              << std::is_same<bool, decltype(b == e)>::value;
}

它都按预期输出 true。然后我想知道对于非零大小的数组可能会有所不同:

int main()
{
    std::array<int, 0> test;
    std::array<int, 1> test2;
    std::cout << std::boolalpha
              << std::is_same<
                    decltype(test.begin() == test.end()),
                    decltype(test2.begin() == test2.end())
                >::value
              << std::is_same<
                    decltype(test.begin()),
                    decltype(test2.begin())
                >::value;
}

一切都是真的。那么什么是“独特值(value)”呢?什么具有独特的值(value)?

最佳答案

begin() == end() == unique value 与其说是实际的 C++,不如说是一种逻辑描述。它表明 begin() 与 end() 相同,并且它们都等于某个唯一值。

基本上是逻辑表达式 'a=b=c',除了使用 C++ ==

关于c++ - 什么是开始()==结束()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37505804/

相关文章:

c++ - 在 C++ 中对 STL vector 调用 resize() 时出现意外结果

c - 数组数据类型内存分配

c++ - 从 C++ 中的另一个类更改数组值

c++ - boost 1.71.0 : How to get process output?

c++ - 编译时动态链接库不生成 .lib 文件(Visual Studio C++ Express)

c++ - 如何在 Cython 中返回新的 C++ 对象?

c++ - 纯虚方法只能在直接派生类中实现——为什么?

php - 比较单个数组中的每个索引

c++ - auto_ptr 和 unique_ptr 有什么意义?

c++ - constexpr 指针值