0

我的模型最好使用一些

v int[30][i][N_i];

结构是 30 个整数元组向量,其中

v[0] 是一个虚拟对象,
v[1] 是普通整数(其中 N_0 个),
v[2] 是整数对(N_1 对)
......
v[29] 将是 29 个整数元组(N_29 个他们)

不像 vector<vector<int>>“generic-vector-of-vectors-in-c”

显然,外部固定的 dim=30 没有问题,内部的由自扩展 STL 向量类处理。

有没有办法让中间维度固定,但不是恒定的?

4

3 回答 3

2

正如我在对您的问题的评论中所写的那样,我不确定您在寻找什么(顺便说一句,我对“as in Java”部分非常感兴趣)。

但是因为我认为看看如何使用 Boost 生成它会很有趣。MPL(和Fusion ... 和Array),我假设您想要一个静态定义的结构,其 Ntn 元素是大小为 N 的 int 数组的向量:

#define FUSION_MAX_VECTOR_SIZE 30

#include <boost/mpl/transform.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/inserter.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/size_t.hpp>

#include <boost/fusion/include/mpl.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <boost/fusion/include/as_vector.hpp>

#include <boost/array.hpp>

#include <vector>


namespace bf = boost::fusion;
namespace bmpl = boost::mpl;

// Type generator used for elements 2..N
// For those elements, the type of the n'th element is
// std::vector<boost::array<int, n>>
template<class SizeT>
struct VectorOfArray
{
    typedef std::vector<boost::array<int, SizeT::type::value> > type;
};

// The dummy type used for the first element
struct Dummy{};

// The container itself
template<size_t Size>
struct StrangeContainer
{

    // Define a fusion::vector (this is, more or less, equivalent to a tuple)
    // of "Size" elements, where:
    // - the type of element 0 is Dummy, 
    // - the type of element 1 is vector<int>
    // - the type of the n'th element is vector<array<int, n>>
    typedef typename bf::result_of::as_vector<
        typename bmpl::transform<
            bmpl::range_c<size_t, 2, Size>,
            VectorOfArray<bmpl::_1>,
            bmpl::back_inserter<
                bmpl::vector<Dummy, std::vector<int> >
            >
        >::type
    >::type ContentsType;

    // Helper struct to compute the return type of the "At()" member
    template<size_t I>
    struct ElemType
    {
        typedef typename VectorOfArray<bmpl::size_t<I> >::type type;
    };

    // Specialize "At()"'s return type for element 1
    template<>
    struct ElemType<static_cast<size_t>(1)>
    {
        typedef std::vector<int> type;
    };

    // Specialize "At()"'s return type for element 0
    template<>
    struct ElemType<static_cast<size_t>(0)>
    {
        typedef Dummy type;
    };

    // Get the I'th element
    template<size_t I>
    typename ElemType<I>::type& 
    At()
    {
        return bf::at_c<I>(m_Contents);
    }

    // The fusion vector holding the elements
    ContentsType m_Contents;
};

int main()
{
    StrangeContainer<30> s;
    Dummy& d = s.At<0>();
    s.At<1>().push_back(1);
    s.At<2>().push_back(boost::array<int, 2>());
    s.At<3>().push_back(boost::array<int, 3>());
    s.At<29>().push_back(boost::array<int, 29>());
    s.At<29>()[0][0] = 1234;

    return 0;
}
于 2009-11-16T22:19:49.333 回答
0

The best way to do what you want is write wrapper functions around the vector accessors. The best way to wrap existing behavior is to write a new class that is implemented with your fancy vector of vector of what ever.

于 2009-11-16T16:25:44.477 回答
0

我是迈克尔(最初的作者),现在拥有 ID La-AIDA

首先,谢谢大家,Boost & Fusion 对我来说是新的。

致 Éric:一个错字:v[1] 应该有 N_1 个条目,v[2] N_2 等等。我想要类似 STL 的东西,而不是 C 数组(缺少边界检查,没有添加它的选项)。

对 Éric 的新评论:我尝试了您的解决方案,它立即起作用(几乎,在删除虚拟查询之后)!谢谢!但是:我需要类似的东西

 for (i = 1;i < 30;i++) {
    cout << s.At<i>[0] << endl; 
 }

也就是说, At<..> 的索引应该是可变的(这就是重点,能够运行索引而不是分别处理 30 个硬编码的东西)
但是 gcc 抱怨错误:'i' cannot appear in一个常量表达式

关于“as in Java”:AfaIk,Java 中的二维矩阵不是一些 int v[10][10];具有固定尺寸,但类似于 int[][] v; 你第一次有一个

v = new int[10][];

(或类似的语法)然后,这就是重点:

v[0] = new int[1];
...
v[9] = new a[10];

它制作了一个三角矩阵,或者当然是您喜欢的任何形式。事实上,一个常规的 10×10 矩阵也需要 1 加 10 个新矩阵。

关于结构本身:等效的数据结构是

vector<int> v1;
vector<pair<int,int>> v2;
vector<int,int,int> v3;
...
vector<int[29]> v29;

但是,我们必须分别处理 30 个部分中的每一个。

我想说v[5][3][123] = 99; 将第 123 个 5 元组中的第 3 个组件设置为 99,而无需定义

vector<int> v[30][30];

这可以解决问题,但会浪费巨大的空间,因为 v[1][2..30][0..\infty] or more generally v[i][i+1..30][*] 从未使用过。

所以,在我的问题中,我有一个 int's 列表,另一个对,三元组,...,30 个 int 元组的列表,它们都应该是可排序的等,在一个结构中,不会浪费空间。

于 2009-12-01T20:59:44.500 回答