
C/C++
born1985man
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
几种效果较好的简单hash函数
// RS Hash Functionunsigned int RSHash(char *str){ unsigned int b = 378551; unsigned int a = 63689; unsigned int hash = 0; while (*str) { hash = hash * a + (*str+原创 2009-10-02 11:24:00 · 885 阅读 · 0 评论 -
拷贝构造函数 赋值操作符
#include using namespace std;class A{ public: A(){cout A(A&a){cout A & operator=(const A &a){cout};int main(void){ A a; A b(a); A c=a;//对原创 2009-10-20 16:01:00 · 125 阅读 · 0 评论 -
一条语句打印1到n,再打印n到1
写一个函数 int p(int i, int N); 能够输出i到N再到i,即以参数1,7调用函数,输出结果为 1 2 3 4 5 6 77 6 5 4 3 2 1 要求只用一个语句完成,不允许用?:等n多操作符和关键字 #include using namespace std;#define PRINT(原创 2009-10-19 17:44:00 · 310 阅读 · 0 评论 -
C++类型转换
C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:TYPE b = (TYPE)a。C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。看似问题复杂化了,其实不然。C风格的类型转换在程序语句中难以识别,类型转换是去const,还是int转换成char,或是子类指针转换成父类指针?C括号风格的强制类型转换解决不了这些问题。C++的4种类型转换能转载 2009-10-19 21:10:00 · 279 阅读 · 0 评论 -
参数传递类型提升
#include using namespace std;void test(char a){coutvoid test(int a){coutvoid test(float a){cout//void test(double a){coutint main(void){ test(1); test(c); test(2+原创 2009-10-20 16:34:00 · 170 阅读 · 0 评论 -
C++成员函数作为函数指针传递
不可以直接通过函数指针来调用类的非静态成员函数下面的例子将报错#include using namespace std;/*不可以直接通过函数指针来调用类的非静态成员函数*/typedef void (*PFN)();class A{ public: void foo(){run(func);} private:原创 2009-10-20 23:42:00 · 356 阅读 · 0 评论 -
位运算
===== 真正强的东西来了! =====二进制中的1有奇数个还是偶数个 我们可以用下面的代码来计算一个32位整数的二进制中1的个数的奇偶性,当输入数据的二进制表示里有偶数个数字1时程序输出0,有奇数个则输出1。例如,1314520的二进制101000000111011011000中有9个1,则x=1314520时程序输出1。var i,x,c:l转载 2009-10-21 15:53:00 · 164 阅读 · 0 评论 -
C++ static变量初始化问题
首先static变量只有一次初始化,不管在类中还是在函数中..有这样一个函数:view plaincopy to clipboardprint?void Foo() { static int a=3; // initialize std::cout a++;原创 2009-10-20 22:54:00 · 333 阅读 · 0 评论 -
static_cast和dynamic_cast
#include "head.h"class B{ public: virtual ~B(){} virtual void op1()=0;};class D1:public B{ public: void op1(){cout<<"D1::op1()/n";}; void op2(){cout<<"D1::op2()/n";}; virtual原创 2009-11-06 11:40:00 · 196 阅读 · 0 评论 -
C++多继承下虚函数
直接上代码#include using namespace std;class A{ public: virtual void funcA(){}};class B1:public virtual A{ public: virtual void funcB1(){}};class B2:public virtual A{ public:原创 2009-11-14 11:37:00 · 227 阅读 · 0 评论 -
Exception specifications (C++ only)
Exception specifications (C++ only)C++ provides a mechanism to ensure that a given function is limited tothrowing only a specified list of exceptions. An exception specification atthe b转载 2009-11-12 11:37:00 · 380 阅读 · 0 评论 -
C中宏#和##的运用
#include #include #include #include #define f(a,b) a##b#define g(a) #a#define h(a) g(a)int main() { char a = a; printf("%s/n",g(a)); // a printf("%s/n原创 2009-11-17 21:05:00 · 188 阅读 · 0 评论 -
printf的参数
#include #include using namespace std;int main(void){ unsigned short ch=0xFFFE; printf("%x,%x/n",ch,(short)ch); return 0;}传给printf的参数当作int型处理 -1是0xFFFFFFFF原创 2009-10-20 15:54:00 · 134 阅读 · 0 评论 -
EC++::Item 9::Never call virtual functions during construction or destruction
#include using namespace std;class Base{ public: Base(){Init();} virtual void Init(){cout void func(){cout};class Derived:public Base{ public: v原创 2009-10-20 15:42:00 · 231 阅读 · 0 评论 -
C++中的引用和指针
实际上引用可以被看作一个const指针,每次使用的时候都会取值1. 指针可以指向NULL,但引用则必须引用非空的对象char *pc=NULL;char &rc=*pc;是未定义的 不同的编译器处理可能不同2.引用必须在定义的时候初始化,否则报错string& rs; // error! References must be initialized原创 2009-10-08 19:15:00 · 256 阅读 · 0 评论 -
C++中自增和自减操作符的重载
自增(自减)的前后缀操作符由于没有带参数,所以重载的时候要区别开来处理,具体的做法是给后缀操作符号一个int 型的形参,这样编译器会自动传递实参0过去。实现的原则是一切向int看齐class UPInt { // "unlimited precision int"public: UPInt& operator++();原创 2009-10-08 19:35:00 · 614 阅读 · 0 评论 -
C++中const用法总结
作者JuKevin1. const修饰普通变量和指针const修饰变量,一般有两种写法:const TYPE value;TYPE const value;这两种写法在本质上是一样的。它的含义是:const修饰的类型为TYPE的转载 2009-10-08 20:05:00 · 293 阅读 · 0 评论 -
C++空类,默认产生哪些成员函数
class Empty{public: Empty(); // 缺省构造函数 Empty( const Empty& ); // 拷贝构造函数 ~Empty(); // 析构函数 Empty& operator=( const Em原创 2009-10-12 15:56:00 · 438 阅读 · 0 评论 -
C++题收集
用代码说明问题1.多态#include using namespace std;class A{public: virtual void print(void) { cout <<"A::print()" <<endl; }};class B:public A{public: virtual void print(void) { co原创 2009-10-12 20:42:00 · 366 阅读 · 0 评论 -
C traps and Puzzles
下面的例子都在Ubuntu8.04 GCC下编译的结果,有些没有给结果编程一定要自己动手试一试!1.定义与声明,定义要分配内存,声明只是声明在别处定义了int a; //定义extern int a; //声明 char str[100]extern char str[] //ok char * str[]extern char str[]原创 2009-10-14 11:27:00 · 231 阅读 · 0 评论 -
大端法,小端法字节序
关于字节序(大端法、小端法)的定义 《UNXI网络编程》定义:术语“小端”和“大端”表示多字节值的哪一端(小端或大端)存储在该值的起始地址。小端存在起始地址,即是小端字节序;大端存在起始地址,即是大端字节序。 也可以说: 1.小端法(Little-Endian)就是低位字节排放在内存的低地址端即该值的起始地址,高位字节排放在内存的高地址端。 2.大端法(Big转载 2009-10-12 19:59:00 · 567 阅读 · 1 评论 -
用位运算求最大值最小值
#include using namespace std;#define AB(a,b) (((a-b)>>31)&0x1)void minmax(int a,int b){ int max,min; max=(abs(a-b)+a+b)/2; min=((a+b)-abs(a-b))/2; cout} /*Use the原创 2009-10-15 16:08:00 · 863 阅读 · 0 评论 -
Linux下调用pthread库实现简单线程池
#include #include #include #include #include #include void * routine(void * arg);int pool_add_job(void *(*process)(void * arg),void *arg);int pool_init(unsigned int thread_num);int p原创 2009-10-15 21:47:00 · 2090 阅读 · 2 评论 -
Linux下通用线程池的创建与使用
Linux下通用线程池的创建与使用本文给出了一个通用的线程池框架,该框架将与线程执行相关的任务进行了高层次的抽象,使之与具体的执行任务无关。另外该线程池具有动态伸缩性,它能根据执行任务的轻重自动调整线程池中线程的数量。文章的最后,我们给出一个简单示例程序,通过该示例程序,我们会发现,通过该线程池框架执行多线程任务是多么的简单。 为转载 2009-10-16 17:11:00 · 604 阅读 · 0 评论 -
Gcc参数压栈顺序
#include void func(int i,int j,int k);int main(void){ static int a=0; func(a++,++a,a++); return 0;}void func(int i,int j,int k){ int l=1; int m=2; int n=3; int *A=(int *)mall原创 2009-10-20 11:16:00 · 864 阅读 · 0 评论 -
测试对C的掌握程度
下面这个程序输出什么?enum {false,true};int main(){ int i=1; do { printf("%d/n",i); i++; if(i cont原创 2009-11-17 21:01:00 · 253 阅读 · 0 评论