I have a STL::multimap and I search to populate a std::list with value which key is duplicated.
Can I find/insert to a std::list the value of elements for all key where count 1 without counting them one by one?
std::multimap<int, std::string> mm ;
mm[0] = "a" ;
mm[1] = "b" ;
mm[0] = "c" ;
mm[2] = "j" ;
mm[2] = "k" ;
std::list<std::string> lst ;
lst might contains "a" ,"c","j","k" ;
I try this
template <class K, class V>
class extract_value {
private:
K last_key_ ;
std::list<V> m_list_value ;
std::pair<K, V> first_elem ;
public:
extract_value(const K& k_): last_key_(k_) { }
void operator() (std::pair<const K, V> elem)
{
if (last_key_ == elem.first)
{
m_list_value.push_back(elem.second) ;
}
else
{
// First entry
last_key_ = elem.first;
first_elem= elem ;
}
}
std::list<V> get_value() { return m_list_value ; }
};
ex_ = for_each(mm.begin(),mm.end(), extract_value<int, std::string>(0)) ;
std::list<std::string> lst = ex_.get_value() ;
I'm not sure that this code compile.