//
// json_parser.h
// mongoose
//
// Created by Alex on 5/26/14.
// Copyright (c) 2014 Cenbong. All rights reserved.
//
#ifndef __mongoose__json_parser__
#define __mongoose__json_parser__
#include
#include
#include
class sms_info
{
sms_info()
{
index_ = 0;
}
public:
static std::string INDEX;
static std::string TO;
static std::string FROM;
static std::string MSG;
static std::string SPLITTER;
static std::string TAG;
private:
int index_;
std::string to_;
std::string from_;
std::string msg_;
public:
sms_info(int index, const std::string& to, const std::string& from, const std::string& msg)
{
index_ = index;
to_ = to;
from_ = from;
msg_ = msg;
}
int index()
{
return index_;
}
std::string to()
{
return to_;
}
std::string from()
{
return from_;
}
std::string msg()
{
return msg_;
}
};
class json_parser
{
private:
static std::string ROOTNAME;
public:
static std::string generate(const std::vector& smss);
static bool parse(const std::string& s, std::vector& smss);
public:
static void tester();
};
#endif /* defined(__mongoose__json_parser__) */
//
// json_parser.cpp
// mongoose
//
// Created by Alex on 5/26/14.
// Copyright (c) 2014 Cenbong. All rights reserved.
//
#include "json_parser.h"
#include
#include "sstream"
#include
#include
#include
#include
#include
std::string sms_info::INDEX = "index";
std::string sms_info::TO = "to";
std::string sms_info::FROM = "from";
std::string sms_info::MSG = "msg";
std::string sms_info::SPLITTER = ",";
std::string sms_info::TAG = "SMSInfo";
std::string json_parser::ROOTNAME = "smss";
void json_parser::tester()
{
std::vector smss1;
for(int i = 0; i < 5; i++)
{
int index = i;
std::string to = "1860000" ;
std::string from = "1880000" ;
std::string msg = "这个短信发给Alex, 谢谢。 ";
smss1.push_back(sms_info(index, to, from, msg));
}
std::string s = generate(smss1);
std::vector smss2;
parse(s, smss2);
assert(smss1.size() == smss2.size());
}
std::string json_parser::generate(const std::vector& smss)
{
boost::property_tree::ptree pt_root;
boost::property_tree::ptree children;
boost::property_tree::ptree child;
for(size_t i = 0; i < smss.size(); i++)
{
sms_info sms = smss.at(i);
child.put(sms_info::INDEX, sms.index());
child.put(sms_info::TO, sms.to());
child.put(sms_info::FROM, sms.from());
child.put(sms_info::MSG, sms.msg());
children.push_back(std::make_pair("", child));
}
pt_root.add_child(ROOTNAME, children);
std::stringstream ss;
boost::property_tree::write_json(ss, pt_root);
std::string s = ss.str();
return s;
}
bool json_parser::parse(const std::string& s, std::vector& smss)
{
std::istringstream iss;
iss.str(s.c_str());
boost::property_tree::ptree parser;
boost::property_tree::json_parser::read_json(iss, parser);
boost::property_tree::ptree sms_array = parser.get_child(ROOTNAME);
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, sms_array)
{
boost::property_tree::ptree p = v.second;
int index = p.get(sms_info::INDEX);
std::string to = p.get(sms_info::TO);
std::string from = p.get(sms_info::FROM);
std::string msg = p.get(sms_info::MSG);
smss.push_back(sms_info(index, to, from, msg));
}
return true;
}
清单 1. 创建 Boost 元组并打印内容
#include
#include
#include
#include
using namespace boost;
int main ( )
{
tuple t(2, 'a', 0.9);
std::cout << t << std::endl;
return 0;
}
此代码的输出为 (2 a 0.9)。请注意,<< 运算符重载 std::ostream,以便通过转储每个单独的 tuple
元素来输出元组。清单 2. 用于元组的类必须具有正确的拷贝构造函数
#include
#include
#include
#include
using namespace std;
class X
{
int x;
X(const X& u) { x = u.x; }
public:
X(int y=5) : x(y) { }
};
int main ( )
{
boost::tuple t(3, X(2));
return 0;
}
清单 3.使用 make_tuple 来从函数返回元组
#include
#include
#include
#include
using namespace std;
boost::tuple
divide_and_modulo(int a, int b)
{
return boost::make_tuple (a/b, a%b);
}
int main ( )
{
boost::tuple t = divide_and_modulo(8, 2);
cout << t << endl;
return 0;
}
要访问元组的各个元素,您可以使用 get 例程。此例程具有两种变体,如清单 4 所示。请注意,还可
以使用 get 例程来设置元组的各个元素,虽然有些编译器可能不支持此功能。
清单 4. 使用 boost::get 例程
#include
#include
#include
#include
using namespace std;
boost::tuple
divide_and_modulo(int a, int b)
{
return boost::make_tuple (a/b, a%b);
}
int main ( )
{
boost::tuple t = divide_and_modulo(8, 2);
cout << t.get<0> () << endl; // prints 4
cout << boost::get<1>(t) << endl; // prints 0
boost::get<0>(t) = 9; // resets element 0 of t to 9
++boost::get<0>(t); // increments element 0 of t
cout << t.get<1>() << endl; // prints 10
return 0;
}
可以使用 const 限定符来声明元组,在这种情况下,用于访问特定元素的 get 调用将返回对 const 的
引用。不能对以这种方式访问的元素进行赋值(请参见清单 5)。
清单 5. 使用 const 限定符来声明的元组不可修改
#include
#include
#include
#include
using namespace std;
int main ( )
{
const boost::tuple t(8, "Hello World!");
t.get<1> ()[0] = "Y"; // error!
boost::get<0>(t) = 9; // error!
return 0;
}
可以使用关系运算符 ==、!=、<、>、<= 和 >= 对相同长度的元组进行比较。比较不同长度的元组会产
生编译时错误。这些运算符的工作原理是从左到右地比较两个参与元组的每个单独的元素(请参见清单清单 6. 关系运算符与元组
#include
#include
#include
#include
#include
using namespace std;
int main ( )
{
boost::tuple t(8, string ("Hello World!"));
boost::tuple t2(8, string("Hello World!"));
cout << (t == t2) << endl;
boost::tuple r(9, string ("Hello World!"));
boost::tuple r2(8, string("Hello World!"));
cout << (r > r2) << endl;
boost::tuple q(string ("AA"), string("BB"));
boost::tuple q2(string("AA"), string ("CC"));
cout << (q < q2) << endl;
return 0;
}
清单 6 的输出为 1 1 1。请注意,如果您不是使用 string 或 int,而是使用没有定义 ==、!= 等运算
符的用户定义的随机类,则会产生编译错误。清单 7. 使用 Boost 静态断言来验证变量的大小
#include
int main ( )
{
BOOST_STATIC_ASSERT(sizeof(int) == 4);
// … other code goes here
return 0;
}
要使用 BOOST_STATIC_ASSERT 宏,您必须包括 static_assert.hpp 头文件。不需要诸如 DNDEBUG 等特
定于编译器的选项,并且您不需要向链接器提供库——单凭该头文件就足够了。示例阐明了概念。
清单 8. 使用 Boost 静态断言来限制类实例化
#include
#include
using namespace std;
using namespace boost;
template
class A
{
private:
T x, y;
BOOST_STATIC_ASSERT(numeric_limits::is_signed);
public:
A(T x1, T y1) : x(x1), y(y1) { }
};
int main ( )
{
A a(2, 1);
return 0;
}
BOOST_STATIC_ASSERT时,它是私有的、受保护的还是公开的并不重要。
清单 9 将 BOOST_STATIC_ASSERT 与函数结合在一起使用。该代码确保在函数 f1 中处理的类型只能是
A 类型或其派生类型。通过使用 Boost 的静态断言宏和 is_convertible 例程(在boost/type_traits/is_convertible.hpp 中定义),此代码确保不希望的类型不会最终调用此例程。
清单 9. 将函数限制为仅处理特定的数据类型
#include
#include
#include
using namespace std;
using namespace boost;
struct A
{
int a;
float b;
};
struct B : public A
{
};
template
int f1 (T y)
{
BOOST_STATIC_ASSERT ((is_convertible::value));
return 0;
}
int main ( )
{
f1 (new B);
return 0;
}
清单 10. 用于在传统 UNIX 中生成随机数的代码
#include
#include
int main ( )
{
srand(time(NULL)); // this introduces randomness
for (int i=0; i<10; i++)
printf("%d\n", rand());
return 0;
}
uniform_real 分布。
清单 11. 将 variate_generator 与 mt19937 引擎和 uniform_int 分布一起使用
#include
#include
using namespace std;
using namespace boost;
int main ( )
{
uniform_int<> distribution (1, 100) ;
mt19937 engine ;
variate_generator > myrandom (engine, distribution);
for (int i=0; i<100; ++i)
cout << myrandom() << endl;
return 0;
}
此代码生成介于 1 和 100 之间(包括 1 和 100)的随机数;用于实现随机化的基础引擎是 mt19937。
清单 12:组合 uniform_real 分布和 kreutzer1986 引擎
#include
#include
using namespace std;
using namespace boost;
int main ( )
{
uniform_real<> distribution(1, 2) ;
kreutzer1986 engine ;
variate_generator > myrandom (engine, distribution);
for (int i=0; i<100; ++i)
cout << myrandom() << endl;
return 0;
}
清单 13. pool 和 object_pool 接口
#include
#include
#include
using namespace std;
using namespace boost;
class A
{
public: A( ) { cout << "Declaring An"; }
~A( ) { cout << "Deleting An"; }
};
int main ( )
{
cout << "Init pool...n";
pool<> p(10 * sizeof(A));
for (int i=0; i<10; ++i)
A* a = (A*) p.malloc(); // Always returns sizeof(A)
p.purge_memory();
cout << "Init object pool...n";
object_pool q;
for (int i=0; i<10; ++i)
A* a = q.construct(); // Calls A's constructor 10 times
return 0;
}
清单 14. singleton_pool 接口
#include
#include
using namespace std;
using namespace boost;
struct intpool { };
struct intpool2 { };
typedef boost::singleton_pool ipool1;
typedef boost::singleton_pool ipool2;
int main ( )
{
cout << "Init singleton pool...n";
for (int i=0; i<10; ++i) {
int* q1 = (int*) ipool1::malloc();
int* q2 = (int*) ipool2::malloc();
}
ipool1::purge_memory();
ipool2::purge_memory();
return 0;
}
清单 15. 使用 boost::program_options
#include
#include
#include
using namespace std;
int main (int ac, char* av[])
{
boost::program_options::options_description options("command line options");
options.add_options() ("help", "Use -h or --help to list all arguments")
("file", boost::program_options::value (),
"Provide input file name");
boost::program_options::variables_map vmap;
boost::program_options::store(
boost::program_options::parse_command_line(ac, av, options), vmap);
boost::program_options::notify(vmap);
if (vmap.count("help")) {
cout << options << endl;
}
return 0;
}
了这些功能。
#include
#include
#include
using namespace std;
int main (int ac, char* av[])
{
boost::program_options::options_description options("command line options");
options.add_options() ("help,h", "Use -h or --help to list all arguments")
("file", boost::program_options::value >( ),
"Provide input file name");
boost::program_options::variables_map vmap;
boost::program_options::store(
boost::program_options::parse_command_line(ac, av, options), vmap);
boost::program_options::notify(vmap);
if (vmap.count("help")) {
cout << options << endl;
}
if (vmap.count("file")) {
vector ifiles(vmap["file"].as< vector > ());
vector::iterator vI;
cout << "Number of input files: " << ifiles.size() << endl;
cout << "Input file list: " << endl;
for(vI = ifiles.begin(); vI != ifiles.end(); ++vI)
cout << "t" << *vI << endl;
} else {
cout << "No file specifiedn";
}
return 0;
}
清单 17. 将位置参数与命令行选项相关联
#include
#include
#include
using namespace std;
int main (int ac, char* av[])
{
boost::program_options::options_description options("command line options");
options.add_options() ("help,h", "Use -h or --help to list all arguments")
("file", boost::program_options::value(),
"Provide input file name")
("do-file", boost::program_options::value(),
"Specify commands file");
boost::program_options::variables_map vmap;
boost::program_options::positional_options_description poptd;
poptd.add ("file", 1);
poptd.add("do-file", 2);
boost::program_options::store(
boost::program_options::command_line_parser(ac, av).
options(options).positional(poptd).run(), vmap);
boost::program_options::notify(vmap);
if (vmap.count("file")) {
cout << "file: " << vmap["file"].as ( ) << endl;
}
if (vmap.count("do-file")) {
cout << "do- file: " << vmap["do-file"].as ( ) << endl;
}
return 0;
}
http://blog.csdn.net/huang_xw/article/details/8451442
void test_foreach()
{
using namespace boost::assign;
std::vector v;
v += 1, 2, 3, 4, 5, 6, 7, 8, 9;
BOOST_FOREACH(int x, v)
{
std::cout << x << ", ";
}
std::cout << std::endl;
std::string str("boost foreach");
BOOST_FOREACH(char& x, str)
{
std::cout << x << "-";
}
std::cout << std::endl;
}
void test_minmax()
{
struct Comp
{
bool operator()(const std::string &a, const std::string &b)
{
return (a < b) || (b.find("BOSS") != std::string::npos);
}
};
std::string s1("5000"), s2("123BOSS");
BOOST_AUTO(x, minmax(s1, s2));
std::cout << x.second << " " << x.first << std::endl;
BOOST_AUTO(y, minmax(s1, s2, Comp()));
std::cout << y.second << " " << y.first << std::endl;
}
void test_minmax_element()
{
std::vector v = boost::assign::list_of(633)(90)(67)(83)(2)(100);
BOOST_AUTO(x, boost::minmax_element(v.begin(), v.end()));
std::cout << "min: " << *x.first << std::endl;
std::cout << "max: " << *x.second << std::endl;
}