2011-01-13 39 views
4

是否可以執行以下操作。Boost元組+變換

說我升壓元組具有<std::string, T>

我想使用std ::變換+ mem_fun在相應的矢量僅插入的std :: string元素。是否有可能還是我們需要使用一個循環和的push_back(獲得< 0>)...

即下面不喜歡編譯...(未知類型...)

result.resize(storage.size()) 
std::transform(storage.begin(), storage.end(), result.begin(), std::mem_fun(&boost::get<0>)); 

這裏是(嘗試一個評論)一個例子:

#include <boost/tuple/tuple.hpp> 
#include <vector> 
#include <string> 
#include <algorithm> 
#include <boost/bind.hpp> 

template <typename T> 
class TestClass 
{ 
private: 
    typedef boost::tuple<std::string,T> PairType; 
    std::vector<PairType> storage; 
public: 
    void extract(std::vector<std::string> &result) 
    { 
     result.resize(storage.size()); 
     std::transform(storage.begin(), storage.end(), result.begin(), boost::bind(&PairType::get<0>, _1)); 
    } 
}; 

int main(int argc, char**argv) 
{ 

    TestClass<int> bb; 
    std::vector< std::string> result; 
    bb.extract(result); 
    return 0; 
} 

g++ test.cpp 
test.cpp: In member function `void TestClass<T>::extract(std::vector<std::string, std::allocator<std::string> >&)': 
test.cpp:17: error: expected primary-expression before ',' token 
test.cpp: In member function `void TestClass<T>::extract(std::vector<std::string, std::allocator<std::string> >&) [with T = int]': 
test.cpp:26: instantiated from here 
test.cpp:17: error: address of overloaded function with no contextual type information 
+1

您需要提供更多的代碼和編譯器嘔吐。 – 2011-01-13 01:42:54

回答

1

類型所需的get<0>超載是:

const std::string& (*)(const boost::tuples::cons<std::string, boost::tuples::cons<int, boost::tuples::null_type> >&) 

如果typedef,爲get0_fn_t,那麼你可以聲明一個指向該get<0>超載有:

get0_fn_t getter_fn = &boost::tuples::get<0, std::string, boost::tuples::cons<int, boost::tuples::null_type> >; 

編輯:這個程序是一個完整的實例:

#include <algorithm> 
#include <cstdlib> 
#include <iostream> 
#include <iterator> 
#include <string> 
#include <vector> 
#include <boost/bind.hpp> 
#include <boost/tuple/tuple.hpp> 

int main() 
{ 
    typedef boost::tuple<std::string, int> tuple_type; 
    std::vector<tuple_type> tuples; 
    tuples.push_back(boost::make_tuple(std::string("test3"), 3)); 
    tuples.push_back(boost::make_tuple(std::string("test0"), 0)); 

    std::vector<std::string> strings; 
    typedef const std::string& (*get0_fn_t)(const boost::tuples::cons<std::string, boost::tuples::cons<int, boost::tuples::null_type> >&); 
    get0_fn_t getter_fn = &boost::tuples::get<0, std::string, boost::tuples::cons<int, boost::tuples::null_type> >; 
    std::transform(tuples.begin(), tuples.end(), std::back_insert_iterator<std::vector<std::string> >(strings), getter_fn); 

    std::vector<std::string>::const_iterator it, end = strings.end(); 
    for (it = strings.begin(); it != end; ++it) 
     std::cout << *it << std::endl; 

    return EXIT_SUCCESS; 
} 

EDIT2:這說明如何將它集成到TestClass模板:

template <typename T> 
class TestClass 
{ 
private: 
    typedef boost::tuple<std::string, T> PairType; 
    std::vector<PairType> storage; 

public: 
    void extract(std::vector<std::string>& result) const 
    { 
     result.clear(); 
     typedef const std::string& (*get0_fn_t)(const boost::tuples::cons<std::string, boost::tuples::cons<T, boost::tuples::null_type> >&); 
     get0_fn_t getter_fn = &boost::tuples::get<0, std::string, boost::tuples::cons<T, boost::tuples::null_type> >; 
     std::transform(storage.begin(), storage.end(), result.begin(), getter_fn); 
    } 
}; 
3

使用的get和Boost.Bind成員版本。我已經測試過它,它的工作原理,它的價值。

#include <algorithm> 
#include <iostream> 
#include <iterator> 
#include <string> 
#include <vector> 

#include <boost/bind.hpp> 
#include <boost/tuple/tuple.hpp> 

int main() 
{ 
    typedef boost::tuple<std::string,int> T; 
    std::vector<T> v1; 
    v1.push_back(T("Blah", 23)); 
    v1.push_back(T("Wibble", 9)); 

    std::vector<std::string> v2; 
    std::transform(v1.begin(), v1.end(), std::back_inserter(v2), boost::bind(&T::get<0>, _1)); 

    std::copy(v2.begin(), v2.end(), std::ostream_iterator<std::string>(std::cout, "\n")); 

    return 0; 
} 
+0

好的,如果我們有int作爲模板參數,那麼怎麼樣? – 2011-01-13 02:08:06

+0

+1非常好的解決方案 – 2011-01-13 02:10:06