2013-12-21 6 views
1

나는 func1()으로 전달하고자하는 std::arrayboost::fusion::vector<X, Y>을 가지고 있습니다. 이 함수는 각 std::array 요소에 boost::fusion::vector<X, Y> 인스턴스를 추가합니다.boost :: fusion :: vector <x,y,z> 요소를 std :: array <boost :: fusion :: vector <x,y,z>>에 추가하는 방법은 무엇입니까?

올바른 숫자의 숫자를 fusion::vector<X,Y>에 추가 할 수 있도록 fusion::fold()을 사용해야합니다. 맞습니까?

그래서 나는 현재 이런 일이 :

void func1(){ 
    boost::fusion::vector<X,Y> my_vec; 
    std::array<boost::fusion::vector<X,Y> > my_array[10]; 
    func2(my_vec, my_array); 
} 

void func2(boost::fusion::vector<X,Y> my_vec, std::array<boost::fusion::vector<X,Y> > my_array){ 

    //THIS IS THE PART I AM UNSURE ABOUT 
    for(int k=0; k<10; k++){ 
     //The first two parameters aren't so important- just included to show the idea 
     my_array[k] = boost::fusion::fold(my_vec, 1, some_struct); 
    } 
} 

//This part is irrelevant 
struct some_struct 
{ 
    typedef int result_type; 

    template<typename T> 
    int operator()(int x, T& t) const 
    { 
     t = something(x); 
     //Not sure if this part needs to return a boost::fusion::vector<X, Y> 
     return x; 
    } 
}; 

여러 boost::fusion::vector<X,Y> 인스턴스를 생성하기 위해 my_vec의 서명을 사용하고 내가 추가 할 수 있도록 그들을 다시 반환하는 방법입니다에 대해 내가 확신하고있는 부분 배열에 func2().

누군가가 조언을 해주실 수 있습니까?

편집 - 방금 fold()에 대한 첫 번째 매개 변수가 있음을 발견하고 내 질문을 수정했습니다.

답변

3

나는 정말로 당신의 질문을 이해할 수 있을지 잘 모르겠다. 그래서 먼저 fold이 무엇을 설명하려고하는지 설명하겠습니다.

일반적으로 (융합뿐만 아니라) 두 개의 매개 변수를 취하는 함수를 "접는"것은 벡터의 각 요소와 벡터의 이전 요소에 함수를 적용한 결과에 적용합니다. 첫 번째 요소에 초기 값이 제공됩니다.

그래서 A f(A, B)으로 폴딩 기능을 정의하면,이 함수의 접힘에 해당 할 것이다 (4 개 요소)

f(f(f(f(A_initial_value, B_0), B_1), B_2), B_3); 

(대문자 프리픽스 형 단지 시행있다)

더 정확하게 퓨전 fold. boost::fusion::vector<> 안에있는 모든 요소에서 함수를 접을 것입니다.

my_array[k] = boost::fusion::fold(my_vec, 1, some_struct); 

my_array[k]는 숫자 값을받을 것이다, 그 이후로 컴파일되지 않습니다 : 당신이 할 때, 그래서

A_final_value = f(A_initial_value, X_value), Y_value); 

을하십시오 boost::fusion::vector<X, Y>std::tuple<X,Y>에 해당 따라 서로 다른 유형의 f를 호출합니다 당신은 그래서 fold을 명확히하기 위해 노력하고, 당신의 질문에 없는 말할 것 가지고 fusion::vector<X,Y>

로 정의하지만 한 내가 "fusion::vector<X,Y>에 올바른 수의 요소를 추가하십시오"라는 의미를 모르겠다는 것을 인정하십시오.

편집 : 의견에서 말한대로 업데이트합니다. 목표는 std::array<fusion::vector<int, int, int, int>>에 fibonacci 시퀀스를 생성하는 것입니다. 예를 들어 vectorarray으로 걸어 다니면 올바른 순서로 피보나치가 생깁니다.

fusion을 사용하면 vector의 요소를 반복하면서 상태를 통과해야하므로 fold이 좋았습니다. (내가 : 죄송합니다 ... 특별한 경우를 처리하기 위해 귀찮게하지 않았다 원인 첫 번째하지만 두 번째 요소의 피보나치 시퀀스를 시작하지 않음) 여기

내 이것에 대한 제안입니다 :

template <typename Value> 
struct Generator 
{ 
    // Easier to read and mandatory for fold 
    typedef typename std::tuple<Value, Value> result_type; 

    // The function that generate the fibonacci and update the Value 
    result_type operator()(result_type previous, Value& elem) const 
    { 
    elem = std::get<0>(previous) + std::get<1>(previous); 
    return std::make_tuple(std::get<1>(previous), elem); 
    } 
}; 

// Use template to be a bit more generic on array size and vector type 
template <typename Vector, size_t array_size, typename Value> 
void func2(std::array<Vector, array_size>& array, std::tuple<Value, Value> init) 
{ 
    // The state that will be fed across the fold function 
    auto previous = init; 
    for (auto& vect: array) 
    {   
    // Generate the fibonnaci value for every element of the vector starting 
    // from where the state is. The return value of fold is the new state 
    previous = boost::fusion::fold(vect, previous, Generator<Value>()); 
    } 
} 

// Tool to print the vector 
struct Printer 
{ 
    template <typename Elem> 
    void operator()(const Elem& elem) const 
    { 
    std::cout << elem << std::endl; 
    } 
}; 

// Use template to be a bit more generic on array size and vector type 
template <typename Vector, size_t array_size, typename Value> 
void func1(std::tuple<Value, Value> init) 
{  
    // Create the vector 
    std::array<Vector, array_size> array; 

    // FIll it with fibonacci 
    func2(array, init); 

    // Print it 
    for (auto vect: array) 
    {   
    boost::fusion::for_each(vect, Printer()); 
    } 
} 

http://rextester.com/XCXYX58360

+0

나는 수많은 fusion :: vector 을 만들고 싶습니다. 그리고이 각각의 인스턴스는 std :: array에 저장되어야합니다. 이게 도움이 되나요? – user3125975

+0

@ user3125975 예/아니요. 그것은 기본적으로 당신이 여기'std :: array > my_array [10];'에서하고있는 것이므로, 여러분은 그것들을 특정한 방법으로 초기화하고 싶다고 가정합니다. 왜 단지'my_array [k] = something()'이 아니며,'something'은'somestruct'에서 호출하는 하나의'fusion :: vector'를 반환하는 함수입니까? – Johan

+0

something()은 벡터에 넣을 요소의 개수를 어떻게 알 수 있습니까? my_vec의 형식을 "확인"해야합니까? – user3125975