gpt4 book ai didi

c++ - 融合 vector 投影

转载 作者:行者123 更新时间:2023-11-30 04:14:59 35 4
gpt4 key购买 nike

我有一个融合 vector ,其元素具有多个不同类型的成员数据,我想创建一个新的融合 vector ,它将仅投影特定的数据成员。我已经对此进行了一段时间的研究,但没有任何进展。

#include <iostream>
#include <string>
#include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/fusion/include/fold.hpp>
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/algorithm.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/bind.hpp>

namespace phx = boost::phoenix;
namespace fusion = boost::fusion;
using namespace phx::arg_names;

struct mprint
{
template<typename T>
void operator()(T& t) const
{
std::cout << t << std::endl;
}
};

struct TStruct
{
std::string val1_;
double val2_;
int val3_;
bool val4_;
TStruct( const std::string &val1, double val2, int val3, bool val4 ) :
val1_(val1), val2_(val2), val3_(val3), val4_(val4) { }
};

template <typename Sequence>
void viewVal1( const std::string &dummy,const Sequence& args )
{
fusion::for_each( boost::tie(phx::bind(&TStruct::val1_, arg1)(args)), mprint() );
}

template <typename Sequence>
void viewVal2( const std::string &dummy,const Sequence& args )
{
//fusion::for_each( boost::tie(phx::bind(&TStruct::val2_, arg1)(args)), mprint() );
}

int main( int argc, char* argv[] )
{
auto mdata = fusion::make_vector(
TStruct( "test1", 2.3, 1, true ),
TStruct( "test2", 3.3, 2, false ),
TStruct( "test3", 4.3, 3, true )
);

viewVal1( "dummy", mdata );
viewVal2( "dummy", mdata );

return 0;
}

最佳答案

我认为(意思是我不确定)您需要使用的是 fusion::transform_view :

Running on Coliru :

#include <iostream>
#include <string>
#include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/fusion/include/fold.hpp>
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/algorithm.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/bind.hpp>

namespace phx = boost::phoenix;
namespace fusion = boost::fusion;
using namespace phx::arg_names;

struct mprint
{
template<typename T>
void operator()(const T& t) const
{
std::cout << t << std::endl;
}
};

struct get_val1
{
template <typename T>
std::string operator()(const T& t) const
{
return t.val1_;
}
};

struct TStruct
{
std::string val1_;
double val2_;
int val3_;
bool val4_;
TStruct( const std::string &val1, double val2, int val3, bool val4 ) :
val1_(val1), val2_(val2), val3_(val3), val4_(val4) { }
};

template <typename Sequence>
void viewVal1( const std::string &dummy, Sequence& args )
{
typedef fusion::transform_view<Sequence, get_val1> project;
fusion::for_each(project(args,get_val1()), mprint() );
}

template <typename Sequence>
void viewVal2( const std::string &dummy, Sequence& args )
{
typedef fusion::transform_view<Sequence,decltype(phx::bind(&TStruct::val2_,arg1))> project;
fusion::for_each( project(args,phx::bind(&TStruct::val2_, arg1)), mprint() );
}

int main( int argc, char* argv[] )
{
auto mdata = fusion::make_vector(
TStruct( "test1", 2.3, 1, true ),
TStruct( "test2", 3.3, 2, false ),
TStruct( "test3", 4.3, 3, true )
);

viewVal1( "dummy", mdata );
viewVal2( "dummy", mdata );

return 0;
}

关于c++ - 融合 vector 投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18601358/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com