std tuple,c 重载运算符

1 , c 重载运算符ostream& operator << (ostream& ,自定义的类 &)istream& operator >> (ostream& ,自定义的类 &)这样实现你自己定义的输入输出流例子:(不是我写的,是以前看谭浩强书上的)#include <iostream>using namespace std;class ComplexComplex()Complex(double r,double i)Complex operator + (Complex &c2);friend ostream& operator << (ostream&,Complex&);private:double real;double imag; };Complex Complex::operator + (Complex &c2)ostream& operator << (ostream& output,Complex& c) return output;}intmain() c3=c1+c2; cout<<c3; return 0;}应该是看的明白吧【std tuple,c 重载运算符】
2,求大神帮帮忙写一段c程序非常感谢#include #include class { public: typedef std::array NameType; typedef std::array NumberType; typedef std::array SexType; typedef std::array IdType; class Birthday; friend class Birthday; typedef Birthday BirthdayType; typedef People ThisType; typedef std::tuple DataType; enum { NameIndex,NumberIndex,SexIndex,IdIndex,BirthdayIndex } People(){} People(const ThisType & aPeople){} People(ThisType && aPeople){} ~People(){} private: DataType data_; }#include#include using namespace std; int main() { int r[101][101]; int a=0,b=0,c=0,n=0; cout<<"请输入关系矩阵的阶数"<>n; for (a=1;a<=n;a++) { cout<<"请输入矩阵的第"<<<"行,中间一空格隔开."<>r[a][b]; } cout<<<"原矩阵为"<
3 , boosttr1与stdtr1冲突了怎么办最近用boost::math库,发现会和我以前的程序冲突 。比如我的程序如果用到#include等等std::tr1名字域下的东西 。。。google了下,原因是boost::tr1设计初衷是为了在你的系统没有std::tr1标准库实现的情况下让你仍然可以在程序中使用std::tr1::unordered_map, std::tr1::tuple等东东,当然了这是一个work around,也就是你其实还是用的boost::tuple等等 只是你的代码写的时候可以写std::tr1::tuple 。比如boost的math库的一个分布函数的实现 /usr/include/boost/math/special_functions/detail/igamma_inverse.hpp 在该文件第13行 #include 然后下面它就用到了tuple使用如下 std::tr1::tuple operator() (const T& x) const //其实呢还是用的boost::下面的tuple看下/usr/include/boost/tr1/tuple.hpp namespace std{ namespace tr1{ using ::boost::fusion::tuple; // [6.1.3.2] Tuple creation functions using ::boost::fusion::ignore; using ::boost::fusion::make_tuple; using ::boost::fusion::tie; using ::boost::fusion::get; // [6.1.3.3] Tuple helper classes using ::boost::fusion::tuple_size; using ::boost::fusion::tuple_element; }} 这就是work around的方法 。现在问题来了 , 我的GCC已经有std::tr1的实现了,比如 ,那么着就会带来命名冲突了,比如同时又两个tuple定义了 。。。。boost::fusion::tuple和原生的std::tr1::tuple 。见boost::tr1的文档 实现 如果 Boost.TR1 被配置 为使用你的标准库中的原生 TR1 实现,则它不需要做多少事情:它只是包含适当的头文件就行了 。如果 Boost.TR1 使用了某个组件的 Boost 实现 , 则它需要包含适当的 Boost 头文件并使用声明将所需的名字导入到 namespace std::tr1 中 。注意,只有作为标准部分的声明会被导入:本实现有意非常严格地没有将所有 Boost-特有的扩展引入到 namespace std::tr1,这是为了能够捕获用户代码中的任何可移植性错误 。如果你真的需要使用 Boost-特有的扩展,则你应当直接包含 Boost 头文件,则改用 namespace boost:: 中的声明 。注意,本实现的风格并不是完全符合标准的,它不能将用户自定义的 TR1 组件的模板特化增加到 namespace std::tr1 中 。还有一到两个 Boost 库尚未完全符合标准,任何与标准不符的地方都已在 "TR1 的分类"一节 中说明 。不过幸好,这些不符合标准的行为在实际中极少会用到 。如果你使用标准的头文件包含(在 boost/tr1/tr1 中),则这些头文件名有时可能与现有的标准库头文件冲突(例如 shared_ptr 已增加到现有的标准库头文件 中而不是它自己的头文件) 。这些头文件可以用以下两种方法之一前转到现有的标准库头文件:对于 gcc,使用 #include_next, 而对于其它编译器则使用宏BOOST_TR1_STD_HEADER(header) (在 boost/tr1/detail/config.hpp 中定义),它将扩展为 #include <../include/header>. 对于大多数编译器,这样就可以直接使用,但是这意味着这些头文件不能被放在名为"include"且已在你的编译器搜索路径中的目录下 。怎么解决冲突问题呢,还是没有完全弄明白,不过解决办法还是google到了 。。OK 解决了就好 。。以后再说 。按理说按照boost/tr1/tuple.hpp 中所写的 #ifdef BOOST_HAS_TR1_TUPLE # ifdef BOOST_HAS_INCLUDE_NEXT # include_next BOOST_TR1_HEADER(tuple) # else # include # include BOOST_TR1_STD_HEADER(BOOST_TR1_PATH(tuple)) # endif 我应该加一个 #define BOOST_HAS_tR1_TUPLE 就能解决问题 因为 BOOST_TR1_HEADER(tuple) 会被转换为 #include 但是实验不成功 。。。。usr/include/boost/tr1/tuple.hpp:13:43: error: no include path in which to search for tr1/tuple //WHY 知道为什么的帮忙告诉我下,谢谢~ //估计可能是incude_next的原因? 没太仔细看include_next作用TODO 最后google到的解决方案是#define BOOST_HAS_TR1_TUPLE 1 #include #undef BOOST_HAS_INCLUDE_NEXT //似乎是需要屏蔽掉 incude_next
4,boost 在c中是什么用Boost库是一个可移植、提供源代码的C++库 , 作为标准库的后备,是C++标准化进程的发动机之一 。Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容 。在C++社区中影响甚大 , 是不折不扣的“准”标准库 。Boost由于其对跨平台的强调,对标准C++的强调,与编写平台无关 。大部分boost库功能的使用只需包括相应头文件即可,少数(如正则表达式库,文件系统库等)需要链接库 。但Boost中也有很多是实验性质的东西 , 在实际的开发中实用需要谨慎 。这里的boost是一个命名空间,相当于平时使用的std , Boost库的函数都放在这个命名空间里面,具体你可以上boost.org看看,这个是boost的官方网站,里面提供boost库的免费下载 。一、Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一 。Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容 。在C++社区中影响甚大 , 是不折不扣的“准”标准库 。Boost由于其对跨平台的强调,对标准C++的强调 , 与编写平台无关 。大部分boost库功能的使用只需包括相应头文件即可,少数(如正则表达式库,文件系统库等)需要链接库 。但Boost中也有很多是实验性质的东西,在实际的开发中实用需要谨慎 。二、按照实现的功能,Boost可为大致归入以下20个分类,在下面的分类中,有些库同时归入几种类别 。1. 字符串和文本处理a) Conversionb) Formatc) IOStreamd) Lexical Caste) Regexf) Spiritg) String Algoh) Tokenizeri) Wavej) Xpressive2. 容器a) Arrayb) Bimapc) Circular Bufferd) Disjoint Setse) Dynamic Bitsetf) GILg) Graphh) ICLi) Intrusivej) Multi-Arrayk) Multi-Indexl) Pointer Containerm) Property Mapn) Property Treeo) Unorderedp) Variant3. 迭代器a) GILb) Graphc) Iteratorsd) Operatorse) Tokenizer4. 算法a) Foreachb) GILc) Graphd) Min-Maxe) Rangef) String Algog) Utility5. 函数对象和高阶编程a) Bindb) Functionc) Functionald) Functional/Factorye) Functional/Forwardf) Functional/Hashg) Lambdah) Member Functioni) Refj) Result Ofk) Signalsl) Signals2m) Utility6. 泛型编程a) Call Traitsb) Concept Checkc) Enable Ifd) Function Typese) GILf) In Place Factory, Typed In Place Factoryg) Operatorsh) Property Mapi) Static Assertj) Type Traits7. 模板元编程a) Function Typesb) Fusionc) MPLd) Protoe) Static Assertf) Type Traits8. 预处理元编程a) Preprocessors9. 并发编程a) Asiob) Interprocessc) MPId) Thread10. 数学和数字a) Accumulatorsb) Integerc) Intervald) Mathe) Math Common Factorf) Math Octoniong) Math Quaternionh) Math/Special Functionsi) Math/Statistical Distributionsj) Multi-Arrayk) Numeric Conversionl) Operatorsm) Randomn) Rationalo) uBLAS11. 排错和测试a) Concept Checkb) Static Assertc) Test12. 数据结构a) Anyb) Bitmapc) Compressed Paird) Fusione) ICLf) Multi-Indexg) Pointer Containerh) Property Treei) Tuplej) Uuidk) Variant13. 图像处理a) GIL14. 输入输出a) Asiob) Assignc) Formatd) IO State Saverse) IOStreamsf) Program Optionsg) Serialization15. 跨语言混合编程a) Python16. 内存管理a) Poolb) Smart Ptrc) Utility17. 解析a) Spirit18. 编程接口a) Functionb) Parameter19. 杂项a) Compressed Pairb) Conversionc) CRCd) Date Timee) Exceptionf) Filesystemg) Flyweighth) Lexical Casti) Meta State Machinej) Numeric Conversionk) Optionall) Polygonm) Program Optionsn) Scope Exito) Statechartp) Swapq) Systemr) Timers) Triboolt) Typeofu) Unitsv) Utilityw) Value Initialized20. 编译器问题的变通方案a) Compatibilityb) Config这是C++的准标准库也是目前比较活跃的C++库.即提供了丰富的C++扩展,也对C++的发展提供了源源不断的创意5,C如何把一个字符串识别为函数#include #include #include int use_foo1_1(int n) { return n; } int use_foo1_2(int n) { return n - 1; } int use_foo2_1(int n, int m) { return n + m; } int use_foo2_2(int n, int m) { return n * m; } void test1(void) { typedef int (*foo_type)(int); //函数类型由函数返回值类型和参数列表类型决定 typedef std::map foo_map_type; foo_map_type map; map["aaa"] = use_foo1_1; map["bbb"] = use_foo1_2; std::cout << (*map["aaa"])(10) << std::endl; std::cout << (*map["bbb"])(10) << std::endl; } //由于 map的value部分只能接受一种类型 // use_foo1_1 和 use_foo2_1 不是同一种类型的函数 //所以 保存函数的指针 使用 void* 这样就能存储了, // 但是通过函数指针调用函数的时候需要把 void* 转换成相对应的指针类型才行 //所以 一个map结点需要保存2个内容 函数指针和 转换函数的类型说明符号 void test2(void) { typedef int (*foo1_type)(int); //函数类型由函数返回值类型和参数列表类型决定 typedef int (*foo2_type)(int, int); typedef std::map > foo_map_type; foo_map_type map; map["aaa"] = std::pair(use_foo1_1, 1); map["bbb"] = std::pair(use_foo2_1, 2); if(map["aaa"].second == 1) { std::cout << (*reinterpret_cast(map["aaa"].first))(10) << std::endl; } if(map["aaa"].second == 2) { std::cout << (*reinterpret_cast(map["aaa"].first))(10, 20) << std::endl; } if(map["bbb"].second == 1) { std::cout << (*reinterpret_cast(map["bbb"].first))(10) << std::endl; } if(map["bbb"].second == 2) { std::cout << (*reinterpret_cast(map["bbb"].first))(10, 20) << std::endl; } } //然后一个更通用的模式 // 要通用 就会碰到 3个问题,boost里有现成的解决方案 当然你也可以自己想办法 // 1 函数指针类型 // 用boost::bind 存储 // 2 函数参数表类型 // 统一成boost::tuple 传递时用boost::any // 3 函数返回值类型 // 统一成参数返回 #include #include #include #include #include #include class foo_container { private: class base_foo_handler // 为了能够统一存储 使用 接口模式 做存储规范 { public: virtual void call(const boost::any& params) const = 0; }; template class foo_handler : public base_foo_handler //带返回值的函数包装 { public: typedef Handler_Foo foo_type; typedef Handler_Params_Parse_And_Call param_parse_and_call_type; typedef Handler_Ret_Back ret_back_type; foo_handler(const foo_type& foo, const param_parse_and_call_type& params_parse, const ret_back_type& ret_back) : _foo(foo), _params_parse_and_call(params_parse), _ret_back(ret_back) { } virtual void call(const boost::any& params) const { _params_parse_and_call(_foo, params, _ret_back); } private: foo_type _foo; param_parse_and_call_type _params_parse_and_call; ret_back_type _ret_back; }; template class foo_handler : public base_foo_handler //不带返回值的函数包装 { public: typedef Handler_Foo foo_type; typedef Handler_Params_Parse_And_Call param_parse_and_call_type; typedef void ret_back_type; foo_handler(const foo_type& foo, const param_parse_and_call_type& params_parse) : _foo(foo), _params_parse_and_call(params_parse) { } virtual void call(const boost::any& params) const { _params_parse_and_call(_foo, params); } private: foo_type _foo; param_parse_and_call_type _params_parse_and_call; }; template struct params_foo_switch //函数执行的规范函数 { template static void s_params_parse_and_call(const Handler_Foo& foo, const boost::any& params) // 不带返回值 //参数有多少个就补充多少组 { BOOST_MPL_ASSERT((boost::mpl::bool_)); } template static void s_params_parse_and_call(const Handler_Foo& foo, const boost::any& params, const Handler_Ret_Back& ret) //带返回值 //参数有多少个就补充多少组 { BOOST_MPL_ASSERT((boost::mpl::bool_)); // 这只是个函数的说明 实际用的是特例函数 } }; template<> struct params_foo_switch<1> //函数执行的规范函数(1参数) { template static void s_params_parse_and_call(const Handler_Foo& foo, const boost::any& params) { typedef Tuple tuple_type; const tuple_type& tuple = boost::any_cast(params); foo(boost::get<0>(tuple)); } template static void s_params_parse_and_call(const Handler_Foo& foo, const boost::any& params, const Handler_Ret_Back& ret) { typedef Tuple tuple_type; const tuple_type& tuple = boost::any_cast(params); ret(foo(boost::get<0>(tuple))); } }; template<> struct params_foo_switch<2> //函数执行的规范函数(2参数) { template static void s_params_parse_and_call(const Handler_Foo& foo, const boost::any& params) { typedef Tuple tuple_type; const tuple_type& tuple = boost::any_cast(params); foo(boost::get<0>(tuple), boost::get<1>(tuple)); } template static void s_params_parse_and_call(const Handler_Foo& foo, const boost::any& params, const Handler_Ret_Back& ret) { typedef Tuple tuple_type; const tuple_type& tuple = boost::any_cast(params); ret(foo(boost::get<0>(tuple), boost::get<1>(tuple))); } }; template<> struct params_foo_switch<3> //函数执行的规范函数(3参数) { template static void s_params_parse_and_call(const Handler_Foo& foo, const boost::any& params) // 不带返回值 { typedef Tuple tuple_type; const tuple_type& tuple = boost::any_cast(params); foo(boost::get<0>(tuple), boost::get<1>(tuple), boost::get<2>(tuple)); } template static void s_params_parse_and_call(const Handler_Foo& foo, const boost::any& params, const Handler_Ret_Back& ret) { typedef Tuple tuple_type; const tuple_type& tuple = boost::any_cast(params); ret(foo(boost::get<0>(tuple), boost::get<1>(tuple), boost::get<2>(tuple))); } }; //如要处理4参以上的函数 请自己补充, 一般20参 足够了copy pase的体力活 typedef boost::shared_ptr foo_handler_ptr_type; typedef std::map map_type; typedef map_type::const_iterator map_citer_type; private: typedef foo_container this_type; public: foo_container(void){} ~foo_container(void){} template bool register_foo(const std::string& name, const Handler_Foo& foo) { typedef Handler_Foo foo_type; typedef Tuple tuple_type; return prv_register_foo(name, foo, boost::bind(&params_foo_switch< boost::tuples::length::value>::s_params_parse_and_call , _1, _2)); } template bool register_foo(const std::string& name, const Handler_Foo& foo, const Handler_Ret_Back& ret) { typedef Handler_Foo foo_type; typedef Handler_Ret_Back ret_back_type; typedef Tuple tuple_type; return prv_register_foo(name, foo, ret, boost::bind(&params_foo_switch< boost::tuples::length::value> ::s_params_parse_and_call, _1, _2, _3)); } template bool call(const std::string& name, const Tuple& params) const { map_citer_type iter = _map.find(name); if(iter == _map.end()) { return false; } iter->second->call(boost::any(params)); return true; } private: template bool prv_register_foo(const std::string& name, const Handler_Foo& foo, const Handler_Params_Parse_And_Call params) { typedef foo_handler now_foo_handler_type; foo_handler_ptr_type ptr(new now_foo_handler_type(foo, params)); return _map.insert(map_type::value_type(name, ptr)).second; } template bool prv_register_foo(const std::string& name, const Handler_Foo& foo, const Handler_Ret_Back& ret, const Handler_Params_Parse_And_Call& params) { typedef foo_handler now_foo_handler_type; foo_handler_ptr_type ptr(new now_foo_handler_type(foo, params, ret)); return _map.insert(map_type::value_type(name, ptr)).second; } private: map_type _map; }; template void back_ret(const T& val) { std::cout << val << std::endl; } void test3(void) { foo_container cont; cont.register_foo< boost::tuple >("aaa", boost::bind(&use_foo1_1, _1), boost::bind(&(back_ret), _1)); cont.register_foo >("bbb", boost::bind(&use_foo2_1, _1, _2), boost::bind(&back_ret, _1)); cont.call("aaa", boost::make_tuple(10)); cont.call("bbb", boost::make_tuple(10, 20)); } int main(int argc, char* argv[]) { test1(); test2(); test3(); char cc = 0; std::cin >> cc; return 0; } vc2010下编译运行通过 功能扩充 弄清楚后你自己添加 boost版本1.53.... 这个你自己写接口吧,if 或者case switch你的意思是直接写一个字符串 , 然后变成一个函数吗?你可以使用创建模式 , 通过工厂加工,又或者使用其他的方法 , 我觉得你表达不是很清晰

    推荐阅读