是否有可能有一个图形BOOST几个边的权重属性映射? [英] Is it possible to have several edge weight property maps for one graph BOOST?

查看:235
本文介绍了是否有可能有一个图形BOOST几个边的权重属性映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么会创建一个图形,使得属性映射(重边)是在每个属性映射不同?是有可能创造这样一个属性映射?
像属性映射的数组?
使用它,我还没有看到互联网上的任何人,我能有一个例子吗?

How would I create a graph, such that the property map (weight of edges) is different in each property map? is it possible to create such a property map? Like an array of property maps? I have not seen anyone on the internet using it,could I have an example?

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

和把每重量属性映射。

推荐答案

您可以撰写各种方式属性映射。最简单的方法似乎是这样的:

You can compose a property map in various ways. The simplest approach would seem something like:

<大骨节病> 住在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

这主要是更加详细的:

Using C++03 with transform_value_property_map

This is mainly much more verbose:

<大骨节病> 住在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

这篇关于是否有可能有一个图形BOOST几个边的权重属性映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆