
I have a map with key is a pair: typedef map < pair<int, int>, string> pairMapString; pair<int, int> key(1,3); map[key] = "one"; How can I create a binary function using boost lamba which check the first value of the pair of the key of the map? void add(pairMapString;& myMap, int x) { iterator& itr = find_if(myMap.begin(), myMap.end(), bind(equal_to < int >(), bind(&BlockDataPairMap::value_type::first::value_type::first, _1 ) , x) ); }

Meryl Silverburgh wrote:
I have a map with key is a pair: typedef map < pair<int, int>, string> pairMapString;
pair<int, int> key(1,3); map[key] = "one";
How can I create a binary function using boost lamba which check the first value of the pair of the key of the map?
void add(pairMapString;& myMap, int x) {
iterator& itr = find_if(myMap.begin(), myMap.end(), bind(equal_to < int >(), bind(&BlockDataPairMap::value_type::first::value_type::first, _1 ) , x) );
using namespace std; using namespace boost::lambda; typedef pair<int, int> intPair; typedef map < intPair, string> pairMapString; void add(pairMapString& myMap, int x) { pairMapString::iterator& itr = find_if(myMap.begin(), myMap.end(), bind(&intPair::first, bind(&pairMapString::value_type::first, _1)) == x); } KevinH -- Kevin Heifner heifner @ ociweb.com http://heifner.blogspot.com Object Computing, Inc. (OCI) www.ociweb.com

Thanks. I have problems compiling it. ../Test.cpp:74: error: invalid initialization of non-const reference of type 'std::_Rb_tree_iterator<std::pair<const std::pair<int, int>, std::basic_string<char, std::char_traits<char>, std::allocator<char> >
&' from a temporary of type 'std::_Rb_tree_iterator<std::pair<const std::pair<int, int>, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >' ../Test.cpp:71: warning: unused variable 'itr'
Here is my code: main() { typedef pair<int, int> intPair; typedef map < intPair, string> pairMapString; int x = 3; pairMapString myMap; pairMapString::iterator& itr = find_if(myMap.begin(), myMap.end(), bind(&intPair::first, bind(&pairMapString::value_type::first, _1)) == x); } On 2/22/06, Kevin Heifner <heifner_k@ociweb.com> wrote:
Meryl Silverburgh wrote:
I have a map with key is a pair: typedef map < pair<int, int>, string> pairMapString;
pair<int, int> key(1,3); map[key] = "one";
How can I create a binary function using boost lamba which check the first value of the pair of the key of the map?
void add(pairMapString;& myMap, int x) {
iterator& itr = find_if(myMap.begin(), myMap.end(), bind(equal_to < int >(), bind(&BlockDataPairMap::value_type::first::value_type::first, _1 ) , x) );
using namespace std; using namespace boost::lambda;
typedef pair<int, int> intPair; typedef map < intPair, string> pairMapString;
void add(pairMapString& myMap, int x) { pairMapString::iterator& itr = find_if(myMap.begin(), myMap.end(), bind(&intPair::first, bind(&pairMapString::value_type::first, _1)) == x); }
KevinH -- Kevin Heifner heifner @ ociweb.com http://heifner.blogspot.com Object Computing, Inc. (OCI) www.ociweb.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I have a function which takes a map (with int as key) and put all keys into another vector: void keySet2(map<int, string> aMap, vector<int>& keySet) { transform( aMap.begin(), aMap.end(), back_inserter(keySet), bind(&map<int, string>::value_type::first, _1) ); } But when I turn that find function into a template, I have compile erro. So is it possible to use boost in a template? here is my code: template <class T> void keySet(T& aMap, vector<int>& keySet) { transform( aMap.begin(), aMap.end(), back_inserter(keySet), bind(&T::value_type::first, _1) ); } here is the error: g++ -O0 -g3 -Wall -c -fmessage-length=0 -oBlockGrouper.o ../BlockGrouper.cpp ../Utils.h: In function âvoid keySet(T&, std::vector<int, std::allocator<int> >&)â: ../Utils.h:149: error: â_1â was not declared in this scope __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

template <class T> void keySet(T& aMap, vector<int>& keySet) { transform( aMap.begin(), aMap.end(), back_inserter(keySet), bind(&T::value_type::first, _1) );
}
here is the error: g++ -O0 -g3 -Wall -c -fmessage-length=0 -oBlockGrouper.o ../BlockGrouper.cpp ../Utils.h: In function 'void keySet(T&, std::vector<int, std::allocator<int> >&)': ../Utils.h:149: error: '_1' was not declared in this scope
Based on your errors it looks like _1 is not declared in the scope. Did you miss adding these: #include <boost/lambda/lambda.hpp> using boost::lambda::_1; -- Nitin Motgi
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Nitin Motgi

Thanks. I add the import and namespace, but I still get the same arror: ../Utils.h: In function âvoid keySet(T&, std::vector<int, std::allocator<int> >&)â: ../Utils.h:175: error: use of â_1â is ambiguous /usr/include/boost/lambda/core.hpp:69: error: first declared as âconst boost::lambda::placeholder1_type& boost::lambda::<unnamed>::_1â here --- nitin motgi <nitin.motgi@gmail.com> wrote:
template <class T> void keySet(T& aMap, vector<int>& keySet) { transform( aMap.begin(), aMap.end(), back_inserter(keySet), bind(&T::value_type::first, _1) );
}
here is the error: g++ -O0 -g3 -Wall -c -fmessage-length=0 -oBlockGrouper.o ../BlockGrouper.cpp ../Utils.h: In function 'void keySet(T&, std::vector<int, std::allocator<int> >&)': ../Utils.h:149: error: '_1' was not declared in this scope
Based on your errors it looks like _1 is not declared in the scope.
Did you miss adding these:
#include <boost/lambda/lambda.hpp> using boost::lambda::_1;
-- Nitin Motgi
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam
protection around
http://mail.yahoo.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Nitin Motgi
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

On 2/23/06, yinglcs2@yahoo.com <yinglcs2@yahoo.com> wrote:
Thanks. I add the import and namespace, but I still get the same arror:
../Utils.h: In function 'void keySet(T&, std::vector<int, std::allocator<int> >&)': ../Utils.h:175: error: use of '_1' is ambiguous /usr/include/boost/lambda/core.hpp:69: error: first declared as 'const boost::lambda::placeholder1_type& boost::lambda::<unnamed>::_1' here
--- nitin motgi <nitin.motgi@gmail.com> wrote:
<snip> Post a complete sample - that looks like you might have included boost/lambda/bind.hpp and boost/bind.hpp Stuart Dootson

Meryl Silverburgh wrote:
I have a map with key is a pair: typedef map < pair<int, int>, string> pairMapString;
pair<int, int> key(1,3); map[key] = "one";
How can I create a binary function using boost lamba which check the first value of the pair of the key of the map?
void add(pairMapString;& myMap, int x) {
iterator& itr = find_if(myMap.begin(), myMap.end(), bind(equal_to < int >(), bind(&BlockDataPairMap::value_type::first::value_type::first, _1 ) , x) );
myMap.lower_bound( make_pair( x, INT_MIN ) ) should give you what you need in logarithmic time, if I'm not mistaken.

I am not sure how low_bound() will work. My understanding is it only return the ** first ** instance. but I am looking for ** all instances ** which fit the condition (i.e. the first value of the key pair matches the input parameter). --- Peter Dimov <pdimov@mmltd.net> wrote:
Meryl Silverburgh wrote:
I have a map with key is a pair: typedef map < pair<int, int>, string> pairMapString;
pair<int, int> key(1,3); map[key] = "one";
How can I create a binary function using boost lamba which check the first value of the pair of the key of the map?
void add(pairMapString;& myMap, int x) {
iterator& itr = find_if(myMap.begin(), myMap.end(), bind(equal_to < int >(),
bind(&BlockDataPairMap::value_type::first::value_type::first,
_1 ) ,
x) );
myMap.lower_bound( make_pair( x, INT_MIN ) ) should give you what you need in logarithmic time, if I'm not mistaken.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

yinglcs2@yahoo.com wrote:
I am not sure how low_bound() will work. My understanding is it only return the ** first ** instance.
But so does find_if.
but I am looking for ** all instances ** which fit the condition (i.e. the first value of the key pair matches the input parameter).
Since a map is sorted by its key, you just need to proceed from the iterator lower_bound returns until you hit an element with .first != x (or end()). Anyway, ...
iterator& itr = find_if(myMap.begin(),
... you need to omit the reference here. iterator itr = find_if( ...

Thanks. I would like to use your suggestion (since it is simpler/cleaner). however, you said 'you just need to proceed from the iterator lower_bound returns until you hit an element with .first != x (or end()). ' How can I find the element with .first != x? I try using end(), but it does not work. it copies evething from (.fires == x) to the end of the list. --- Peter Dimov <pdimov@mmltd.net> wrote:
yinglcs2@yahoo.com wrote:
I am not sure how low_bound() will work. My understanding is it only return the ** first ** instance.
But so does find_if.
but I am looking for ** all instances ** which fit the condition (i.e. the first value of the key pair matches the input parameter).
Since a map is sorted by its key, you just need to proceed from the iterator lower_bound returns until you hit an element with .first != x (or end()). Anyway, ...
iterator& itr = find_if(myMap.begin(),
... you need to omit the reference here.
iterator itr = find_if( ...
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

yinglcs2@yahoo.com wrote:
Thanks. I would like to use your suggestion (since it is simpler/cleaner).
however, you said 'you just need to proceed from the iterator lower_bound returns until you hit an element with .first != x (or end()). '
How can I find the element with .first != x?
I try using end(), but it does not work. it copies evething from (.fires == x) to the end of the list.
Should be something like (untested): for( iterator it = m.lower_bound( make_pair( x, INT_MIN ) ); it != m.end() && it->first.first == x; ++it ) { // do something with *it }
participants (6)
-
Kevin Heifner
-
Meryl Silverburgh
-
nitin motgi
-
Peter Dimov
-
Stuart Dootson
-
yinglcs2@yahoo.com