Create a fixed size std::vector and write to the elements

后端 未结 3 1480
滥情空心
滥情空心 2021-02-19 19:34

In C++ I wish to allocate a fixed-size (but size determined at runtime) std::vector then write to the elements in this vector. This is the code I am using:

int b         


        
3条回答
  •  孤城傲影
    2021-02-19 19:59

    The actual error is because you declare the vector to be constant, meaning you can never change the contents.

    Then when you try to get a non-constant reference to an entry in the vector, the compiler tells you that you can't do that, because then you could change the constant value stored in the vector.


    As for creating a vector with a size that can be fixed at runtime, but not change size after the vector has been created, then you have to create a container adaptor. Basically you have to create a wrapper around another container, just like e.g. std::stack does.

提交回复
热议问题