c++ boost库学习三:实用工具
noncopyable
大家都知道定义一个空类的时候,它实际包含了构造函数,拷贝构造函数,赋值操作符和析构函数等。
这样就很容易产生一个问题,就是当用户调用A a(“^_^") 或者A c="^_^" 时会发生一些意想不到的行为,所以很多时候我们需要禁用这样的用法。
一种方法就是把拷贝构造函数和赋值操作符显式的定义为private,但是这样需要很多代码。
于是boost库为大家提供了一个简单的方法:只需要将类继承于noncopyable就可以了。
#include "iostream"
#include <string>
#include <boost\noncopyable.hpp>
using namespace std; class A:public boost::noncopyable
{
//public:
// A(){};
// A(const A&a){cout<<"call copy constructor"<<endl;}
}; int _tmain(int argc, _TCHAR* argv[])
{
A a;
A b(a); //error
A c;
c=a; //error
return ;
}
经过测试,如果显式的把拷贝构造函数和赋值操作符定义为public的话,noncopyable就失效了。
assign
用过vector等容器的都知道向vector里添加元素可以用insert或push_back方法,但这些方法都太复杂了。
boost库为大家提供了一个简单的方法assign,看过它的用法后你就知道它真的很方便
#include "iostream"
#include <string>
#include <map>
#include <list>
#include <boost\assign.hpp>
using namespace std;
using namespace boost::assign; int _tmain(int argc, _TCHAR* argv[])
{
// += 操作符
vector<string> v;
v+= "aa","bb","cc";
map<int,string> m;
m+=make_pair(,"java"),make_pair(,"python"); // assign的push_back(), push_front(), insert()
vector<int> v2;
push_back(v2) ()()();
list<string> l;
push_front(l) ("aa")("bb")("cc");
map<int,string> m2; //push_back 和 push_front只能用在拥有同名方法的容器里
insert(m2) (,"java")(,"c++");
return ;
}
uuid
uuid就是机器的唯一识别码,一共有128位(16字节)。用uuid可以生成全球唯一的标识,这个东西在写程序的时候会经常用到,比如一款软件的序列号只能用在1台机器上时它就是靠识别uuid来判断的。
可以把uuid看作一个16字节长元素类型为unsign char的容器。
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <assert.h>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
using namespace boost::uuids;
using namespace std;
int main()
{
uuid u;
assert(u.static_size()==);
assert(u.size()==);
vector<unsigned char> v(,);
copy(v.begin(),v.end(),u.begin());
cout<<u<<endl; // uuid生成算法
uuid u2 = random_generator()(); //随机生成一个不重复的uuid
assert(!u2.is_nil()); //判断是否无效uuid
assert(u!=u2);
cout<<u2<<endl; // uuid与字符串的转换
uuid u3 = string_generator()("01234567-89ab-cdef-0123-456789abcdef");
stringstream ss;
ss<<u3;
string s;
ss>>s;
cout<<s<<endl; return ;
}
在vs中编译上面的代码时候可能会报warning C4996错,这时候有两种方法可以解决此问题
1. 需要向文件最开头(我有stdafx.h所以在里面加)里加上#define _SCL_SECURE_NO_WARNINGS,可参考
http://choorucode.com/2010/08/30/visual-c-c4996-warning-on-copy-with-array-parameters/,
2. 在所有#include之前(不包括stdafx.h)加上#include <boost\config\warning_disable.hpp>,其作用是避免VC编译器在使用已经deprecated函数的时候会出现c4996 error从而无法编译通过
比如下面的例子:
#include <boost\config\warning_disable.hpp>
#include <iostream>
using namespace std; int main()
{
char s[]="teststr";
char *s2=new char[];
strcpy(s2,s);
cout<<s2<<endl;
delete s2; return ;
}
c++ boost库学习三:实用工具的更多相关文章
- boost库学习之开篇
本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...
- Boost库学习之旅入门篇
学习及使用Boost库已经有一段时间了,Boost为我的日常开发中带来了极大的方便,也使得我越来越依赖于boost库了.但boost功能太多,每次使用还是得翻看以前的 资料,所以为了以后可以更方便的使 ...
- boost库学习之regex
一.背景 项目中许多地方需要对字符串进行匹配,比如根据指定的过滤字符串来过滤文件名.刚开始是排斥使用boost库的,第一,我不熟悉boost库:第二,如果引入第三方库,就会增加库的依赖,这样的后果是, ...
- Guava库介绍之实用工具类
作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...
- c++ boost库学习一:时间和日期
timer类 #include <boost\timer.hpp> #include "iostream" using namespace std; int _tmai ...
- boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等
一.使用同步定时器 这个示例程序通过展示如何在一个定时器执行一个阻塞等待. //makefile #-------------------------------------------------- ...
- boost asio 学习(三)post与dispatch
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=4 本章节为io_ ...
- c++ boost库学习二:内存管理->智能指针
写过C++的人都知道申请和释放内存组合new/delete,但同时很多人也会在写程序的时候忘记释放内存导致内存泄漏.如下所示: int _tmain(int argc, _TCHAR* argv[]) ...
- boost库的安装,使用,介绍,库分类
1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...
随机推荐
- hadoop2.4完全分布式部署
hadoop2.4完全分布式部署 感谢:http://blog.csdn.net/licongcong_0224/article/details/12972889 集群组成: 两台red hat en ...
- asp.net源程序编译为dll文件并调用的实现过程
很多时候,我们需要将.cs文件单独编译成.dll文件,这就需要使用csc命令将.cs文件编译成.dll动态链接库文件.具体的操作步骤如下: 打开命令窗口->输入cmd到控制台->cd C: ...
- 使用PHP函数输出前一天的时间和后一天的时间
1.明确date()函数和time()函数的功能,其中time()函数是获取时间戳函数 2.输出前一天的当前时间: echo '一天之前的时间为:'.date('Y-m-d H:i:s',time() ...
- mysql UNION all 实现不对称数据统计
当统计多条的三个参数在不同时间段的数据的sum,又只能写在同一个sql上时,可以考虑union all三次查询, select * from ( select kk.a_time as dates,k ...
- CodeIgniter框架——访问方式 URI 分配变量 数据库操作
1.访问方式: CodeIgniter 的访问URL使用的是pathinfo,入口文件/控制器/方法(/参数列表) eg:localhost/index.php/welcome/index/id 第一 ...
- [转]libevent 环境配置
libevent 据说是IO复用的好东西.所以今天来耍耍. 1. 从官网下载源代码:http://libevent.org/,最新的版本已达到2.0. 2. 先把ubuntu系统自带的libevent ...
- 3N Numbers
D - 3N Numbers Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Let N b ...
- 2018.10.24-day3 python总结
昨日回顾:1.while2.运算符3.初始编码4.补充p2和p3的区别 Python2 (1) 今日学习目录1.整型 int() 2.布尔值 bool() 3.字符串详解 4. for循环
- Truthy Falsy
https://developer.mozilla.org/zh-CN/docs/Glossary/Truthy falsy(虚值)是在 Boolean 上下文中已认定可转换为‘假‘的值. JavaS ...
- A C Program to demonstrate adjacency list representation of graphs
w Graph and its representations - GeeksforGeekshttp://www.geeksforgeeks.org/graph-and-its-representa ...