C++ 恒定大小的向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11134497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:53:14  来源:igfitidea点击:

Constant-sized vector

c++stlvector

提问by Iron-Eagle

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

有人知道定义恒定大小向量的方法吗?

For example, instead of defining

例如,而不是定义

std::vector<int>

it will be

这将是

std::vector<10, int>

It should be completely cross-platformed. Maybe an open source class?

它应该是完全跨平台的。也许是一个开源课程?

采纳答案by svelten

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

std::vector 总是可以动态增长,但有两种方法可以分配初始大小:

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

回答by juanchopanza

There is no way to define a constant size vector. If you know the size at compile time, you could use C++11's std::arrayaggregate.

无法定义恒定大小的向量。如果您知道编译时的大小,则可以使用 C++11 的std::array聚合。

#include <array>

std::array<int, 10> a;

If you don't have the relevant C++11 support, you could use the TR1 version:

如果您没有相关的 C++11 支持,您可以使用 TR1 版本:

#include <tr1/array>

std::tr1::array<int, 10> a;

or boost::array, as has been suggested in other answers.

boost::array,正如其他答案中所建议的那样。

回答by Andrew

Use std::arrayc++11

使用std::array c++11

For better readability you can make typedef:

为了更好的可读性,您可以使用 typedef:

typedef std::array<int, 10> MyIntArray;

回答by Steve Lorimer

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 0and N, then a good option is eastl::fixed_vector.

如果您想要一个固定的编译时指定大小 (ala std::array<T, N>),但您希望能够在0和之间使用不同数量的元素填充向量N,那么一个不错的选择是eastl::fixed_vector.

std::vector:

标准::向量:

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

a 的大小std::vector是动态的 - 它将动态分配所需的存储空间,您不能限制大小并强制执行错误。

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

但是reserve,您可以确定某个大小,然后在需要分配新存储之前将元素添加到该大小。

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

vector.size()最初为 0,并随着您添加元素而增加

std::array:

标准::数组:

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

a 的大小std::array是一个编译时常量 - 它将静态分配所需的存储空间,并且您无法更改大小。

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

array.size()始终是数组的大小,并且等于 array.max_size()

eastl::fixed_vector:

东部::固定_向量:

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

an 的大小eastl::fixed_vector可以是静态的或动态的。

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 bEnableOverflowin the template instantiation below)

出于您最初要求的目的,您可以禁用增长(通过bEnableOverflow下面的模板实例化)

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

fixed_vector.size()最初为 0,并随着您添加元素而增加。

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 arrayis 10, whereas vectorand fixed_vectorare 0

请注意,的大小array为 10,而vectorfixed_vector为 0

回答by hmjd

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

Astd::vector是动态容器,没有机制限制其增长。分配初始大小:

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

C++11 has a std::arraythat would be more appropriate:

C++11 有一个std::array更合适的:

std::array<int, 10> my_array;

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

如果您的编译器不支持 C++11,请考虑使用boost::array

boost::array<int, 10> my_array;

回答by Pari

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:

这是一个老问题,但如果有人只需要在运行时定义大小的常量大小索引容器,我喜欢使用unique_ptr

// c++14
auto constantContainer = std::make_unique<YourType []> ( size );

// c++11
std::unique_ptr<YourType[]> constantContainer {new YourType[ size ]};


// Access
constantContainer[ i ]

回答by Rontogiannis Aristofanis

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::vector<10, int>无效并导致错误。但是新的 C++ 标准引入了一个新类;std::array 。您可以像这样声明一个数组:

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::arrayhas 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/.

std::array具有恒定的大小和支持iterator/const_iterator/reverse_iterator/const_reverse_iterator。您可以在http://cplusplus.com/reference/stl/array/找到有关此类的更多信息。