摘要: 调用栈的英文叫做call stack,从其英文书名来看,知道它本身就是一个栈,故而它满足栈的先入后出的特性。wiki上有篇文章讲述call stack:http://en.wikipedia.org/wiki/Call_stack关于栈的溢出(stack overflow),有下面的定义:Since the call stack is organized as astack, the callerpushesthe return address onto the stack, and the called subroutine, when it finishes,popsthe return 阅读全文
posted @ 2012-06-17 18:02 Zero Lee 阅读(804) 评论(0) 推荐(0) 编辑
摘要: 关于STL 中allocator的接口与实现,C++标准有比较清楚的定义:http://en.wikipedia.org/wiki/Allocator_(C%2B%2B)关于STL的allocator的标准接口阐述,可阅读另外一篇转载文章:allocator1. SGI STL版本的allocator并没有遵守C++标准。它只提供simple_alloc类共container使用,设计的allocator名字叫做alloc,有二级配置器。第一级配置采用malloc/free来实现allocate/deallocate,第二级配置器采用针对申请的内存有特别处理,如果大小大于128字节,则转调用第 阅读全文
posted @ 2012-06-17 12:05 Zero Lee 阅读(1966) 评论(1) 推荐(1) 编辑
摘要: Given one array, and calculate its maximum sum from any sequential subarray setCpp code demo as below: 1 int maxSubarraySum(int arr[], int length, int& beg, int& end) 2 { 3 assert(arr!=0); 4 int maxSum = 0; 5 int tmpSum = 0; 6 beg = 0; end = 0; 7 for (int i = 0; i < length; i++) ... 阅读全文
posted @ 2012-06-17 11:49 Zero Lee 阅读(166) 评论(0) 推荐(0) 编辑
摘要: Given x and n, calculate its power n: 1 int power(int x, int n) 2 { 3 if (n==0) 4 return 1; 5 else (n%2==0) 6 return power(x*x, n/2); 7 else 8 return x*power(x*x, n/2); 9 }10 11 阅读全文
posted @ 2012-06-17 11:48 Zero Lee 阅读(203) 评论(0) 推荐(0) 编辑
摘要: Given one binary search tree, and convert it to one double-linked listCpp code demo as below: 1 struct BSTreeNode 2 { 3 int m_value; 4 BSTreeNode* m_pLeft; 5 BSTreeNode* m_pRight; 6 }; 7 8 BSTreeNode* convert(BSTreeNode* phead, bool asRight) 9 {10 if (!phead)11 return NULL;1... 阅读全文
posted @ 2012-06-17 11:48 Zero Lee 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 定义栈的数据结构,要求添加一个min的函数,能够得到栈的最下元素。要求函数min, push, pop的时间复杂度都是O(1) 1 class stackmin { 2 int data[N]; 3 int minidx[N]; 4 int top; 5 public: 6 stackmin() 7 : top(-1) 8 { 9 ::memset(minidx, -1, sizeof(int)*N);10 ::memset(data, 0, sizeof(int)*N);11 }12 13 ... 阅读全文
posted @ 2012-06-17 11:46 Zero Lee 阅读(311) 评论(0) 推荐(0) 编辑
摘要: 类模板的模板友元函数定义有2种方式:1. 将友元模板函数直接定义在类模板中。这种方式比较简单直接。2. 将友元模板函数声明在类模板中,定义在类模板之外。这种方式的写法,如果不小心,通常会出现编译没问题,链接时无法解析的错误。以下是一个简单的正确的例子: 1 #include <iostream> 2 #include <vector> 3 4 template <typename T> 5 class Number; 6 7 template <typename T> 8 void print(const Number<T>& 阅读全文
posted @ 2012-06-17 11:46 Zero Lee 阅读(471) 评论(0) 推荐(0) 编辑
摘要: 给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。要求:空间复杂度O(1),时间复杂度为O(n)。 1 bool func(int n) 2 { 3 return (n&1)==0; // n%2 is more expensive 4 } 5 6 void group_oddeven(std::vector<int>& a, bool (*func)(int)) 7 { 8 int i = 0, j = a.size()-1; 9 int buf = 0;10 while (i < j) {11 if (!func(a[i]... 阅读全文
posted @ 2012-06-17 11:45 Zero Lee 阅读(211) 评论(0) 推荐(0) 编辑
摘要: 编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列 1 template <typename T> 2 void displayHexBin(const T& v) 3 { 4 const unsigned char c2h[] = "0123456789ABCDEF"; 5 const unsigned char c2b[] = "01"; 6 7 unsigned char* p = (unsigned char*)&v; 8 char* buf = new char [sizeof 阅读全文
posted @ 2012-06-17 11:44 Zero Lee 阅读(208) 评论(0) 推荐(0) 编辑
摘要: 一个文件中有40亿个整数,每个整数为四个字节,内存为1GB,写出一个算法:求出这个文件里的整数里不包含的一个整数下面的代码片段仅仅是一个样例。4个字节的整数最大可表示为2^32=4294967296, 一个数一个数的读入内存,建立一个bit map,共需要4294967296个bits(也就是0.5G字节的内存,并没有超过1G内存的限制),读入每一个数,置相应的bit为1。 1 int N = 20; // # of number 2 int M = 1000; // number range 3 std::vector<int> a(N); // can be ... 阅读全文
posted @ 2012-06-17 11:43 Zero Lee 阅读(204) 评论(0) 推荐(0) 编辑
摘要: (转)C++中extern “C”含义深层探索1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同。作为一种欲与C兼容的语言,C++保留了一部分过程 式语言的特点(被世人称为“不彻底地面向对象”),因而它可以定义不属于任何类的全局变量和函数。但是,C++毕竟是一种面向对象的程序设计语言,为了支 持函数的重载,C++对全局函数的处理方式与C有明显的不同。 2.从标准头文件说起 某企业曾经给出如下的一道面试题: 面试题 为什么标准头文件都有类似以下的结构?#ifndef __INCvxWorks... 阅读全文
posted @ 2012-06-17 11:41 Zero Lee 阅读(126) 评论(0) 推荐(0) 编辑
摘要: http://en.wikipedia.org/wiki/Selection_algorithm1. partition algorithm 1 function partition(list, left, right, pivotIndex) 2 pivotValue := list[pivotIndex] 3 swap list[pivotIndex] and list[right] // Move pivot to end 4 storeIndex := left 5 for i from left to right-1 6 ... 阅读全文
posted @ 2012-06-17 11:40 Zero Lee 阅读(236) 评论(0) 推荐(0) 编辑
摘要: 删除与某个字符相邻且相同的字符,如,abcddef,删除相邻的相同字符d后变为: abcdef。要求:输入字符串,输出删除后的结果。参考STL算法: unique/unique_copy 1 void deldupchar(char* s) 2 { 3 char* i, *j; 4 if (s && *s!='\0') { 5 i = s; j = s+1; 6 while (*j!='\0') { 7 if (*i!=*j) 8 *++i = *j; 9 j... 阅读全文
posted @ 2012-06-17 11:38 Zero Lee 阅读(381) 评论(0) 推荐(0) 编辑
摘要: A permutation can be obtained by selecting an element in the given set and recursively permuting the remaining elements.At each stage of the permutation process, the given set of elements consists of two parts: a subset of values that already have been processed, and a subset that still needs to be 阅读全文
posted @ 2012-06-17 11:37 Zero Lee 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 显示一组数的全排列和组合程序: 1 void print(const std::vector<int>& s) 2 { 3 static int n = 1; 4 printf("%d:", n++); 5 printf("["); 6 for (int i = 0; i < s.size(); i++) 7 printf(" %d ", s[i]); 8 printf("]\n"); 9 }10 11 void permutation(std::vector<int>& 阅读全文
posted @ 2012-06-17 11:36 Zero Lee 阅读(300) 评论(0) 推荐(0) 编辑
摘要: 求一个正整数的平方根的程序实现:采用加法递增的方式来代替乘法与N进行比较,递增是按照等差数列的方式。 1 int square(int n) 2 { 3 int tmp = 0; 4 for (int i = 1; i < n; i++) { 5 tmp += 2*(i-1)+1; 6 if (tmp == n) 7 return i; 8 continue; 9 }10 if (n!=0) {11 printf("no integer sqare found!\n");12... 阅读全文
posted @ 2012-06-17 11:35 Zero Lee 阅读(567) 评论(0) 推荐(0) 编辑
摘要: 原文来自于:http://www.parallellabs.com/2010/10/25/practical-concurrent-queue-algorithm/多线程队列(Concurrent Queue)的使用场合非常多,高性能服务器中的消息队列,并行算法中的Work Stealing等都离不开它。对于一个队列来说有两个最主要的动作:添加(enqueue)和删除(dequeue)节点。在一个(或多个)线程在对一个队列进行enqueue操作的同时可能会有一个(或多个)线程对这个队列进行dequeue操作。因为enqueue和dequeue都是对同一个队列里的节点进行操作,为了保证线程安全, 阅读全文
posted @ 2012-06-17 11:32 Zero Lee 阅读(230) 评论(0) 推荐(0) 编辑
摘要: Allocators are one of the most mysterious parts of the C++ Standard library. Allocators are rarely used explicitly; the Standard doesn't make it clear when they should ever be used. Today's allocators are substantially different from those in the original STL proposal, and there were two oth 阅读全文
posted @ 2012-06-17 11:28 Zero Lee 阅读(661) 评论(0) 推荐(0) 编辑
摘要: 假设有一片内存,大小为m*n, m是每个单元的大小,而且>=8,共有n个这样的单元,如何将它们链接成n个节点的链表,要求不再使用任何其它内存空间。这里给出SGI STL内存分配器的一个简单实现:首先定义一个union数据结构:1 union obj {2 union obj* free_list_link;3 char client_data[1];4 };这个union结构体的最大大小为4bytes (在32bits 平台上),8bytes (在64bits平台上)。假设那片内存的地址为chunk,那么我们可以这样做: 1 obj* current_obj, *next_o... 阅读全文
posted @ 2012-06-17 11:06 Zero Lee 阅读(199) 评论(0) 推荐(0) 编辑
摘要: [代码] 阅读全文
posted @ 2010-10-17 15:22 Zero Lee 阅读(228) 评论(0) 推荐(0) 编辑
只有注册用户登录后才能阅读该文。 阅读全文
posted @ 2010-10-17 14:35 Zero Lee 阅读(26) 评论(0) 推荐(0) 编辑