3

プロパティ マップ (エッジの重み) が各プロパティ マップで異なるようなグラフを作成するにはどうすればよいですか? このようなプロパティ マップを作成することは可能ですか? プロパティ マップの配列のようなものですか?

インターネット上でそれを使用している人を見たことがありません。例を教えてください。

Graph g(10); // graph with 10 nodes
cin>>a>>b>>weight1>>weight2>>weight3>>weight4;

各重みをプロパティ マップに入れます。

4

1 に答える 1

8

プロパティ マップはさまざまな方法で作成できます。最も単純なアプローチは次のようになります。

C++11 ラムダの使用function_property_map

Live On Coliru

#include <boost/property_map/function_property_map.hpp>
#include <iostream>

struct weights_t {
    float weight1, weight2, weight3, weight4;
};

using namespace boost;

int main() {
    std::vector<weights_t> weight_data { // index is vertex id
        { 1,2,3,4 },
        { 5,6,7,8 },
        { 9,10,11,12 },
        { 13,14,15,16 }, 
    };

    auto wmap1 = make_function_property_map<unsigned, float>([&weight_data](unsigned vertex_id) { return weight_data.at(vertex_id).weight1; });
    auto wmap2 = make_function_property_map<unsigned, float>([&weight_data](unsigned vertex_id) { return weight_data.at(vertex_id).weight2; });
    auto wmap3 = make_function_property_map<unsigned, float>([&weight_data](unsigned vertex_id) { return weight_data.at(vertex_id).weight3; });
    auto wmap4 = make_function_property_map<unsigned, float>([&weight_data](unsigned vertex_id) { return weight_data.at(vertex_id).weight4; });

    for (unsigned vertex = 0; vertex < weight_data.size(); ++vertex)
        std::cout << wmap1[vertex] << "\t" << wmap2[vertex] << "\t" << wmap3[vertex] << "\t"<< wmap4[vertex] << "\n";
}

C++03 の使用transform_value_property_map

これは主にはるかに冗長です。

Live On Coliru

#include <boost/property_map/transform_value_property_map.hpp>
#include <iostream>

struct weights_t {
    float weight1, weight2, weight3, weight4;

    weights_t(float w1, float w2, float w3, float w4)
        : weight1(w1), weight2(w2), weight3(w3), weight4(w4)
    { }

    template <int which> struct access {
        typedef float result_type;

        float operator()(weights_t const& w) const { 
            BOOST_STATIC_ASSERT(which >= 1 && which <= 4);
            switch (which) {
                case 1: return w.weight1;
                case 2: return w.weight2;
                case 3: return w.weight3;
                case 4: return w.weight4;
            }
        }
    };
};

using namespace boost;

int main() {
    std::vector<weights_t> weight_data; // index is vertex id
    weight_data.push_back(weights_t(1,2,3,4));
    weight_data.push_back(weights_t(5,6,7,8));
    weight_data.push_back(weights_t(9,10,11,12));
    weight_data.push_back(weights_t(13,14,15,16));

    boost::transform_value_property_map<weights_t::access<1>, weights_t*, float> wmap1 = make_transform_value_property_map(weights_t::access<1>(), &weight_data[0]);
    boost::transform_value_property_map<weights_t::access<2>, weights_t*, float> wmap2 = make_transform_value_property_map(weights_t::access<2>(), &weight_data[0]);
    boost::transform_value_property_map<weights_t::access<3>, weights_t*, float> wmap3 = make_transform_value_property_map(weights_t::access<3>(), &weight_data[0]);
    boost::transform_value_property_map<weights_t::access<4>, weights_t*, float> wmap4 = make_transform_value_property_map(weights_t::access<4>(), &weight_data[0]);

    for (unsigned vertex = 0; vertex < weight_data.size(); ++vertex)
        std::cout << wmap1[vertex] << "\t" << wmap2[vertex] << "\t" << wmap3[vertex] << "\t"<< wmap4[vertex] << "\n";
}

出力

両方のサンプル出力

1   2   3   4
5   6   7   8
9   10  11  12
13  14  15  16
于 2014-11-17T21:58:35.467 に答える