#include <utility>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
template < typename container_type>
typename container_type:: iterator find_by_key( container_type& container,
const typename container_type:: value_type :: first_type & key)
{
typedef typename container_type:: value_type element_type;
return std:: find_if ( std:: begin ( container) , std:: end ( container) ,
[ & ] ( const element_type& k) { return key == k.first ; } ) ;
}
int main( )
{
typedef std:: pair < unsigned , std:: string > element_type;
std:: vector < element_type> v =
{
{ 1 , "One" } ,
{ 3 , "Three" } ,
{ 5 , "Five" } ,
{ 7 , "Seven" } ,
{ 9 , "Nine" } ,
{ 10 , "Ten" } ,
{ 8 , "Eight" } ,
{ 6 , "Six" } ,
{ 4 , "Four" } ,
{ 2 , "Two" }
} ;
std:: cout << "By key:\n " ;
for ( unsigned i = 1 ; i < 11 ; ++ i)
{
std:: vector < element_type> :: iterator it = find_by_key( v, i) ;
std:: cout << '\t ' << i << " | " ;
if ( it ! = v.end ( ) )
std:: cout << it- > second << '\n ' ;
else
std:: cout << "(null)\n " ;
}
std:: cout << "\n \n By position:\n " ;
for ( unsigned i = 0 ; i < v.size ( ) ; ++ i)
{
std:: cout << "\t pos: " << i << ": " ;
std:: cout << v[ i] .first << " | " << v[ i] .second << '\n ' ;
}
}
I2luY2x1ZGUgPHV0aWxpdHk+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDxpb3N0cmVhbT4KCnRlbXBsYXRlIDx0eXBlbmFtZSBjb250YWluZXJfdHlwZT4KdHlwZW5hbWUgY29udGFpbmVyX3R5cGU6Oml0ZXJhdG9yIGZpbmRfYnlfa2V5KGNvbnRhaW5lcl90eXBlJiBjb250YWluZXIsIAoJCQkJY29uc3QgdHlwZW5hbWUgY29udGFpbmVyX3R5cGU6OnZhbHVlX3R5cGU6OmZpcnN0X3R5cGUmIGtleSkgCnsKICAgIHR5cGVkZWYgdHlwZW5hbWUgY29udGFpbmVyX3R5cGU6OnZ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