将multimap <key,value>转换为vector <vector <value>&gt; </vector <value> </key,value>

时间:2015-04-12 20:08:53

标签: c++ c++11 stl c++14

我需要将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

所以,现在我想知道是否有更好/更好的方式。

3 个答案:

答案 0 :(得分:1)

这是我的尝试。

证据就在这里: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;
    }
}

答案 1 :(得分:1)

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

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);
    }
}

答案 2 :(得分:0)

以下对我有用的东西

#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;
}
相关问题