C++常用方法之Boost

安装:

在boost官网下载相应版本的库 http://www.boost.org/,再解压到/usr/local/目录下,就能使用了,如需要编译用于发布则可编译安装.so和.a库,具体如下:

http://www.boost.org/doc/libs/1_62_0/more/getting_started/windows.html

编译:

g++  test.cpp -o test -I/usr/local/boost_1_61_0

 

lexical_cast进行字符串、整数/浮点数之间的字面转换:

#include 
#include 
#include 
int main()
{
    int a = boost::lexical_cast("123456");
    std::cout << a << std::endl;
    double b = boost::lexical_cast("123.12");
    std::cout << b << std::endl;
    long double c = boost::lexical_cast("123.11");
    std::cout << c << std::endl;

    const double d = 123.12;
    std::string s = boost::lexical_cast(d);
    std::cout << s << std::endl;

    int i;
    try{
        i = boost::lexical_cast("abc");
    }
    catch(boost::bad_lexical_cast & e)
    {
        std::cout << e.what() << std::endl;
        return -1;
    }
    std::cout << i << std::endl;

    return 0;
}
123456
123.12
123.11
123.12
bad lexical cast: source type value could not be interpreted as target

progress进度条指示:

#include 
#include 
#include 
#include 
#include 
int main()
{
    std::vector v(100);
    std::ofstream fs("test");
    boost::progress_display pd(v.size());
    std::vector::iterator pos;
    for(pos=v.begin();pos!=v.end();++pos)
    {
        fs<<*pos<

date_time扩展来处理时区的问题:

#include 
#include 
int main()
{
        boost::gregorian::date d(2010, 1, 30);
        std::cout << d.year() << std::endl;
        std::cout << d.month() << std::endl;
        std::cout << d.day() << std::endl;
        std::cout << d.day_of_week() << std::endl;
        std::cout << d.end_of_month() << std::endl;
}
2010
Jan
30
Sat
2010-Jan-31

scoped_ptr等智能指针避免内存泄漏:

#include 
#include 
#include 
#include 
int main()
{
    boost::shared_ptr p1(new std::string("123"));
    std::cout << *p1 << '\n';
    boost::shared_ptr p2(p1);
    p1.reset(new std::string("234"));
    std::cout << *p1.get() << '\n';
    std::cout << *p2.get() << '\n';
    p1.reset();
    std::cout << std::boolalpha << static_cast(p2) << '\n';

    boost::scoped_ptr p3(new std::string("qwe"));
    std::cout << *p3 << std::endl;
    //boost::scoped_ptr p4(p3); // error , is privated
    //p3.reset(); //delete p3

    return 0;
}
123
234
123
true
qwe

format替代C里面的sprintf进行字符串化:

#include 
#include 
int main()
{
    std::cout << boost::format("writing %1%, x=%2% : %3%-th try") % "to" % 40.23 % 50 << std::endl;
    boost::format f("a=%1%, b=%2%, c=%3%, a=%1%");
    f % "string" % 2 % 10.0;
    std::cout << f.str() << std::endl;
}
writing to, x=40.23 : 50-th try
a=string, b=2, c=10, a=string

algorithm/string字符串算法库:

#include 
#include 
#include 
#include 
int main()
{
        std::string str("readme.txt");
        if(boost::ends_with(str,"txt"))
        {
                std::cout << boost::to_upper_copy(str) + "UPPER" << std::endl;
                assert(boost::ends_with(str, "txt"));
        }
        boost::replace_first(str, "readme", "followme");
        std::cout << str << std::endl;
        std::vector v(str.begin(),str.end());
        std::vector v2 = boost::to_upper_copy(boost::erase_first_copy(v, "txt"));
        for(int i=0; i

tokenizer用于分词的字符串处理库:

#include 
#include 
#include 
int main(void)
{
        std::string s("This is , a ,test!");
        boost::tokenizer<> tok(s);
        for(boost::tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg)
        {
                std::cout << *beg << " ";
        }
        return 0;
}
This is a test

xpressive正则表达式库,比原来的正则表达式库boost.regex要好的是不需要编译:

#include 
#include 
int main()
{
        boost::xpressive::cregex reg = boost::xpressive::cregex::compile("a.c");
        std::cout << boost::xpressive::regex_match("abc", reg) << std::endl;
        std::cout << boost::xpressive::regex_match("a+c", reg) << std::endl;
        std::cout << !boost::xpressive::regex_match("ac", reg) << std::endl;
        std::cout << !boost::xpressive::regex_match("abd", reg) << std::endl;
        return 0;
}
1
1
1
1

any一种通用的数据类型,可以将类型包装后统一放在容器中:

#include 
#include 
#include 
#include 
int main(void)
{
        typedef std::vector many;
        many a;
        a.push_back(2);
        a.push_back(std::string("test"));
        for(unsigned int i=0; i < a.size(); ++i)
        {
                std::cout << a[i].type().name() << std::endl;
                try
                {
                        int result = boost::any_cast(a[i]);
                        std::cout << result << std::endl;
                }
                catch(boost::bad_any_cast & ex)
                {
                        std::cout << "cast error:" << ex.what() << std::endl;
                }
        }
        return 0;
}
i
2
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
cast error:boost::bad_any_cast: failed conversion using boost::any_cast


你可能感兴趣的:(C/C++应用)