468,484 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

How to find_if in a map?

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
Aug 9 '06 #1
4 14445
Pierre Couderc wrote:
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
Aug 9 '06 #2

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, stringtestMap;
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;

}

Aug 9 '06 #3
Pierre Couderc wrote:
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
Aug 9 '06 #4
In article <eb***********@biggoron.nerim.net>, pi****@couderc.cc says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by mikets | last post: by
3 posts views Thread by marco_segurini | last post: by
4 posts views Thread by Michael | last post: by
5 posts views Thread by dave_if | last post: by
18 posts views Thread by ma740988 | last post: by
3 posts views Thread by devel | last post: by
1 post views Thread by AlanJSmith | last post: by
10 posts views Thread by JDT | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.