上一页 1 2 3 4 5 6 7 ··· 30 下一页
摘要: 按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示:class String {String ( const char* p );//用C风格的字符串p作为初始化值//…}String s1 = “hello”;//OK 隐式转换,等价于String s1 = String(“hello”);但是有的时候可能会不需要这种隐式转换,如下:class String { String ( int n );//本意是预先分配n个字节给字符串String ( const char* p );//用C风格的字符串p作为初始化值//…}下面两种 阅读全文
posted @ 2012-09-29 14:11 瑞尼书苑 阅读(203) 评论(0) 推荐(0)
摘要: 1. 获取时间戳:time_t rawtime;time(&rawtime); //结果是时间戳2. 将时间戳转为 char* 类型的日期Www Mmm dd hh:mm:ss yyyy 1 /* ctime example */ 2 #include <stdio.h> 3 #include <time.h> 4 #include <iostream> 5 6 using namespace std; 7 8 int main () 9 {10 time_t rawtime;11 12 time ( &rawtime );13 printf 阅读全文
posted @ 2012-09-29 11:54 瑞尼书苑 阅读(8013) 评论(0) 推荐(0)
摘要: 有时候经常要实现程序在某个时间段进行某些操作的功能,如果利用boost库的话可以很方便就能实现。[cpp] #include<boost/date_time/posix_time/posix_time.hpp> #include<boost/date_time/gregorian/gregorian.hpp> //首先要定义一个时间区间段,设程序需在每日的8:50~9:15之间实现某些操作; boost::posix_time::time_period*pTimePeriod=NULL; boost::posix_time::ptimept(b... 阅读全文
posted @ 2012-09-28 17:38 瑞尼书苑 阅读(693) 评论(0) 推荐(0)
摘要: 可以设置函数属性(Function Attribute), 变量属性(Variable Attribute), 类型屬性(Type Attribute)语法: __attribute__ (parameter) 注: attribute 前后各有两个下划线(underscore) 后面紧跟小括号((bracket)), 括号中间为__attribute__参数.其位置一般于声明的尾部; 以前函数属性(Function Attribute) 函数属性可以帮助开发者把一些特性添加到函数声明中,从而可以使编译器在错误检查方面的功能更强 大。__attribute__机制也很容易同非GNU应用程序做到 阅读全文
posted @ 2012-09-27 16:59 瑞尼书苑 阅读(158) 评论(0) 推荐(0)
摘要: #include <string>//使用C++标准库的string类时using namespace std;//同上#include <sstream>#include <iostream>#include <stdlib.h>//要将string类和int类型直接转换最好有这些包含,//因为自己写一个转换函数比较方便,函数定义参考如下string getstring ( const int n ){std::stringstream newstr;newstr<<n;return newstr.str();}////////// 阅读全文
posted @ 2012-09-27 14:19 瑞尼书苑 阅读(225) 评论(0) 推荐(0)
摘要: 在编写多线程程序时,多个线程同时访问某个共享资源,会导致同步的问题,这篇文章中我们将介绍C++11 多线程编程中的数据保护。数据丢失让我们从一个简单的例子开始,请看如下代码:01#include <iostream>02#include <string>03#include <thread>04#include <vector>0506usingstd::thread;07usingstd::vector;08usingstd::cout;09usingstd::endl;1011classIncrementer12{13private:14in 阅读全文
posted @ 2012-08-29 10:56 瑞尼书苑 阅读(276) 评论(0) 推荐(0)
摘要: 引子1 猜猜看,下面这段代码有什么问题: void Delay(UINT32 n) { while(—n); } 答案: 本来代码完全正常,但是为了优化性能,打开了编译器的优化。但是发现这个函数被编译器优化掉了。为了防止被优化掉,需要给函数的参数加上volatile: void Delay(volatile UINT32 n) { while(—n); } 引子2 再猜猜看,这段代码有什么问题: int square(volatile int *ptr) { return *ptr * *ptr; } 答案: 由于*ptr指向一个volatile型参数,编译器将产生类... 阅读全文
posted @ 2012-08-29 10:40 瑞尼书苑 阅读(187) 评论(0) 推荐(0)
摘要: try{} catch(…){}以前都是用try{} catch(…){}来捕获C++中一些意想不到的异常,今天看了Winhack的帖子才知道,这种方法在VC中其实是靠不住的。例如下面的代码:try{BYTE* pch ;pch = ( BYTE* )00001234 ;//给予一个非法地址*pch = 6 ; //对非法地址赋值,会造成Access Violation 异常}catch(...){AfxMessageBox( "catched" ) ;}这段代码在debug下没有问题,异常会被捕获,会弹出”catched”的消息框。但在Release方式下如果选择了编译器 阅读全文
posted @ 2012-08-28 14:07 瑞尼书苑 阅读(968) 评论(0) 推荐(0)
摘要: unix时间相关,也是在标准库里面的。 1.timegm函数只是将struct tm结构转成time_t结构,不使用时区信息 time_t timegm(struct tm *tm); 2.mktime使用时区信息 time_t mktime(struct tm *tm); timelocal 函数是GNU扩展的与posix函数mktime相当 time_t timelocal (struct tm *tm); 3.gmtime函数只是将time_t结构转成struct tm结构,不使用时区信息 struct tm * gmtime(const time_t *clock); ... 阅读全文
posted @ 2012-08-14 11:22 瑞尼书苑 阅读(1607) 评论(0) 推荐(0)
摘要: 在我们使用CentOS系统的时候,CentOS防火墙有时是需要改变设置的。CentOS防火墙默认是打开的,设置CentOS防火墙开放端口方法如下:打开iptables的配置文件:vi /etc/sysconfig/iptables修改CentOS防火墙时注意:一定要给自己留好后路,留VNC一个管理端口和SSh的管理端口下面是一个iptables的示例:# Firewall configuration written by system-config-securitylevel# Manual customization of this file is not recommended.*filt 阅读全文
posted @ 2012-08-04 12:28 瑞尼书苑 阅读(268) 评论(0) 推荐(0)
上一页 1 2 3 4 5 6 7 ··· 30 下一页