0

I want to convert std::vector<std::pair<const K, V>*> to std::vector<std::pair<const K, V>>. However, as you know due to pair<const K, V> I can't assign.

I guess there can be someway to leverage

template< class InputIt >
void assign( InputIt first, InputIt last );

to make this happen. However, I can't find a way out.

Look for advice. Thanks!!

Nan Hua
  • 3,414
  • 3
  • 17
  • 24

1 Answers1

0

The const is only an issue if you try to assign elements or call certain functions like insert. But push_back and emplace_back will be fine.

One solution:

template <typename Elem>
std::vector<Elem> deref_vec_elems( const std::vector<Elem*>& v ) {
    std::vector<Elem> v2;
    for ( Elem* e : v )
        v2.push_back( *e );
    return v2;
}
aschepler
  • 70,891
  • 9
  • 107
  • 161