How to find_if in a map?

P

Pierre Couderc

I have a
map<string, T*> MyMap;
and I want to find the element which contains a specific T*.

I thought of some

T* pT = &MyT
find(MyMap.begin(), MyMap.end(), xxx(pT));

But I do not success
 
P

Pierre Barbier de Reuille

Pierre said:
I have a
map<string, T*> MyMap;
and I want to find the element which contains a specific T*.

I thought of some

T* pT = &MyT
find(MyMap.begin(), MyMap.end(), xxx(pT));

But I do not success

You have to do something like:

typedet map<string,T*> mymap_t;
// Init MyMap
find(MyMap.begin(), MyMap.end(), make_pair(some_key, some_value) );

As the map<K,T>::iterator::value_type is pair<const K, T>.

Pierre
 
X

xiaohuamao

template <class _InputIter, class _Predicate>
inline _InputIter __find_if(_InputIter __first, _STLP_MPW_EXTRA_CONST
_InputIter __last,
_Predicate __pred,
const input_iterator_tag &)
{
while (__first != __last && !__pred(*__first))
++__first;
return __first;
}


Maybe this sample will help you:

#include <string>
#include <map>
#include <iostream>
using namespace std;

typedef map<int, string> testMap;
typedef map<int, string>::iterator testMapIter;

bool str2int (pair<const int,string> & iter){
return iter.second == "third";
}
int main()
{
testMap coll;

coll.insert(make_pair<int, string> (1, "first"));
coll.insert(make_pair<int, string> (2, "second"));
coll.insert(make_pair<int, string> (3, "third"));
coll.insert(make_pair<int, string> (4, "forth"));
coll.insert(make_pair<int, string> (5, "fifth"));

testMapIter iter = find_if(coll.begin(), coll.end(), str2int);
cout << iter->first << iter->second << endl;

}
 
K

Kai-Uwe Bux

Pierre said:
I have a
map<string, T*> MyMap;
and I want to find the element which contains a specific T*.

I thought of some

T* pT = &MyT
find(MyMap.begin(), MyMap.end(), xxx(pT));

You need find_if. See below.
But I do not success

#include <map>
#include <string>
#include <algorithm>
#include <iostream>

template < typename T >
struct match_second {

T val;

match_second ( T const & t )
: val ( t )
{}

template < typename Pair >
bool operator() ( Pair const & p ) const {
return ( val == p.second );
}

}; // match_second


typedef std::map< std::string, int > my_map;

int main ( void ) {
my_map m;
m[ "hello" ] = 1;
m[ "world" ] = 2;
my_map::const_iterator iter =
std::find_if( m.begin(), m.end(), match_second<int>(2) );
if ( iter != m.end() ) {
std::cout << iter->first << " --> " << iter->second<< '\n';
}
}


Best

Kai-Uwe Bux
 
J

Jerry Coffin

I have a
map<string, T*> MyMap;
and I want to find the element which contains a specific T*.

I thought of some

T* pT = &MyT
find(MyMap.begin(), MyMap.end(), xxx(pT));

As has already been mentioned, you can use find_if to do this.

What hasn't been mentioned is that find_if has linear complexity, so if
you're doing this very often, you'd be better off with a different data
structure, where you can search more directly for the specific T* you
need.

IIRC, Boost has some code to handle situations like this relatively
cleanly and give you fast lookups from either item.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top