简体   繁体   English

如何从向量的大小声明数组?

[英]How can I declare an array from the size of a vector?

I am trying to copy a vector into an array however I don't know how to declare the array from the size of the vector. 我正在尝试将向量复制到数组中,但是我不知道如何从向量的大小声明数组。

Code: 码:

int main() {
    vector<int> ivec = {1, 2, 3, 4, 5};
    constexpr size_t size = ivec.size();
    int arr[size];
    for(size_t i = 0; i < ivec.size(); ++i)
        arr[i] = ivec[i];
    for(size_t i : arr)
        cout << i << endl;
    return 0;
}

However, I think this won't compile because ivec.size() can't be a constant expression (though I'm not sure if this is the case). 但是,我认为这不会编译,因为ivec.size()不能为常量表达式(尽管我不确定是否是这种情况)。 In which case how could I do this without having to manually enter the number of elements? 在那种情况下,我该如何做而不必手动输入元素数量?

As of right now std::vector size() is not a constexpr , so you cannot use it in constexpr essions. 到目前为止, std::vector size()还不是constexpr ,因此您不能在constexpr使用它。 As a result, you can try using the new keyword for dynamically sized arrays, but that would be pointless, as you're already using a vector. 结果,您可以尝试将new关键字用于动态调整大小的数组,但这将毫无意义,因为您已经在使用向量。

vector<int> vi = {1, 2, 3, 4, 5};
int* arr = new int[vi.size()];
std::copy(vi.begin(), vi.end(), arr);
for (unsigned int i = 0; i < vi.size(); i++)
    std::cout << arr[i] << " ";
delete[] arr;

Note: : You can use std::begin() with the second example because arr[] is an array but not with the first example because arr* is a pointer. 注意 std::begin()可以在第二个示例中使用std::begin() ,因为arr[]是数组,但不能在第一个示例中使用,因为arr*是指针。 However, std::copy() accepts both, so it should be fine. 但是, std::copy()接受两者,因此应该没问题。

initializer_list s can be used in constexpr essions: initializer_list可以在constexpr语句中使用:

constexpr initializer_list<int> il = {1, 2, 3, 4, 5};
int arr[il.size()];
std::copy(il.begin(), il.end(), std::begin(arr));
for (unsigned int i = 0; i < il.size(); i++)
    std::cout << arr[i] << " ";

In general, it is not possible to copy a vector into array, because, a usual array is constexpr , while vector size is not, it is of dynamic size. 通常,不可能将向量复制到数组中,因为通常的数组是constexpr ,而向量大小不是,而是动态大小。 There are also dynamic arrays supported by some compilers, but then again, there size is never constexpr . 一些编译器还支持动态数组,但是同样,大小永远不会是constexpr I guess, you need just to use vector. 我想,您只需要使用向量。

I don't know your motivation, but... 我不知道你的动机,但是...

int main() {
    vector<int> ivec = {1, 2, 3, 4, 5};
    constexpr size_t size = ivec.size();
    int* arr = (int*)malloc( sizeof( int * size ) );
    for(size_t i = 0; i < ivec.size(); ++i)
        arr[i] = ivec[i];
    for(size_t i : arr)
        cout << i << endl;
    free( arr );
    return 0;
}

I'd avoid 我会避免

constexpr size_t size = ivec.size();
int arr[size];

and do it like this 并这样做

size_t size = ivec.size();
int* arr = new int[size];

and then you handle it like the constantly allocated array. 然后像处理不断分配的数组一样处理它。 Read more about dynamically allocated arrays. 阅读有关动态分配数组的更多信息。 and don't forget to 而且不要忘记

delete[] array;

You need to allocate memory, because as you have said vector size is not a constant: 您需要分配内存,因为正如您所说的,向量大小不是常数:

int main() {
    vector<int> ivec = { 1, 2, 3, 4, 5 };
    size_t size = ivec.size();
    int *arr = new int[size]; // allocate memory
    for (size_t i = 0; i < ivec.size(); ++i)
        arr[i] = ivec[i];
    for (size_t i = 0; i < size; ++i)
        cout << i << endl;
    delete [] arr;  // release memory
    return 0;
}

It seems you want to get hold of the number of the elements in an initializer list yielding a constexpr . 看来您想掌握产生constexpr的初始化程序列表中的元素数量。 The only way I'm aware of doing this is to use 我知道这样做的唯一方法是使用

#include <cstddef>
template <typename T, std::size_t N>
constexpr std::size_t size(T(&)[N]) {
    return N;
}

int main() {
    int vec[] = { 1, 2, 3, 4, 5 };
    constexpr std::size_t s = size(vec);

    int array[s];
    std::copy(std::begin(vec), std::end(vec), array);
}

If you really need to use a std::vector<T> as source, you'll need to allocate memory, probably using std::vector<T> in the first place! 如果确实需要使用std::vector<T>作为源,则需要分配内存,可能首先使用std::vector<T> If you want to allocate the memory yourself you'd use it something like this: 如果您想自己分配内存,则可以使用以下方法:

std::vector<int> vec = { 1, 2, 3, 4, 5 };
std::unique_ptr<int[]> array(new int[vec.size()]);
std::copy(vec.begin(), vec.end(), array.get());

The use of std::unique_ptr<int[]> makes sure that the allocated memory is released automatically. 使用std::unique_ptr<int[]>可确保分配的内存自动释放。

A constexpr is a Constant Expression which is an expression that is evaluated at compile-time. constexpr是一个常量表达式 ,它是在编译时求值的表达式。 That it is known at compile-time is a fundamental trait of a constexpr . 在编译时知道它是constexpr的基本特征。

Given this, you can see how it makes no sense to try to construct a non-dynamically-allocated C-style array at run time when the number of elements will only be known at run-time. 鉴于此,您将看到在运行时尝试仅在运行时知道元素数量的情况下尝试构造非动态分配的C样式数组是没有任何意义的。 The two ideas are orthogonal. 这两个想法是正交的。

From a technical standpoint, you cannot initialize a constexpr from a non-constant-expression. 从技术角度来看,您不能从非常量表达式初始化constexpr vector::size() is non- constexpr , so as you suspect, it is not only not compilable, but it is also not logical from a design standpoint to try to construct a constexpr from it: vector::size()不是constexpr ,因此您怀疑它不仅不可编译,而且从设计的角度来看试图从中构造constexpr也不合逻辑:

constexpr size_t size = ivec.size(); // NO GOOD!

All this being said, it's very rare to need to construct a C-style array from vector . 这一切都这样说,这是非常罕见的需要构建从C数组vector You're already doing the Right Thing by using vector in the first place. 首先,您已经通过使用vector正确的事 Don't mess it all up now by copying it to a crappy array. 现在就不要将其复制到一个糟糕的数组中,使一切混乱。

A vector is guaranteed to have contigious storage. 保证vector具有连续存储。 What this means is you can use it just like a C-style array in most cases. 这意味着在大多数情况下,您可以像使用C样式数组一样使用它。 All you need to do is pass the address (or reference) to the first element in the vector to whatever is expecting a C-style array and it will work just fine. 您需要做的就是将地址(或引用)传递到vector的第一个元素,传递给期望使用C样式数组的任何元素,它将正常工作。

void AincentApiFunction (int* array, size_t sizeofArray);

int main()
{
  std::vector <int> v;
  // ...

  AincentApiFunction (&v[0], v.size());
}

In C++11 arrays may declared as runtime bound on the stack: (Note: this is only per the latest available draft: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf and will likely not be standard, but g++ with -std=c++11 will allow it 在C ++ 11中,数组可以声明为在堆栈上绑定的运行时:(注:仅根据最新的可用草案提供: http//www.open-std.org/jtc1/sc22/wg21/docs/papers/2013 /n3690.pdf ,可能不是标准的,但是带有-std=c++11 g ++将允许它

Note that is is not constexpression: 注意不是constexpression:

8.3.4 Arrays [dcl.array] 8.3.4数组[dcl.array]

D1 [ expressionopt] attribute-specifier-seqopt D1 [expressionopt]属性说明符-seqopt

Example from the standard: 来自标准的示例:

void f(unsigned int n) {
    int a[n]; // type of a is “array of runtime bound of int”
}

So all you need to do is remove constexpr: 因此,您需要做的就是删除 constexpr:

int main() {
    vector<int> ivec = {1, 2, 3, 4, 5};
    size_t size = ivec.size();                  // this is fine
    int arr[size];
    for(size_t i = 0; i < ivec.size(); ++i)
        arr[i] = ivec[i];
    for(size_t i : arr)
        cout << i << endl;
    return 0;
}

Your compiler may or may not allow this, so depends on how strictly standard you need to be 您的编译器可能允许也可能不允许,因此取决于您需要达到的严格标准

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

相关问题 如何获得数组的大小,以便我可以从 function 声明所述数组 - how can i get the size of an array so i can declare said array from a function 如何根据参数的大小在函数内部声明数组? - How can I declare an array inside a function according to the size of a parameter? 如何声明vector :: size_type的向量? - How do I declare a vector of vector::size_type? 如何声明大小未知的结构? - How can I declare a structure of an unknow size? 有没有办法声明一个特定大小的 std::vector 数组? - Is there a way to declare an array of std::vector of a certain size? 在C ++中,我如何声明一个将任何对象的向量数组作为一个agrument的函数? - In C++, how can I declare a function that takes a vector array of any object as an agrument? 如何声明同一个类的成员向量? - How can I declare a member vector of the same class? 如果它是成员函数,如何声明字符串数组的大小 - How do I declare the size of a string array if it's a member function 如何从 Arduino 上的 function 内部声明/设置全局数组的大小? - How to declare/set size of global array from inside a function on Arduino? 我如何声明一个未知大小的数组,然后输入直到我想要然后再获取该数组的大小? - How do I declare an array of unknown size then take input till I desire and then get the size of the array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM