简体   繁体   English

返回未知大小的std :: array

[英]Return std::array of unknown size

I would like to return a multidimensional std::array from my function. 我想从我的函数返回多维std :: array。 The size of the returned array shall be determined by the size of an input argument. 返回数组的大小应由输入参数的大小确定。 Something like: 就像是:

std::array<std::array<unsigned int, n>, n> foo(std::vector<int> vec){
    unsigned int n = vec.size;
    std::array<std::array<unsigned int, n>, n> result;
    return result;
}

It would be nice to solve this without an annoying additional template argument. 无需烦人的附加模板参数即可解决此问题。 std::vector instead of std::array seems not to be as straightforward to initialize with n (undetermined) items as std::array (without explicit initialization). 用std :: vector代替std :: array似乎不像使用std :: array初始化n(不确定)项那样简单(没有显式初始化)。 How can this be made possible? 如何做到这一点? Thank you! 谢谢!

You can create a n sized std::vector of n sized std::vector s using its c'tor, ie: 您可以使用其c'tor创建n大小为std::vectorn大小的std::vector ,即:

std::vector<std::vector<unsigned int>> result(n, std::vector<unsigned int>(n, 0));

NOTE: According to cppreference.com , the second parameter used in the above example of std::vector s c'tor is for the value of each item to be created: c'tor signature: 注意:根据cppreference.com ,上述std::vector s c'tor示例中使用的第二个参数是要创建的每个项目的值:c'tor签名:

vector( size_type count, const T& value, const Allocator& alloc = Allocator()); .

First thing you need to know is that std::array has its size fixed at compile time, as the documentation from cppreference sais: 首先需要了解的是std::array的大小在编译时是固定的,如cppreference的文档所述

std::array is a container that encapsulates fixed size arrays. std :: array是封装固定大小的数组的容器。

This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. 此容器是一种聚合类型,其语义与将C型数组T [N]作为其唯一的非静态数据成员的结构具有相同的语义。

If n comes from std::cin or from the command line arguments (or whatever kind of input out of compile time), then the compiler can't deduce the type, and therefore it will throw an error. 如果n来自std::cin或命令行参数(或编译时间之外的任何类型的输入),则编译器无法推断出类型,因此将引发错误。

The most sensible way to do this is with std::vector , and you can do it like this: 最明智的方法是使用std::vector ,您可以这样做:

std::vector<std::vector<unsigned int>> foo(std::vector<int> vec){
    unsigned int n = vec.size();
    std::vector<std::vector<unsigned int>> result(n, std::vector<unsigned int>(n));
    return result;
}

Just initialize every vector using the size constructor of the vector. 只需使用向量的大小构造函数初始化每个向量。

Try using Boost.MultiArray . 尝试使用Boost.MultiArray It allows you to create multidimensional arrays with contents of arbitrary content type. 它允许您创建具有任意内容类型的内容的多维数组。 I have used it (the boost::multi_array_ref part to be more specific) and it works pretty nice. 我已经使用了它(boost :: multi_array_ref部分更加具体),并且效果很好。 An important feature is the ability to create array views (and slices based on views). 一个重要的功能是能够创建数组视图 (以及基于视图的切片)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM