0

私のモデルはいくつかを使用するのが最適です

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

intのタプルの30個のベクトルである構造。

v [0]はダミー、
v [1]はプレーンint(N_0の)、
v [2]はintのペア(N_1ペア)
...
v [29]は29タプルのint(N_29の彼ら)

これは vector<vector<int>>「generic-vector-of-vectors-in-c」とは異なります。

どうやら、外側の固定dim = 30は問題ありません、内側の固定dim=30は自己拡張STLベクトルクラスによって処理されます。

中間の寸法を固定する方法はありますが、一定ではありませんか?

4

3 に答える 3

2

私があなたの質問へのコメントに書いたように、私はあなたが探しているものを理解していません (そして、私は「Java のように」部分、BTW に非常に興味があります)。

でも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

目的を達成するための最良の方法は、ベクター アクセサーの周りにラッパー関数を記述することです。既存の動作をラップする最善の方法は、これまでにないベクターの派手なベクターで実装される新しいクラスを作成することです。

于 2009-11-16T16:25:44.477 に答える
0

私は Michael (最初の著者) で、現在 ID La-AIDA を持っています。

まず、皆さん、Boost & Fusion は私にとって初めてのことでしたが、ありがとうございました。

Éric へ: 1 つのタイプミス: v[1] には N_1 個のエントリ、v[2] には N_2 個のエントリが必要です。C配列ではなく、STLのようなものが欲しいです(境界チェックがなく、追加するオプションがありません)。

Éric への新しいコメント: 私はあなたの解決策を試しましたが、(ダミークエリを削除した後、ほぼ) すぐに機能しました! ありがとうございました!しかし:次のようなものが必要です

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

つまり、At<..> のインデックスは可変である必要があります (つまり、30 個のハードコードされたものを個別に処理する代わりにインデックスを実行できるようにすることが重要です)
が、gcc はエラーで不平を言います: 'i' cannot appear in定数式

「Java のように」について: AfaIk、Java の 2 次元行列は int v[10][10] ではありません。固定次元ですが、int[][] v; のようなものです。あなたが最初に持っている場所

v = new int[10][];

(または同様の構文)そして、これがポイントです:

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

これにより、三角行列、またはもちろん好きな形式が作成されます。実際、通常の 10 行 10 列の行列でも、1 プラス 10 個の new が必要です。

構造自体について: 同等のデータ構造は次のようになります。

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 、別のペア、トリプル、...、 int の 30 タプルのリストがあり、これらはすべて単一の構造内で、スペースを無駄にすることなくソート可能である必要があります。

于 2009-12-01T20:59:44.500 に答える