Searching through a vector of tuples...
typedef boost::tuples::tuple<int,double> some_tuple; std::vector<some_tuple> vec_of_some_tuples; // ... stuff some data into the vector // ... search for a specific int in the vector of some_tuple What would be a good method, using an stl algorithm, to search through the vector above to match only the int portion of the tuple (ignoring the double)? Thanks, Graham
on Mon Aug 13 2007, "Graham Reitz" <graham.cpp-AT-gmail.com> wrote:
typedef boost::tuples::tuple<int,double> some_tuple;
std::vector<some_tuple> vec_of_some_tuples;
// ... stuff some data into the vector
// ... search for a specific int in the vector of some_tuple
What would be a good method, using an stl algorithm, to search through the vector above to match only the int portion of the tuple (ignoring the double)?
Use std::find with a boost::transform_iterator over a function returning the first member of the tuple, then extract the resulting iterator's base() iterator. HTH, -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com
hmm haven't really tested it because i just dled boost lambda_tuple in the library but if it works than somehting like std::find_if(vec_of_some_tuples.begin(), vec_of_some_tuples.end(), boost::lambda::get<0>(boost::lambda::_1) == 5); On 8/13/07, David Abrahams <dave@boost-consulting.com> wrote:
on Mon Aug 13 2007, "Graham Reitz" <graham.cpp-AT-gmail.com> wrote:
typedef boost::tuples::tuple<int,double> some_tuple;
std::vector<some_tuple> vec_of_some_tuples;
// ... stuff some data into the vector
// ... search for a specific int in the vector of some_tuple
What would be a good method, using an stl algorithm, to search through the vector above to match only the int portion of the tuple (ignoring the double)?
Use std::find with a boost::transform_iterator over a function returning the first member of the tuple, then extract the resulting iterator's base() iterator.
HTH,
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
The following appears to work (it compiles :)): #include <boost/tuple/tuple.hpp> #include <boost/bind.hpp> #include <vector> #include <algorithm> #include <cassert> template<int N, class tuple_type> inline typename boost::tuples::element<N, tuple_type>::type & get(tuple_type & t) { return boost::tuples::get<N>(t); } int main(const int argc, const char ** argv) { typedef boost::tuples::tuple<int, double> some_tuple; std::vector<some_tuple> vec_of_some_tuples; std::vector<some_tuple>::iterator result(std::find_if(vec_of_some_tuples.begin(), vec_of_some_tuples.end(), boost::bind(get<0, some_tuple>, ::_1) == 42)); assert(result != vec_of_some_tuples.end()); return 0; } Where 42 is the item you're looking for. See this post for original get(): http://lists.boost.org/boost-users/2007/01/24529.php Otherwise, use a map? Richard On 8/14/07, David Abrahams <dave@boost-consulting.com> wrote:
on Mon Aug 13 2007, "Graham Reitz" <graham.cpp-AT-gmail.com> wrote:
typedef boost::tuples::tuple<int,double> some_tuple;
std::vector<some_tuple> vec_of_some_tuples;
// ... stuff some data into the vector
// ... search for a specific int in the vector of some_tuple
What would be a good method, using an stl algorithm, to search through the vector above to match only the int portion of the tuple (ignoring the double)?
Use std::find with a boost::transform_iterator over a function returning the first member of the tuple, then extract the resulting iterator's base() iterator.
HTH,
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thanks Folks! I really appreciate the help! That's just what I needed. Graham On 8/13/07, Richard Dingwall <rdingwall@gmail.com> wrote:
The following appears to work (it compiles :)):
#include <boost/tuple/tuple.hpp> #include <boost/bind.hpp> #include <vector> #include <algorithm> #include <cassert>
template<int N, class tuple_type> inline typename boost::tuples::element<N, tuple_type>::type & get(tuple_type & t) { return boost::tuples::get<N>(t); }
int main(const int argc, const char ** argv) {
typedef boost::tuples::tuple<int, double> some_tuple;
std::vector<some_tuple> vec_of_some_tuples;
std::vector<some_tuple>::iterator result(std::find_if(vec_of_some_tuples.begin(), vec_of_some_tuples.end(), boost::bind(get<0, some_tuple>, ::_1) == 42));
assert(result != vec_of_some_tuples.end());
return 0; }
Where 42 is the item you're looking for. See this post for original get(): http://lists.boost.org/boost-users/2007/01/24529.php
Otherwise, use a map?
Richard
On 8/14/07, David Abrahams <dave@boost-consulting.com > wrote:
on Mon Aug 13 2007, "Graham Reitz" <graham.cpp-AT-gmail.com> wrote:
typedef boost::tuples::tuple<int,double> some_tuple;
std::vector<some_tuple> vec_of_some_tuples;
// ... stuff some data into the vector
// ... search for a specific int in the vector of some_tuple
What would be a good method, using an stl algorithm, to search through the vector above to match only the int portion of the tuple (ignoring the double)?
Use std::find with a boost::transform_iterator over a function returning the first member of the tuple, then extract the resulting iterator's base() iterator.
HTH,
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
chun ping wang
-
David Abrahams
-
Graham Reitz
-
Richard Dingwall