《Boost C++ Application Development Cookbook》笔记(持续更新)

小试牛刀
  • 可用boost::optianal来作为函数返回值的标识,函数返回值用来表示函数是否执行成功,但如果执行成功但没有正确的结果,这种情况则可以借助boost::optianal来实现

  • boost::array 函数返回数组可以用array,不用担心数据越界等问题。

转换
  • 转换字符串到数字
//boost库(效率最高)
int i = boost::lexical_cast<int>("100");
//c++
std::istringstream iss("100");
int j;
iss >> j;
  • 数字转字符串
//boost库
std::string s = boost::lexical_cast<std::string>(100);
//c++
std::stringstream ss;
ss << 100;
ss >> s;
  • 为避免int转到uint发生数值超出有限范围的错误,boost提供了类型转换
try
{
int j = -100;
boost::numeric_cast<unsigned short>(j);
}
catch(const boost::numeric::positive_overflow& e)
{
...
}
catch(const boost::numeric::negative_overflow &e)
{
...
}
  • 用户定义的类型与字符串的相互转换,必须要重载<< 和 >>两个运算符
#include<iostream>
#include<boost/lexical_cast.hpp>
using namespace std;
class negative_number
{
  unsigned short number_;
public:
  negative_number(unsigned short number) : number_(number)
  {

  }
  negative_number(){}
  unsigned short value_without_sign() const
  {
    return number_;
  }
};

//重载<<
std::ostream& operator <<(std::ostream& os, const negative_number& num)
{
  os << '-' << num.value_without_sign();
  return os;
}
//重载>>
std::istream& operator >>(std::istream& is, negative_number& num)
{
  unsigned short s;
  is >> s;
  num = negative_number(s);
  return is;
}

int main(int argc, char *argv[])
{
  negative_number n = boost::lexical_cast<negative_number>("100");
  cout<<n.value_without_sign()<<endl;
  int i = boost::lexical_cast<int>(n);
  return 0;
}
资源管理
  • 保证指针所指资源在域中被销毁.当new一个指针后,若程序在异常情况下在delete这个指针前就退出程序了,那么将造成内存泄露,此时可以使用boost::scoped_ptr.其等同于const std::auto_ptr.
#include <boost/scoped_ptr.hpp>
bool foo()
{
    boost::scoped_ptr<foo_class> p(new foo_class("fc"));
    bool something_else_happened = func(p.get());
    if (something_else_happened)
    {
        return false;
    }
    ...
    return true;
}
Boost libraries are developed by professionals, tested on multiple platforms and processor architectures, and contain reliable solutions for a wide range of tasks. This Cookbook takes you on a journey of simplifying the process of application development and guides you through writing perfect applications fast. “Boost C++ Application Development Cookbook” provides you with a number of clear step-by-step recipes that will help you take advantage of the real power of Boost and C++, while giving you a good grounding in using it in any project. “Boost C++ Application Development Cookbook” looks at the Boost libraries, and breaks down the mystery and confusion about which library to use in which situation. It will take you through a number of clear, practical recipes that will help you to take advantage of the readily available solutions. Boost C++ Application Development Cookbook starts with teaching the basics of Boost libraries that are now mostly part of C++11 and leave no chance for memory leaks. Managing resources will become a piece of cake. We’ll see what kind of work can be done at compile time and what Boost containers can do. Do you think multithreading is a burden? Not with Boost. Think writing portable and fast servers is impossible? You’ll be surprised! Compilers and operating systems differ too much? Not with Boost. From manipulating images to graphs, directories, timers, files, strings – everyone will find an interesting topic. You will learn everything for the development of high quality fast and portable applications. Write a program once and then you can use it on Linux, Windows, MacOS, Android operating systems. What you will learn from this book Get familiar with new data types for everyday use Use pointers to manage resources Get to grips with compile-time computations and assertions Use Boost libraries for multithreading Learn about Parallel execution of different task Perform common string-related tasks using Boost li
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值