inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_vpair 00005 #define inlib_vpair 00006 00007 #include <vector> 00008 00009 namespace inlib { 00010 00011 template <class K,class V> 00012 inline void add(std::vector< std::pair<K,V> >& a_vec, 00013 const K& a_key,const V& a_value) { 00014 typedef typename std::vector< std::pair<K,V> >::iterator it_t; 00015 it_t it; 00016 for(it=a_vec.begin();it!=a_vec.end();++it) { 00017 if((*it).first==a_key) { 00018 (*it).second = a_value; //override. 00019 return; 00020 } 00021 } 00022 //not found, add a new pair : 00023 a_vec.push_back(std::pair<K,V>(a_key,a_value)); 00024 } 00025 00026 template <class K,class V> 00027 inline bool find(const std::vector< std::pair<K,V> >& a_vec, 00028 const K& a_key,V& a_value) { 00029 typedef typename std::vector< std::pair<K,V> >::const_iterator it_t; 00030 it_t it; 00031 for(it=a_vec.begin();it!=a_vec.end();++it) { 00032 if((*it).first==a_key) { 00033 a_value = (*it).second; 00034 return true; 00035 } 00036 } 00037 a_value = V(); 00038 return false; 00039 } 00040 00041 } 00042 00043 #endif