Re: copy from keys from multimap into the vector
On Oct 29, 8:33 pm, puzzlecracker <ironsel2...@gmail.com> wrote:
I am using while loop for that but I am sure you can do it
quicker and more syntactically clear with copy function.
Here is what I do and would like to if someone has a cleaner
solution:
vector<string> vec;
multimap<stirng, int> myMap
// populate myMap
multimap<string, int >::iterator iter = myMap.begin();
while(iter != myMap.end())
{
vec.push_back(iter->first)
}
Do you really want multiple entries in the vector when there are
multiple entries for a single key in the map? If so, something
like the following should work:
template< typename Pair >
struct First
{
typedef Pair argument_type ;
typedef typename Pair::first_type
result_type ;
typename Pair::first_type
operator()( Pair const& obj ) const
{
return obj.first ;
}
} ;
and then:
typedef First< Map::value_type >
Mapper ;
typedef boost::transform_iterator< Mapper, Map::const_iterator >
InitIter ;
std::vector< std::string >
k( InitIter( m.begin(), Mapper() ),
InitIter( m.end(), Mapper() ) ) ;
If you only want each unique key to appear once, then you should
be able to use a boost::filter_iterator on the
transform_iterator.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34