0

キーとして文字列、値としてCnode構造体を持つマルチマップがあります。

struct Cnode
{
    Cnode() : wtA(0), wtC(0), wtG(0), wtT(0) { }
    Cnode(int newA, int newC, int newG, int newT)
      : wtA(newA), wtC(newC), wtG(newG), wtT(newT)
    { }

    int wtA, wtC, wtG, wtT;
};

Cnode combine_valuesA(const myFast map, const string& key)
{
    return std::accumulate(
        map.equal_range(key).first,
        map.equal_range(key).second,
        0,
        [](int sumA, int sumC, int sumG, int sumT, myFast::value_type p) //error
        {
            return Cnode(
                sumA + p.second.wtA,
                sumC + p.second.wtC,
                sumG + p.second.wtG,
                sumT + p.second.wtT);
        }
    );
}

マルチマップ上の重複キーのCnodeにすべてのintを追加する必要があります。これは私が得るエラーです:

'int'から'Cnode'への実行可能な変換はありません

4

1 に答える 1

3

これは、あなたが探しているセマンティクスを持っているようです:

struct Cnode
{
    Cnode() : wtA(), wtC(), wtG(), wtT() { }
    Cnode(int a, int c, int g, int t)
      : wtA(a), wtC(c), wtG(g), wtT(t)
    { }

    int wtA, wtC, wtG, wtT;
};
typedef std::multimap<std::string, Cnode> myFast;

myFast::mapped_type combine_valuesA(myFast const& map, std::string const& key)
{
    auto range = map.equal_range(key);
    return std::accumulate(
        range.first, range.second, myFast::mapped_type(),
        [](myFast::mapped_type const& node, myFast::value_type const& p)
        {
            return myFast::mapped_type(
                node.wtA + p.second.wtA,
                node.wtC + p.second.wtC,
                node.wtG + p.second.wtG,
                node.wtT + p.second.wtT
            );
        }
    );
}

コードを簡略化するために、 、、 またはCnodeで自明に置き換えることができることに注意してください。最初のものを使用すると、次のようになります。std::valarray<int>std::array<int, 4>std::tuple<int, int, int, int>

typedef std::multimap<std::string, std::valarray<int>> myFast;

myFast::mapped_type combine_valuesA(myFast const& map, std::string const& key)
{
    auto range = map.equal_range(key);
    return std::accumulate(
        range.first, range.second, myFast::mapped_type(4),
        [](myFast::mapped_type const& node, myFast::value_type const& p)
        {
            return node + p.second;
        }
    );
}
于 2012-09-28T19:48:51.470 に答える