首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Boost tuple +转换

Boost tuple +转换
EN

Stack Overflow用户
提问于 2011-01-13 09:29:12
回答 2查看 2.2K关注 0票数 4

可以做到以下几点吗?

假设我的boost元组具有<std::string, T>

我想使用std::transform + mem_fun在相应的向量中只插入std::string元素。我们是否可以使用循环和push_back(get<0>)...

也就是说下面的代码不喜欢编译...(未知类型...)

代码语言:javascript
复制
result.resize(storage.size())
std::transform(storage.begin(), storage.end(), result.begin(), std::mem_fun(&boost::get<0>));

下面是一个例子(尝试其中一条评论):

代码语言:javascript
复制
#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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-13 10:00:50

您需要的get<0>重载类型为:

代码语言:javascript
复制
const std::string& (*)(const boost::tuples::cons<std::string, boost::tuples::cons<int, boost::tuples::null_type> >&)

如果您将其typedefget0_fn_t,则可以使用以下命令声明指向此get<0>重载的指针:

代码语言:javascript
复制
get0_fn_t getter_fn = &boost::tuples::get<0, std::string, boost::tuples::cons<int, boost::tuples::null_type> >;

EDIT:此程序是一个完整的工作示例:

代码语言:javascript
复制
#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模板中:

代码语言:javascript
复制
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);
    }
};
票数 1
EN

Stack Overflow用户

发布于 2011-01-13 09:57:58

使用get和Boost.Bind的成员版本。我已经测试过了,不管它有什么价值,它都能工作。

代码语言:javascript
复制
#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;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4675930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档