const std::vector<T> 和 std::vector<T> const 有什么区别?

时间:2021-03-03 15:06:33

标签: c++ vector constants const-correctness

我认为声明 const<vector> 的唯一方法是:

const std::vector<T> v; 

1 个答案:

答案 0 :(得分:7)

const 适用于它左边的东西,除非左边没有任何东西,然后它适用于它右边的东西。

所以,const int a=1;int const a=1; 是相等的。

const int *bint const *b 相等(指向常量 int 的指针),但与 int * const b 不同,后者是指向非常量 {{1 的常量指针}}。

这适用于所有数据类型,我选择 int 因为它比 int 更容易输入。

相关问题