将 multimap<Key,Value> 转换为 vector<vector<Value>>

Convert multimap<Key,Value> to vector<vector<Value>>

我需要将 std::unordered_multimap<Key,T> 转换为 std::vector<std::vector<T>>。我需要这样做,因为我的程序需要对所有数据进行排序,而地图无法排序。一个例子:

// Map:
{ "A", 1 },
{ "A", 3 },
{ "A", 2 },
{ "B", 5 }, 
{ "B", 2 },

// Map converted to vector<vector<Value>>:
{ 1, 3, 2 }, 
{ 5, 2 }

现在我有了这段有效的代码。但我想知道这是否是最好的方法。

#include <unordered_map>
#include <iostream>
#include <string>
#include <vector>    

int main()
{
    typedef std::string Key_t;
    typedef int Value_t;
    typedef std::unordered_multimap<Key_t, Value_t> Map_t;

    const Map_t map = {
        { "A", 1 }, 
        { "A", 3 }, 
        { "A", 2 },
        { "B", 5 }, 
        { "B", 2 },
    };

    std::vector< std::vector< Value_t > > output;

    for ( Map_t::const_iterator it = map.cbegin(); it != map.cend(); )
    {
        std::vector< Value_t > temp;
        const Map_t::const_iterator end = map.upper_bound( it->first );
        for ( ; it != end; ++it )
            temp.push_back( it->second );

        output.push_back( temp );
    }

    // Print the result
    for ( const std::vector< Value_t >& values : output )
    {
        for ( const Value_t& value : values )
            std::cout << value << " ";

        std::cout << std::endl;
    }
}

输出:

1 3 2
5 2

所以,现在我想知道是否有 faster/better 方法。

这是我的尝试。

证明在这里:http://goo.gl/JVpHw9

#include <unordered_map>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
    typedef std::string Key_t;
    typedef int Value_t;
    typedef std::unordered_multimap<Key_t, Value_t> Map_t;

    const Map_t map = {
        { "A", 1 }, 
        { "A", 3 }, 
        { "A", 2 },
        { "B", 5 }, 
        { "B", 2 },
    };

    std::vector< std::vector< Value_t > > output;

    for (auto it = map.begin(); it != map.end(); )
    {
        auto er = map.equal_range(it->first);
        auto tmp = std::vector< Value_t >{};
        for( ; it != er.second ; ++it) {
            tmp.push_back(it->second);
        };
        output.push_back(std::move(tmp));
    }
    // Print the result
    for ( const std::vector< Value_t >& values : output )
    {
        for ( const Value_t& value : values )
            std::cout << value << " ";

        std::cout << std::endl;
    }
}

通常的多图迭代应该在这里工作:

std::vector<std::vector<T>> out;

for (auto it1 = m.begin(), it2 = it1, end = m.end(); it1 != end; it1 = it2)
{
    out.emplace_back();
    for ( ; it1->first == it2->first; ++it2)
    {
        out.back().push_back(it2->second);
    }
}

以下适合我的作品:

#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>

int main () {
    std::unordered_multimap<std::string, int> map;
    map = {
        { "A", 1 }, 
        { "A", 3 }, 
        { "A", 2 },
        { "B", 5 }, 
        { "B", 2 },
    };

    std::vector<int> values;

    std::transform(map.begin(), map.end(),
                   std::back_inserter(values),
                   [](std::pair<std::string, int> element) {
                       return element.second;
                   });

    for (auto const& value : values) {
        std::cout << value << std::endl;
    }

    return 0;
}