conststd::vector<T>和std::vector<T>const有什么区别?
我认为声明 a 的唯一方法const<vector>是:
const std::vector<T> v;
回答
const 适用于它左边的东西,除非左边没有任何东西,然后它适用于它右边的东西。
所以,const int a=1;和int const a=1;是相等的。
const int *b和int const *b相等(指向常量的指针int),但与 不同int * const b,后者是指向非常量的常量指针int。
这适用于所有数据类型,我选择int它是因为它比std::vector<T>.
- And the West Const/East Const battle still continues to this day....
- "`const` goes to the left" is ambiguous; it can mean "`const` applies to whatever is on its left" as you intended, but it can also mean "`const` should be written to the left of whatever it should apply to", which is wrong. Better to write "`const" applies to the thing to its left` or something like that, and similar with right.
THE END
二维码