Constant-sized vector

前端 未结 7 874
無奈伤痛
無奈伤痛 2020-12-24 02:04

Does someone know the way to define constant-sized vector?

For example, instead of defining

std::vector

it will be

相关标签:
7条回答
  • 2020-12-24 02:11

    This ----> std::vector<10, int> is invalid and causes error. But the new C++ standard has introduced a new class; the std::array. You can declare an array like this:

    std::array<int, 5> arr; // declares a new array that holds 5 ints
    std::array<int, 5> arr2(arr); // arr2 is equal to arr
    std::array<int, 5> arr3 = {1, 2, 3, 4, 5}; // arr3 holds 1, 2, 3, 4, 5
    

    The std::array has constant size and supports iterator/const_iterator/reverse_iterator/const_reverse_iterator. You can find more about this class at http://cplusplus.com/reference/stl/array/.

    0 讨论(0)
  • 2020-12-24 02:13

    The std::vector can always grow dynamically, but there are two ways you can allocate an initial size:

    This allocates initial size and fills the elements with zeroes:

    std::vector<int> v(10);
    v.size(); //returns 10
    

    This allocates an initial size but does not populate the array with zeroes:

    std::vector<int> v;
    v.reserve(10);
    v.size(); //returns 0
    
    0 讨论(0)
  • 2020-12-24 02:14

    Use std::array c++11

    For better readability you can make typedef:

    typedef std::array<int, 10> MyIntArray;
    
    0 讨论(0)
  • 2020-12-24 02:19

    This is an old question but if someone just needs constant-size indexed container with size defined at runtime, I like to use unique_ptr:

    // c++14
    auto constantContainer = std::make_unique<YourType []> ( size );
    
    // c++11
    std::unique_ptr<YourType[]> constantContainer {new YourType[ size ]};
    
    
    // Access
    constantContainer[ i ]
    
    0 讨论(0)
  • 2020-12-24 02:23

    If you want a fixed compile-time specified size (ala std::array<T, N>), but you want to be able to populate the vector with varying numbers of elements between 0 and N, then a good option is eastl::fixed_vector.

    std::vector:

    The size of a std::vector is dynamic - it will allocate required storage dynamically, and you cannot limit the size and enforce an error.

    You can however reserve a certain size, and then add elements up that size before it needs to allocate new storage.

    vector.size() is initially 0, and increases as you add elementss

    std::array:

    The size of a std::array is a compile-time constant - it will allocate required storage statically, and you cannot change the size.

    array.size() is always the size of the array, and is equal to array.max_size()

    eastl::fixed_vector:

    The size of an eastl::fixed_vector can be either static or dynamic.

    It will allocate a certain number of elements initially, and then if you allow dynamic growth, will allocate dynamically if required.

    For the purpose you originally asked for, you can disable growth (via bEnableOverflow in the template instantiation below)

    fixed_vector.size() is initially 0, and increases as you add elements.

    template<typename T, 
             size_t nodeCount, 
             bool bEnableOverflow = true, 
             typename OverflowAllocator = 
                          typename eastl::type_select<bEnableOverflow,
                                                      EASTLAllocatorType, 
                                                      EASTLDummyAllocatorType>::type>
    class fixed_vector;
    

    Simple example:

    #include <iostream>
    #include <vector>
    #include <array>
    #include "EASTL/fixed_vector.h"
    
    int main()
    {
        std::vector<int> v;
        v.reserve(10);
        std::cout << "size=" << v.size() << " capacity=" << v.capacity() << '\n';
    
        std::array<int, 10> a;
        std::cout << "size=" << a.size() << " capacity=" << a.max_size() << '\n';
    
        eastl::fixed_vector<int, 10, false> fv;
        std::cout << "size=" << fv.size() << " capacity=" << fv.capacity() << '\n';
    
        return 0;
    }
    

    Output:

    size=0 capacity=10
    size=10 capacity=10
    size=0 capacity=10
    

    Note that the size of array is 10, whereas vector and fixed_vector are 0

    0 讨论(0)
  • 2020-12-24 02:24

    A std::vector is a dynamic container, there is no mechanism to restrict its growth. To allocate an initial size:

    std::vector<int> v(10);
    

    C++11 has a std::array that would be more appropriate:

    std::array<int, 10> my_array;
    

    If your compiler does not support C++11 consider using boost::array:

    boost::array<int, 10> my_array;
    
    0 讨论(0)
提交回复
热议问题