2013年8月30日
摘要: It's not clear if it's legal or portable, but it is rather popular.#include #include struct line { int length; char contents[]; // Flexible array member};int main(){ int this_length = 20; struct line* this_line = (struct line*) malloc(sizeof(struct line) + sizeof(char) * this_length); this_l 阅读全文
posted @ 2013-08-30 11:14 chenkkkabc 阅读(327) 评论(0) 推荐(0) 编辑
  2013年8月7日
摘要: 作为类成员使用。前缀是先加/减1,再取值;后缀是先取值,再加/减1。前缀是左值,返回引用;后缀是右值,返回值。后缀多一个int参数进行区分,用时编译器会传个没用的0作实参。在后缀实现中调用前缀版本。可以显式调用:前缀 xxx.operator++(); 后缀 xxx.operator++(0)#include #include class CheckedPtr {public: // no default ctor CheckedPtr(int* b, int *e) : beg(b), end(e), cur(b) {} // prefix operators CheckedPtr& 阅读全文
posted @ 2013-08-07 15:09 chenkkkabc 阅读(337) 评论(0) 推荐(0) 编辑
  2013年8月5日
摘要: Matthew Effect强者恒强,弱者恒弱,赢家通吃。 阅读全文
posted @ 2013-08-05 12:49 chenkkkabc 阅读(165) 评论(0) 推荐(0) 编辑
  2013年8月2日
摘要: Flywheel Effect万事开头难。飞轮效应指为了使静止的飞轮转动起来,一开始你必须使很大的力气,一圈一圈反复地推,每转一圈都很费力,但是每一圈的努力都不会白费,飞轮会转动得越来越 快。达到某一临界点后,飞轮的重力和冲力会成为推动力的一部分。这时,你无须再费更大的力气,飞轮依旧会快速转动,而且不停地转动。 阅读全文
posted @ 2013-08-02 21:38 chenkkkabc 阅读(265) 评论(0) 推荐(0) 编辑
  2013年7月31日
摘要: 假设是ASCII字符集,开一个128大小的bitset来记录每个字符。#include #include #include bool isUniqueChars(std::string s){ std::bitset a; size_t len = s.length(); for (size_t i = 0; i < len; ++i) { char idx = s[i]; if (a.test(idx)) { return false; } a.set(idx); } return true;} 阅读全文
posted @ 2013-07-31 17:23 chenkkkabc 阅读(399) 评论(0) 推荐(0) 编辑
  2013年7月23日
摘要: 支持类型安全地存储和获取任意类型的值#include #include #include #include typedef std::list many;void append_int(many & values, int value){ boost::any to_append = value; values.push_back(to_append);}void append_string(many & values, const std::string & value){ values.push_back(value);}void append_char_ptr(m 阅读全文
posted @ 2013-07-23 16:44 chenkkkabc 阅读(234) 评论(0) 推荐(0) 编辑
  2013年7月17日
摘要: BOOST_FOREACH简化了C++的循环遍历序列元素。支持的序列类型:Boost.Range识别的序列STL容器数组Null-terminated Stringstd::pair of iterators#include #include #include int main(){ std::string hello( "Hello, world!" ); BOOST_FOREACH( char ch, hello ) { std::cout list_int( /*...*/ );BOOST_FOREACH( int i, list_int ){ /... 阅读全文
posted @ 2013-07-17 15:31 chenkkkabc 阅读(356) 评论(0) 推荐(0) 编辑
摘要: http://www.cplusplus.com/reference/algorithm/for_each/对一个序列应用函数。可以是函数指针,或者是functor。// for_each example#include // std::cout#include // std::for_each#include // std::vectorvoid myfunction (int i) { // function: std::cout myvector; myvector.push_back(10); myvector.push_back(20); ... 阅读全文
posted @ 2013-07-17 12:26 chenkkkabc 阅读(251) 评论(0) 推荐(0) 编辑
  2013年6月22日
摘要: 采用 #pragma weak name 形式时,指令使 name 成为弱符号。链接程序没有找到 name 的符号定义时,不会显示错误消息,也不会出现符号的多个弱定义的错误消息。链接程序仅执行第一个遇到的定义。如果另一个编译单元有函数或变量的强定义,那么 name 将链接到它。如果没有 name 的强定义,那么链接程序符号的值为 0。Example1编译单元A cu1.c#include extern int foo;#pragma weak fooint main() { int *ptr; ptr = &foo; if (ptr == 0) { printf("foo h 阅读全文
posted @ 2013-06-22 16:46 chenkkkabc 阅读(1359) 评论(0) 推荐(0) 编辑
  2013年6月20日
摘要: 代码思路来自Thinking in C++ 10.13.1内部类方法。类似多重继承,但是类型转换是单向的:Outer支持“向上”转型,但是不能“向下”转型回Outer。#include #include using namespace std;class Interface1{public: virtual void function1() = 0;};void CallInterface1(Interface1& if1){ if1.function1();}class Interface2{public: virtual void function2() = 0;};void Ca 阅读全文
posted @ 2013-06-20 14:11 chenkkkabc 阅读(271) 评论(0) 推荐(0) 编辑