H
hurcan solter
Consider the following snippet
#include <map>
#include <iostream>
#include <algorithm>
#include <iterator>
typedef std::multimap<int ,int> map_type;
template <typename Pair>
struct select2nd
{
typedef Pair argument_type ;
typedef typename Pair::second_type result_type ;
const result_type &
operator()(const argument_type &p) const
{
return p.second ;
}
} ;
int main()
{
map_type mymap;
map_type::iterator iter;
iter = mymap.insert(map_type::value_type(1,1));
iter = mymap.insert(iter,map_type::value_type(1,2));
iter =mymap.insert(iter,map_type::value_type(1,3));
iter =mymap.insert(iter,map_type::value_type(1,4));
iter =mymap.insert(iter,map_type::value_type(1,5));
std::transform(mymap.begin(),mymap.end(),std:stream_iterator<int>
(std::cout," ")
,select2nd<map_type::value_type>());
};
prints out as 5 4 3 2 1 whereas the insert without hint sorts the key
as 1 2 3 4 5
which is the desired behaviour for me.
according to defect report ;
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1780.html
insertion before the hint is the sensible behaviour , but i need the
keys sorted as
in the order as they are added(they are guaranteed to be in ascending
order) to the multimap and also need the performance improvement.
(tens or hundred of thousands of entries)
so doing something like
iter = mymap.insert(map_type::value_type(1,1));
iter = mymap.insert(++iter,map_type::value_type(1,2));
....
do you think i still get the performance boost or should I stick with
the hintless version
Thanks in advance
Hurcan
#include <map>
#include <iostream>
#include <algorithm>
#include <iterator>
typedef std::multimap<int ,int> map_type;
template <typename Pair>
struct select2nd
{
typedef Pair argument_type ;
typedef typename Pair::second_type result_type ;
const result_type &
operator()(const argument_type &p) const
{
return p.second ;
}
} ;
int main()
{
map_type mymap;
map_type::iterator iter;
iter = mymap.insert(map_type::value_type(1,1));
iter = mymap.insert(iter,map_type::value_type(1,2));
iter =mymap.insert(iter,map_type::value_type(1,3));
iter =mymap.insert(iter,map_type::value_type(1,4));
iter =mymap.insert(iter,map_type::value_type(1,5));
std::transform(mymap.begin(),mymap.end(),std:stream_iterator<int>
(std::cout," ")
,select2nd<map_type::value_type>());
};
prints out as 5 4 3 2 1 whereas the insert without hint sorts the key
as 1 2 3 4 5
which is the desired behaviour for me.
according to defect report ;
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1780.html
insertion before the hint is the sensible behaviour , but i need the
keys sorted as
in the order as they are added(they are guaranteed to be in ascending
order) to the multimap and also need the performance improvement.
(tens or hundred of thousands of entries)
so doing something like
iter = mymap.insert(map_type::value_type(1,1));
iter = mymap.insert(++iter,map_type::value_type(1,2));
....
do you think i still get the performance boost or should I stick with
the hintless version
Thanks in advance
Hurcan