1

I would like to complete the following functions

#include <algorithm>
#include <iostream>
#include <list>
#include <map>

#include <boost/foreach.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/utility.hpp>

struct X
{
    X(int n, int m){a=n; b=m;}

    int a;
    int b;
};

int main(int argc, char* argv[])
{
    std::list<X> lstPP;
    std::list<std::pair<int, std::list<X>>> lstYY;

    X x1(1, 2);
    X x2(3, 4);
    X x3(4, 5);
    X x4(5, 6);
    X x5(6, 7);
    X x6(7, 8);

    std::list<X> lstA;
    lstA.push_back(x1);
    lstA.push_back(x2);
    lstYY.push_back(std::make_pair(1, lstA));

    std::list<X> lstB;
    lstB.push_back(x3);
    lstB.push_back(x4);
    lstYY.push_back(std::make_pair(6, lstB));

    std::list<X> lstC;
    lstC.push_back(x5);
    lstC.push_back(x6);
    lstYY.push_back(std::make_pair(2, lstC));

    std::list<X> lstD;
    lstD.push_back(x4);
    lstD.push_back(x2);
    lstYY.push_back(std::make_pair(6, lstD));

    int m1 = 6;

    //BOOST_FOREACH(BOOST_IDENTITY_TYPE((std::pair<int, std::list<X>>)) value, lstYY)
    //{
    //    if(value.first == m1)
    //    {
    //        boost::push_back(lstPP, value.second);
    //    }
    //}

    boost::push_back(lstPP, lstYY 
    | boost::adaptors::filtered(boost::bind(&std::pair<int, std::list<X>>::first, _1) == m1)
    | boost::adaptors::transformed(boost::bind(&std::pair<int, std::list<X>>::second, _2)));

    return 0;
}

I would like to use boost::adaptors chain should be how to do it

    boost::push_back(lstPP, lstYY 
    | boost::adaptors::filtered(boost::bind(&std::pair<int, std::list<X>>::first, _1) == m1)
    | boost::adaptors::transformed(boost::bind(&std::pair<int, std::list<X>>::second, _2)));

instead of

BOOST_FOREACH(BOOST_IDENTITY_TYPE((std::pair<int, std::list<X>>)) value, lstYY)
{
    if(value.first == m1)
    {
        boost::push_back(lstPP, value.second);
    }
}

But the application vs2010 compiler error:

    error C2664: 'void std::list<_Ty>::_Insert(std::_List_const_iterator<_Mylist>,const _Ty &)' : cannot convert parameter 2 from 'const std::list<_Ty>' to 'const X &'
1>          with
1>          [
1>              _Ty=X,
1>              _Mylist=std::_List_val<X,std::allocator<X>>
1>          ]
1>          and
1>          [
1>              _Ty=X
1>          ]

How to solve it? by the way can not use c++0x lambda, thanks

  • In case anyone is interested [this](http://melpon.org/wandbox/permlink/0NYRK9ebn4zARVMs) is a complete example that reproduces the error. – llonesmiz Sep 03 '16 at 13:17

1 Answers1

0

Your algorithm is broken in terms of performance--you have a sorted container but you do a linear search for a key. Instead, this code is much simpler and much faster (O(log n) instead of O(n)):

std::list<X> lstPP;
typedef std::map<int, std::list<X>> map_t;
map_t mapYY;
int m1 = 9;

map_t it = mapYY.find(m1);
if (it != mapYY.end())
{
    lstPP.push_back(it->second);
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • thanks you but I may not know the meaning of the expression I would send a post –  Sep 03 '16 at 11:22