矢量<int &> 不编译

vector<int &> not compiling

本文关键字:编译 gt int 矢量 lt      更新时间:2023-10-16

问题来自下面的代码:

vector<int &> one;  //compile failed

我很困惑为什么代码不编译

不能直接使用引用向量

但是你可以使用std::reference_wrapper来达到你想要的结果。

可能重复Why can't I make a vector of references?(我目前没有代表来标记它)。

STL容器必须保存可赋值的类型,而引用不能赋值。