Question: I'd like to copy values that match a predicate (equal ints) from a `map<string,int>` to a `vector<int>`. 

This is what I tried:

```
#include <map>
#include <vector>
#include <algorithm>

int main()
{
    std::vector< int > v;
    std::map< std::string, int > m;

    m[ "1" ] = 1;
    m[ "2" ] = 2;
    m[ "3" ] = 3;
    m[ "4" ] = 4;
    m[ "5" ] = 5;

    std::copy_if( m.begin(), m.end(), v.begin(),
                  [] ( const std::pair< std::string,int > &it )
                  {
                    return ( 0 == ( it.second % 2 ) );
                  }
                  );
}

```

The error message from g++ 4.6.1 is : 

```
error: cannot convert 'std::pair<const std::basic_string<char>, int>' to 'int' in assignment

```

Is there a way to adjust the example to do the above copy?

Answer: 
The compiler error is actually quite succinct:

```
error: cannot convert 'std::pair<const std::basic_string<char>, int>' to 'int' in assignment

```

And that's exactly what the problem is. The `map` you're copying from has iterators that dereference to a `pair<KEY,VALUE>`, and there's no way to implicitly *transform* a `pair<KEY,VALUE>` to just a `VALUE`.

Because of this, you can't use `copy` or `copy_if` to copy from a `map` to a `vector`; but the Standard Library does provide an algorithm you can use, creatively called `transform`. `transform` is very similar to `copy` in that it takes two source iterators and a destination iterator. The difference is `transform` also takes a unary function that does the actual transformation. Using a C++11 lambda, you can copy the entire contents of a `map` to a `vector` like this:

```
transform( m.begin(), m.end(), back_inserter(v), [] (const MyMap::value_type& vt)
{
  return vt.second;
});

```

What if you don't want to copy the entire contents of the `map`, but only some elements meeting certian criteria? Simple, just use `transform_if`.

What's that, you say? There is no `transform_if` in the Standard Library? Well yeah, you do have a point there. Frustratingly, there is no `transform_if` in the Standard Library. However [writing one](https://stackoverflow.com/questions/2999537/stl-writing-where-operator-for-a-vector/3000008#3000008) is a simple enough task. Here's the code:

```
template<class InputIterator, class OutputIterator, class UnaryFunction, class Predicate>
OutputIterator transform_if(InputIterator first, 
                            InputIterator last, 
                            OutputIterator result, 
                            UnaryFunction f, 
                            Predicate pred)
{
    for (; first != last; ++first)
    {
        if( pred(*first) )
            *result++ = f(*first);
    }
    return result; 
}

```

As you might expect, using `transform_if` is like taking `copy_if` and mashing it together with `transform`. Here's some psudo-code to demonstrate:

```
transform_if( m.begin(), m.end(), back_inserter(v),
  [] (const MyMap::value_type& vt) // The UnaryFunction takes a pair<K,V> and returns a V
  {
    return vt.second;
  }, [] (const MyMap::value_type& vt) // The predicate returns true if this item should be copied
  {
     return 0 == (vt.second%2);
  } );

```
Presumably you just want to retrieve the associated values from the `map`, not the keys.

The SGI version of STL has `select1st` and `select2nd` iterators for this kind of task.

Personally, however, I don't think this should really be done with copy -- you're transforming the data, not copying it. As such, I'd advise using `std::transform` with a functor to return the second item in the pair.