![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C/C++
文章平均质量分 61
shanno
这个作者很懒,什么都没留下…
展开
-
C++逗号运算符重载
#include #include class Person{ int _id; std::string _name; unsigned int _age;public: explicit Person(int id): _id(id){ } Person(std::string name, unsigned int age):_id(0), _name(name), _a原创 2012-03-23 16:48:35 · 3535 阅读 · 2 评论 -
C++ 构造析构的顺序
#include class A{public: A(){ std::cout << "A ctor" << std::endl; } ~A(){ std::cout << "A dtor" << std::endl; }};class B{public: B(){ std::cout << "B ctor" << std::endl; } ~B(){原创 2012-06-29 08:47:08 · 592 阅读 · 0 评论 -
代码可读性例子
using namespace log4cplus;using namespace log4cplus::helpers;int main(){ Logger _logger = Logger::getInstance("test"); { SharedObejctPtr _append(new ConsoleAppender()); { std::auto_ptr _原创 2012-06-27 11:12:05 · 669 阅读 · 0 评论 -
string与数值类型的转换以及stringstream的注意点
#include #include templatevoid convertStringToAnyValue(const std::string& strValue, ValueType& val){ std::stringstream ss(strValue); ss >> val;}templatevoid convertAnyValueToString(std::st原创 2012-05-25 15:36:53 · 881 阅读 · 0 评论 -
两个大数相乘的C++实现
#include #include #include class BigIntegerMultiplier{private: /// 判断两个字符串可不可以表示一个整数 static inline bool isNumber(const char* s) { if(0 == s || '\0' == *s++) return false; while('\0' !=原创 2012-04-22 19:41:18 · 2484 阅读 · 0 评论 -
一道关于杀猪的面试题
题目:假设有n头待宰的猪,杀猪的人比较变态,他会先杀单数位置上的猪,一次杀完之后,将剩下的猪按照原来的相对位置,又从1开始排列,接着宰单数位置上的猪,直到最后杀完为止。那里有压迫哪里就有反抗,有这么变态的屠夫,就有非常聪明的猪猪。问题是最聪明的猪会站在什么位置,才能保证最后一个被宰? 解这道题目的思路跟约瑟夫环有点类似: 首先在只有一头猪的时候,最后被宰的猪的位置肯定为1;原创 2012-04-14 18:33:42 · 1373 阅读 · 0 评论 -
POCO库的Logging framework
纪念下我曾经的一句名言:需求是软件开发的根本准则。Logging architecture 需求一:记录需要包含的内容?消息的内容、消息生成的时间和地点、消息的重要性。这些东西都有Message对象来描述。一条记录就是一个Message对象。Message对象非常像一个装饰模式,Text是Message的内容,Message中的其它属性是对Text的装饰(或者用描述原创 2012-04-10 08:10:27 · 2868 阅读 · 1 评论 -
订阅者模式(观察者模式)
#include #include #include struct Notification{ virtual ~Notification() { } virtual void process() = 0;};class SubscriberA: public Notification{public: //Notification virtual void proc原创 2012-04-05 16:34:17 · 1352 阅读 · 0 评论 -
可复用的全排列实现
偶然在网上看到一道面试题,细看下才发现是曾经被面试过的全排列问题。于是手痒,想以OOD的思想重新实现一番。实现可复用软件的一个重要原则是:封装变化。是故,看下全排列代码中哪些因素是不稳定的: 1. 元素的数据类型。元素的类型可能是int, double, char等基本数据类型,不过,大部分情况下应该是用户自定义的类型。 2. 元素的存储方式。 3. 找到一个新排列后,如原创 2012-04-04 11:39:12 · 709 阅读 · 0 评论 -
std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题
在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已。std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移问题。这导致多个std::auto_ptr类型的局部变量不能共享同一个资源,这个问题是非常严重的哦。因为,我个人觉得,智能指针内存管理要解决的根本问题是:一个堆对象(或则资源,比如文件句柄)在被多个对原创 2012-03-17 10:56:38 · 36793 阅读 · 2 评论 -
生产者消费者实现
代码编译:gcc main.c -o test -lpthread代码跑起来是这个样子的:源代码:#include #include #include #include #define MAX_DATA_NUM (10)typedef struct tagObject{ int iData; pthread_cond_t stCond; pthread原创 2013-03-25 22:28:51 · 925 阅读 · 0 评论