boost::tuple 博客分类: boost boosttuple
程序员文章站
2024-03-06 14:33:32
...
/*
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)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;
}
推荐阅读
-
boost学习笔记之Pointer Container库 博客分类: boost C++boost编程
-
boost::tuple 博客分类: boost boosttuple
-
boost::BOOST_FOREACH 博客分类: boost boost
-
C++之Boost使用 博客分类: Linux和开源软件 C++BoostLinux
-
Boost::bind 博客分类: boost bindboost
-
Linux编译安装boost1.48 博客分类: Linux安装笔记 linuxboost
-
bloom filter server 博客分类: boost学习 boostbloom filter
-
C++ boost asio 学习(一) 博客分类: C++ c++boostasio
-
Boost 使用 博客分类: C/C++ vs10Boostc++标准库asio
-
boost的简单编译过程: 博客分类: C/C++ boost