【问题标题】:What is the difference between const std::vector<T> and std::vector<T> const?const std::vector<T> 和 std::vector<T> const 有什么区别?
【发布时间】:2021-06-02 04:41:43
【问题描述】:

我认为声明const&lt;vector&gt; 的唯一方法是:

const std::vector<T> v; 

【问题讨论】:

    标签: c++ vector constants const-correctness


    【解决方案1】:

    const 适用于其左侧的东西,除非左侧没有任何东西,否则它适用于其右侧的东西。

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

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

    这适用于所有数据类型,我选择int,因为它比std::vector&lt;T&gt; 更容易输入。

    【讨论】:

    • const 向左走”是模棱两可的;它可能意味着“const 适用于其左侧的任何内容”,但它也可能意味着“const 应该写在它应该适用的任何内容的左侧”,这是错误的。最好写“const" applies to the thing to its left”之类的,和right类似。
    • 而西区/东区的战斗一直持续到今天......
    • @Thomas 谢谢,应用是正确的词。写答案的时候不想写“东西”,但是不知道还能写什么,可以是指针,也可以是类型。
    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 2017-09-01
    • 2016-10-12
    相关资源
    最近更新 更多