升压元组+变换 [英] Boost tuple + transform

查看:120
本文介绍了升压元组+变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能做到以下几点。

我说升压元组具有<的std ::字符串,T>

我想使用std ::变换+ mem_fun在相应的矢量仅插入的std ::字符串元素。是否有可能还是我们需要使用一个循环和的push_back(获得℃,>)...

也就是说,下不喜欢编译...(未知类型...)

  result.resize(storage.size())
性病::变换(storage.begin(),storage.end(),result.begin()的std :: mem_fun(安培;提高::获得℃的>));

下面是一个例子(尝试一个评论)

 的#include<升压/元组/ tuple.hpp>
#包括LT&;矢量>
#包括LT&;串GT;
#包括LT&;&算法GT;
#包括LT&;升压/ bind.hpp>模板< typename的T>
类识别TestClass
{
私人的:
    TYPEDEF提振::元组LT;的std ::字符串,T> PairType;
    的std ::矢量<&PairType GT;存储;
上市:
    无效提取物(的std ::矢量<标准::字符串>&放大器;结果)
    {
        result.resize(storage.size());
        性病::变换(storage.begin(),storage.end(),result.begin()的boost ::绑定(安培; PairType ::获得℃的>中_1));
    }
};INT主(INT ARGC,字符** argv的)
{    识别TestClass< INT> BB;
    的std ::矢量<标准::字符串>结果;
    bb.extract(结果);
    返回0;
}G ++ TEST.CPP
TEST.CPP:在成员函数'无效的TestClass< T> ::提取物(的std ::矢量<标准::字符串,性病::分配器<标准::字符串>>&安培;):
TEST.CPP:17:错误:前','令牌预期主要-EX pression
TEST.CPP:在成员函数'无效的TestClass< T> ::提取物(的std ::矢量<标准::字符串,性病::分配器<标准::字符串>>&安培;)与T = INT]':
TEST.CPP:26:从这里实例化
TEST.CPP:17:错误:没有上下文类型信息的重载函数的地址


解决方案

类型的获取℃的GT; 要超载是:

 常量标准::字符串&安培; (*)(常量的boost ::元组::利弊<的std ::字符串,提振::元组::利弊< INT,提振::元组:: null_type>>&安培;)

如果您的typedef ,为 get0_fn_t ,那么你可以声明一个指向该获得℃的> 与过载:

  get0_fn_t getter_fn =安培;升压::元组::获得℃的,标准::字符串,提振::元组::利弊< INT,提振::元组:: null_type>取代;

编辑::该计划是一个完整的例子:

 的#include<&算法GT;
#包括LT&; cstdlib>
#包括LT&;&iostream的GT;
#包括LT&;&迭代器GT;
#包括LT&;串GT;
#包括LT&;矢量>
#包括LT&;升压/ bind.hpp>
#包括LT&;升压/元组/ tuple.hpp>诠释的main()
{
    TYPEDEF提振::元组LT;的std ::字符串,整数> tuple_type;
    的std ::矢量<&tuple_type GT;元组;
    tuples.push_back(升压:: make_tuple(性病::字符串(TEST3),3));
    tuples.push_back(升压:: make_tuple(性病::字符串(TEST0),0));    的std ::矢量<标准::字符串>串;
    typedef的常量标准::字符串&安培; (* get0_fn_t)(常量的boost ::元组::利弊<的std ::字符串,提振::元组::利弊< INT,提振::元组:: null_type>>&安培;);
    get0_fn_t getter_fn =放大器;提高::元组::获得℃的,标准::字符串,提振::元组::利弊< INT,提振::元组:: null_type>取代;
    性病::变换(tuples.begin(),tuples.end()的std :: back_insert_iterator<的std ::矢量<标准::字符串>>(串),getter_fn);    的std ::矢量<标准::字符串> ::为const_iterator它,结束= strings.end();
    对于(IT = strings.begin(!);它=结束++吧)
        性病::法院LT&;< *它<<的std :: ENDL;    返回EXIT_SUCCESS;
}

EDIT2:这说明如何将它集成到的TestClass 模板:

 模板< typename的T>
类识别TestClass
{
私人的:
    TYPEDEF提振::元组LT;的std ::字符串,T> PairType;
    的std ::矢量<&PairType GT;存储;上市:
    无效提取物(的std ::矢量<标准::字符串>&放大器;结果)常量
    {
        result.clear();
        typedef的常量标准::字符串&安培; (* get0_fn_t)(常量的boost ::元组::利弊<的std ::字符串,提振::元组::利弊< T,提振::元组:: null_type>>&安培;);
        get0_fn_t getter_fn =放大器;提高::元组::获得℃的,标准::字符串,提振::元组::利弊< T,提振::元组:: null_type>取代;
        性病::变换(storage.begin(),storage.end(),result.begin(),getter_fn);
    }
};

Is it possible to do the following.

Say my boost tuple has <std::string, T>

I would like to use std::transform + mem_fun to insert only the std::string element in a corresponding vector. Is it possible or are we required to use a loop and push_back(get<0>)...

Ie the following doesn't like to compile... (unknown types...)

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

Here is an example (trying one of the comments):

#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

解决方案

The type of the get<0> overload that you want is:

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

If you typedef that to get0_fn_t, then you can declare a pointer to this get<0> overload with:

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

EDIT: This program is a complete working example:

#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: This shows how to integrate it into the TestClass template:

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);
    }
};

这篇关于升压元组+变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆