Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate a vector with all multimap values with a given key

Tags:

c++

stl

Given a multimap<A,B> M what's a neat way to create a vector<B> of all values in M with a specific key.

e.g given a multimap how can I get a vector of all strings mapped to the value 123?

An answer is easy, looping from lower->upper bound, but is there a neat loop-free method?

like image 649
Mr. Boy Avatar asked Apr 02 '10 17:04

Mr. Boy


2 Answers

Here's the way to do it STL style :

// The following define is needed for select2nd with DinkumWare STL under VC++
#define _HAS_TRADITIONAL_STL 1

#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <functional>
#include <map>
#include <iterator>
#include <iostream>

using namespace std;

void main()
{
    typedef multimap<string, int> MapType;
    MapType m;
    vector<int> v;

    // Test data
    for(int i = 0; i < 10; ++i)
    {
        m.insert(make_pair("123", i * 2));
        m.insert(make_pair("12", i));
    }

    MapType::iterator i = m.lower_bound("123");
    MapType::iterator j = m.upper_bound("123");

    transform(i, j, back_inserter(v), select2nd<MapType::value_type>());

    copy(v.begin(), v.end(),  ostream_iterator<int>(cout, ","));

}
like image 86
rep_movsd Avatar answered Sep 30 '22 19:09

rep_movsd


Let's go lambda

given: multimap<A,B> M

requested: vector<B> (of all values in M with a specific key 'a'.)

method:

std::pair<M::iterator, M::iterator> aRange = M.equal_range('a')
std::vector<B> aVector;
std::transform(aRange.first, aRange.second,std::back_inserter(aVector), [](std::pair<A,B> element){return element.second;});         

System environment:

  1. compiler: gcc (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413 (with -std=c++11)
  2. os: ubuntu 16.04

Code example:

#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <functional>
#include <iostream>

int main()
{
    typedef std::multimap<std::string, int> MapType;
    MapType m;
    std::vector<int> v;

    /// Test data
    for(int i = 0; i < 10; ++i)
    {
        m.insert(std::make_pair("123", i * 2));
        m.insert(std::make_pair("12", i));
    }

    std::pair<MapType::iterator,MapType::iterator> aRange = m.equal_range("123");

    std::transform(aRange.first, aRange.second, std::back_inserter(v), [](std::pair<std::string,int> element){return element.second;});

    for(auto & elem: v)
    {
        std::cout << elem << std::endl;
    }
    return 0;
}
like image 33
Patricio Astudillo Avatar answered Sep 30 '22 20:09

Patricio Astudillo