Container of fixed dynamic size

后端 未结 3 760
离开以前
离开以前 2020-12-15 16:11

Is there a standard container for a sequence of fixed length, where that length is determined at runtime. Preferrably, I\'d like to pass an argument to the constructor of ea

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 16:49

    I think const std::vector has the properties you ask for. Its elements aren't actually defined with const, but it provides a const view of them. You can't change the size. You can't call any of the member functions that need T to be movable, so for normal use they won't be instantiated (they would be if you did an extern class declaration, so you can't do that).

    If I'm wrong, and you do have trouble because T isn't movable, try a const std::deque instead.

    The difficulty is constructing the blighter -- in C++11 you can do this with an initializer list, or in C++03 you can construct a const vector from a non-const vector or from anything else you can get iterators for. This doesn't necessarily mean T needs to be copyable, but there does need to be a type from which it can be constructed (perhaps one you invent for the purpose) .

提交回复
热议问题