473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 16023
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: mikets | last post by:
Hi there, I found out that very often I use the following construct: for (it = myvec.begin(); it != myvec.end(); ++it) { if (it->name() == "myname") break; } Certainly, the container the...
1
by: John Black | last post by:
Hi, In using find_if() you need to name a comparision function which normally is a static function, but sometimes it is really inconvinient for this, let's take this example, class MyClass{...
3
by: marco_segurini | last post by:
Hi, when the following code is executed bool Test1(); bool Test2(); .... if (Test1() || Test2()) { ... } ....
4
by: Michael | last post by:
Hi, I'm having a few problems getting the following find_if to work class Entity { }; class factory {
5
by: dave_if | last post by:
I have a vector of TCard objects, I am sorting them based on the the box field. That part works fine (finally!). I would then like to random_shuffle all cards that are in a particular box, that is,...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
3
by: devel | last post by:
Hello, Could someone tell me why the find_if statement applied to my multimap dictionnary is doesn't compile? Does this algorithm doesn't work on a multimap? I don't understand why the...
1
by: AlanJSmith | last post by:
can some clever guru help please. I am porting some C++ 6.0 projects to C++ VS2005 and find_if no longer exists in vector, i am not sure what to do. i am only at begginer level so please dont get...
3
by: Misiu | last post by:
Hello everybody, How to avoid using struct IsDataType for comparison for find_if algorithm but use something like that what is now commented out in the code below? Regards, Misiu ...
10
by: JDT | last post by:
Hi, The following find_if works fine to me except that I want the internal individual comparison performed backward (from m+5, m+4, ..., to m). I can use reverse() to reverse the array first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.