为什么我不能打印 vector begin() 迭代器?

Why can't I print an vector's begin() iterator?

所以,我只是在学习向量,我有 2 个问题。 第一题:

std::vector<int> c{1,2,3};
std::cout << c.begin();

我知道我应该在 c.begin() 之前放一个 * 来输出 1 ,但是如果我不放指针符号,为什么 std::cout 会给我一个错误?不应该简单的输出c的第一个元素的地址吗?错误是“不匹配运算符 <<”。

know that i should put '*' before c.begin() to output 1 ,but why does cout give me an error if i don't put the pointer sign?

因为编译器不知道如何“打印”c.begin()类型的值。但是我们只读 the error(GodBolt;使用 GCC),这是非常重要的:

<source>:10:15: error: invalid operands to binary expression ('std::ostream'
(aka 'basic_ostream<char>') and 'std::vector<int, std::allocator<int> >::iterator' 
(aka '__normal_iterator<int *, std::vector<int, std::allocator<int> > >'))
    std::cout << c.begin();
    ~~~~~~~~~ ^  ~~~~~~~~~

换句话说:

I don't know how to apply the << binary operator when on the left-hand side you have a basic_ostream<char> and on the right-hand side you have a "normal iterator" corresponding to an std::vector

所以,即使 std::vector<int> 的迭代器可能是一个简单的 int* - 情况不一定如此:它可以是一些带有指针成员的 class 结构,或者指向向量开头的指针和整数偏移量等。语言标准不(AFAICR)保证此实现细节。