首页 > 解决方案 > 字符串名称和下标运算符前的 & 符号的含义

问题描述

最近我一直在玩 c++ 并注意到这段代码:

#include <iostream>
#include <string>

int main()
{
    std::string word = "hello there";
    std::cout << &word[2] << '\n';
}

打印变量的后缀,即:“llo there”。

为什么会这样?它不应该返回第三个字符的地址吗?我还注意到这条线给出了相同的结果:

std::cout << &*(word.begin()+2) << '\n';

有人可以向我解释吗?预先感谢

标签: c++string

解决方案


推荐阅读