Re: Multimap: how to get a key list?
On Feb 28, 5:09 pm, Sam <s...@email-scan.com> wrote:
Rui Maciel writes:
Is there any way to get a list of keys from a multimap besides relying =
on a couple of nested
loops to assemble that list?
What nested loops? Only one loop is required to iterate over the multimap=
..
There is no single function that gives you a set of all keys stored in th=
e
multimap, but a single loop is all that's needed to retrieve all the keys=
..
It's fairly easy to define a template function that gives them to you,
something like this:
template<typename multimap_t>
void keys(const multimap_t &m,
std::set<typename multimap_t::key_type> &k)
{
for (typename multimap_t::const_iterator b(m.begin()), e(m.end())=
;
b != e; ++b)
{
k.insert(b->first);
}
}
The std::set automatically takes care of deduping the multimap's keys.
I think that works fine. This might speed it up a little:
template<typename multimap_t>
void keys(const multimap_t &m,
std::set<typename multimap_t::key_type> &k)
{
typename std::set<typename multimap_t::key_type>::iterator
keys_end = k.end();
for (typename multimap_t::const_iterator b(m.begin()),
e(m.end());
b != e; ++b)
{
k.insert(keys_end, b->first);
}
}
That would work best if k is being passed in
as an empty container as seems likely.
Brian Wood
http://webEbenezer.net
(651) 251-9384