How to use the search algorithm for a vector

advertisements

If the vector's element is a pair type, like vector<pair<int, double>>. I want to the find algorithm focus on the first element of my vector. How can I do this?

For example, the following is my data:

<1, 2>

<3, 5>

<3, 4>
...

I want the find 1 in the first column.

Thanks,


Going out of my way to make the answer generic:

template <typename K>
struct match_first
{
    const K _k; match_first(const K& k) : _k(k) {}
    template <typename V>
        bool operator()(const std::pair<K, V>& el) const
    {
        return _k == el.first;
    }
};

use it like, e.g.

it = std::find_if(vec.begin(), vec.begin(), match_first<int>(1));

if (it!=vec.end())
{
    // found
}