简体   繁体   English

C ++如何影响可变关键字对容器的性能?

[英]C++ How affects mutable keyword to the performance of container?

I want to know how mutable affects a container (map, vector, list, ...). 我想知道mutable如何影响容器(map,vector,list,...)。 In addition, what do I have to bear in mind? 另外,我还要记住什么?

mutable , like const , is just a compile-time thing. const一样, mutable只是一个编译时间的东西。 It just allows you to modify that variable in a constant context. 它只允许您在常量上下文中修改该变量。 At runtime, there is no difference wether you declared the container mutable or not. 在运行时,您宣布容器是否mutable是没有区别的。

class Foo{
  mutable int i;

public:
  void foo() const{
    // constant context, but you can modify `i`
    i = 5;
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM