`
lunan
  • 浏览: 77143 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

boost::tuple

阅读更多
/*

1)Boost::tuple就为我们提供了一种类似于匿名struct的方法为我们解决函数的多个返回值的问题。
2)对boost::tuple你可以绑定10个参数,或者你可以迭代实现无限多参数的情况。
3)t.get<N>()或get<N>(t) ,取得第N个值
4)make_tuple ,生成tuple
5)tie , 生成都是ref的tuple
6) 重载比较运算符 ,可以直接用来比较
7)重载输入输出运算符 ,可以直接使用IO
8)get_head()和get_tail()函数,用来取得值
9)length<>和element<>用来得到tuple的size和第N个的值类型
10)如果使用boost::TR1,则还可以使用std::tr1::tuple_size(),std::tr1::tuple_element(),分别用来得到tuple的size和第N个值的类型。

*/

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

#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"

#include "boost/bind.hpp"

#include "boost/array.hpp"
#include "boost/assign/list_of.hpp"

//Tuples 与 for_each
template <typename Function>
void for_each_element( const boost::tuples::null_type&, Function)
{}
template <typename Tuple, typename Function>
void for_each_element(Tuple& t, Function func)
{
func(t.get_head()); for_each_element(t.get_tail(),func);
}

struct print
{
template <typename T> void operator()(const T& t)
   { std::cout << t << '\n'; }
};

template <typename T> struct print_type
{
void operator()(const T& t)
     { std::cout << t << '\n'; }
template <typename U> void operator()(const U& u) {}
};

//虚函数
class base
{
public:
virtual ~base() {};
virtual void test()
{ std::cout << "父类::test()\n"; }
};

class derived : public base
{
public:
virtual void test()
        { std::cout << "子类::test()\n"; }
};

//数组排序
template <int Index> class element_less
{
public:
template <typename Tuple> bool operator()(const Tuple& lhs,const Tuple& rhs) const
{
return boost::get<Index>(lhs) < boost::get<Index>(rhs);
}
};

//函数返回多个值
boost::tuple<int, int> divide_and_modulo(int a, int b)
  {
    return boost::make_tuple<int, int> (a/b, a%b);
  }

int main(int argc, _TCHAR* argv[])
{
    //虚函数
    boost::tuple<int,         std::string,derived> tup1(-5,"Tuples");
boost::tuple<unsigned int,std::string,base   > tup2;
tup2=tup1;
tup2.get<2>().test();
tup1.get<2>().test();

std::cout << "Interesting value: " << tup2.get<1>() << '\n';

   //定义一个tuple
   typedef boost::tuple<short,int,long> Mtuple;
   std::vector<Mtuple> vec ;
   vec= boost::assign::tuple_list_of(3,2,7)(6,2)(7,1)(5,9); //第一"tuple"必须满参数
   int ir = 1;
   vec.push_back(boost::make_tuple(1,boost::ref(ir),1));

   //降序排列
   std::sort(vec.begin(),vec.end(),element_less<0>());
   std::cout << "After sorting: " << vec[0].get<0>() << '\n' << vec[1].get<0>() << '\n' << vec[2].get<0>() << '\n';
  
   //升序排列
   std::sort(vec.begin(),vec.end(),boost::bind(std::greater<short>(),
                                           boost::bind(&Mtuple::get<0>,_1),
   boost::bind(&Mtuple::get<0>,_2)) );
  
   std::cout << "After sorting: " << vec[0].get<0>() << '\n' << vec[1].get<0>() << '\n' << vec[2].get<0>() << '\n';

    //绑定tuple
short  s;
int    d;
double b;
boost::tie(s,d,b)=vec[0];
   
//for_each
typedef boost::tuple<short,int,long> my_tuple;
boost::tuple<int,short,double> nums(111,222,3.01);
for_each_element(nums, print());
for_each_element(nums, print_type<int>());

//使用 make_tuple 来从函数返回元组
boost::tuple<int, int> t = divide_and_modulo(8, 3);
std::cout << t << '\n';

return 0;
}
分享到:
评论
1 楼 lunan 2011-11-29  
如果在头文件中我引用如下:

#include "boost/lambda/lambda.hpp"
#include "boost/lambda/bind.hpp"
using namespace boost::lambda;

那么 bind(&Mtuple::get<0>,_1),  会有问题

有人说是:因为get有const和非const   2个重载版本。得先static_cast指针转换以获取正确的版本。
例子如下:
void   foo(int);
void   foo(float);
int   i;  
    ...
bind(&foo,   _1)(i);                                                         //   error  
    ...
void   (*pf1)(int)   =   &foo;
bind(pf1,   _1)(i);                                                           //   ok
bind(static_cast <void(*)(int)> (&foo),   _1)(i);   //   ok

没看明白。这个问题未完待续。
解决办法是加上命名空间,写出的效果如下:

#include "stdafx.h"

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>

#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"

#include "boost/array.hpp"
#include "boost/assign/list_of.hpp"

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
using namespace boost;

#include <boost/bind.hpp>

class Person
{
public:
Person(){}
Person(const std::string& name) : name_(name){}

std::string& Name()
{ return name_; }

private:
std::string name_;

};


int main()
{
std::map<int, Person> persons;
persons[123] = Person("Amy");
persons[234] = Person("Ralph");
persons[345] = Person("Simon");
persons[456] = Person("Maggie");
   
//非lamad::bind 编译有问题?
std::cout << "Ralph's Id is: " <<
              std::find_if( persons.begin(), persons.end(), 
  ::lambda::bind(&Person::Name,::lambda::bind(&std::map<int, Person>::value_type::second, ::lambda::_1)) == "Ralph" )->first;


typedef boost::tuple<short,int,long> Mtuple;
std::vector<Mtuple> vec ;
vec= boost::assign::tuple_list_of(3,2,7)(6,2)(7,1)(5,9);


std::cout << "bind tuple: " <<
std::find_if( vec.begin(), vec.end(),
bind(&Mtuple::get<0>,_1) == 7 )->get<1>();
return 0;
}


相关推荐

    类似Boost中Tuple的实现

    类似Boost中Tuple的实现 自己编写的 请支持原创

    pfr:std :: tuple类似用户定义类型的方法,没有任何宏或样板代码

    增强PFR 这是一个C ++ 14库,用于非常基本的反射,使您可以按索引访问结构元素,并为用户定义的类型提供其他std::tuple类的方法,而无需任何宏或样板代码。检测结果分行建立测试覆盖率更多信息开发: 主: 激励实例...

    rebind:C++ 模板操作库

    在重新绑定中,您可以将类型存储在您想要的任何类中,例如std::tuple或空结构: template struct Container { }; Rebind本身是从一个实例中获取模板参数并将它们应用于不同的模板 - 例如: # include " rebind...

    THE BOOST C++ LIBRARIES

    THE BOOST C++ LIBRARIES是一份...14.2 Boost.Tuple 14.3 Boost.Any 14.4 Boost.Variant 14.5 Exercises Chapter 15: Error Handling 15.1 General 15.2 Boost.System 15.3 Boost.Exception Chapter 16: Cast Operators

    boost 1.41 中文文档,使用帮助,教程手册

    汉化 boost 文档,致力 boost 推广。 如果你对本项目有兴趣,欢迎加入,相关说明请查阅项目论坛: https://groups.google.com/ 到目前为止,各人贡献的译文如下: 贡献者 贡献的译文 alai04 accumulators, any, ...

    hana:您的元编程标准库

    助推汉娜 您的元编程标准库总览# include &lt; boost&gt;# include &lt; cassert&gt;# include &lt; string&gt;namespace hana = boost::hana;using namespace hana ::literals ;struct Fish { std::string name; };struct Cat { std::...

    libfptu:最快的类似于JSON数据的二进制表示形式之一

    我们可以说libfptu提供了JSON的灵活性,即具有boost :: optional和boost :: variant功能的本地C结构的速度。 当前版本的libfptu支持绑定到本机C结构的数据架构和预放置字段(也称为预放置字段),以及在共享内存中...

    cxl:一些C ++实用程序

    从Simimir到Boost.Variant的variant ,具有完整的move和右值引用支持,减少了黑客攻击,即避免使用boost::detail::variant::void_来满足所有模板参数。 通过使用宏定义一些元数据来最小化反射支持: CXL_BEGIN_...

    tinympl:C++11 模板元编程库

    与 Boost.MPL 不同的是,这个包利用了 C++11 特性,从而产生了更小的代码库。 主要特点: 广泛的算法库,适用于可变参数模板和(可能是用户定义的)模板类型(如std::tuple )。 编译时容器,例如map 、 vector和...

    multi_index:最小提升

    multi_index BoostLess MultiIndex,用于C ++ 11编译器。 目录 介绍 Boost是一个很棒的库,尽管它旨在通过尽可能多的... 删除Boost.MPL以使用和std :: tuple。 文献资料 除Boost名称空间外,该文档应与相同。 平台

    Reflection_C_plus_plus:C ++中的反思

    介绍使用C ++模板元编程技术(Boost.MPL) 在编译期间生成的元数据存储在只读数据区域中需要boost库和GCC 4.7或更高版本样本# include &lt; cstdlib&gt;# include &lt; cstring&gt;# include &lt; iostream&gt;# include &lt; tuple&gt;# ...

    Class_to_json.zip

    C++通用性的Class转化为json。使用boost,tuple完成反射,使用jsoncpp,cmake。

    lexyaccmingw

    * unique元函数 * filter元函数 * map元函数 * reduce元函数 * 自动生成C++类 * scatter类 * tuple结构 * 分析C++类层次 * 分析C++类结构 * 类是否拥有指定参数的成员函数 ...

Global site tag (gtag.js) - Google Analytics