12 2012 档案
摘要:#include using namespace std;class A {public: A() { cout using namespace std;class A {public: A() { cout << "call A()" << endl; } virtual ~A() { cout << "call ~A()"; }};class B:public A { char *buf;public: B() { buf = new char[100]; cout << "call B()&
阅读全文
摘要:为什么需要虚函数 为了实现多态,即希望派生类重新定义基类的某个函数。 #include <iostream>using namespace std;class A {public: int x; A() { x = 1000; } virtual void print() { cout << "A:x=" << x << endl; }};class B:public A {public: int y; B() { y = 2000; } void print() { cout << "B:y="
阅读全文
摘要:解决多重继承中,一个公共基类可能在派生类中产生多个拷贝的现象。 如:有一个公共基类A,类B和类C都有类A派生,类D由类B和类C派生,则类D含有类A的两个拷贝,这不仅多占内存,还可能造成多个拷贝中的数据不一致。 #include <iostream>using namespace std;class A {public: int x; A(int a = 0) { x = a; }};class B:public A {public: int y; B(int a = 0, int b = 0):A(b) { y = a; } void PB() { cout << &q
阅读全文
摘要:对于公有派生来说,可以将派生类的对象赋给基类的对象,反之是不可以的。赋值兼容性规则可以总结为4点:1、不允许将基类的对象赋给派生类的对象。2、派生类的对象可以赋给基类的对象。3、可将派生类的对象的指针赋给基类的指针变量。4、派生类对象可以初始化基类型的引用。在3、4两种情况下,使用基类的指针或引用时,只能访问从相应基类中继承来的成员,不允许访问其他基类的成员或在派生类中增加的成员。#include class Base {private: int val; char bit1;};class Child:public Base {private: char bit2;};int mai...
阅读全文
摘要:http://www.cnblogs.com/lionfight/archive/2012/05/25/2518631.html 什么时候用拷贝构造函数,和赋值构造函数:(一)当用一个已初始化过了的自定义类类型对象去初始化另一个新构造的对象的时候,拷贝构造函数就会被自动调用。也就是说,当类的对象需要拷贝时,拷贝构造函数将会被调用。以下情况都会调用拷贝构造函数: 一个对象以值传递的方式传入函数体 一个对象以值传递的方式从函数返回 一个对象需要通过另外一个对象进行初始化。 如果在类中没有显式地声明一个拷贝构造函数,那么,编译器将会自动生成一个默认的拷贝构造函数,该构造函数完成对象之间的位...
阅读全文
摘要:下面使用errno进行错误处理是错误的。 /*调用库函数*/if (errno) /*处理错误*/原因:上面代码的意图是,调用库函数失败,则会设置非零errno,从而进行错误处理;调用成功,则不进行错误处理。问题是,即使调用成功,也没有强制要求将errno设置为0,即虽然调用库函数成功,但是errno并不一定为0,可能是在调用库函数之前errno就被设置为非零值了。下面代码仍然是错误的。errno = 0;/*调用库函数*/if (errno) /*处理错误*/原因:这段代码在调用库函数之前将errno设置为0,表面上看,如果调用库函数成功,则不会处理错误。问题在于,即使调用库函数成功,err
阅读全文
摘要:下面定义的是类模板,用int实例化T后成为模板类。实例化类模板的语法:类名<模板实参表>结合下例即 Array<int> 就是将类模板Array实例化为模板类的语法。/*类模板*/#include <iostream>using namespace std;template<class T>class Array { T *data; int size;public: Array(int i) { data = new T[i]; size = i; } ~Array() { delete[] data; } T& operator[](
阅读全文
摘要:/*函数模板*/#include <iostream>using namespace std;//template<typename T> //也可以用typename关键字。template<class T>T mymax(T x, T y){ return (x > y) ? x : y;}int main(void){ cout << mymax(2.3f, 1.3f) << endl; cout << mymax(2, 3) << endl; cout << mymax('a&
阅读全文
摘要:有 a[10]; 有效下标为什么是从0到9呢,《c陷阱与缺陷》给出了解释。 不对称边界 0 <= x < 10 不对称边界就是一个左闭右开区间,0称为入届点(上届),10称为出界点(下届)。 不对称边界的好处就是: 1、上届与下届之差是元素个数。 2、如果取值范围为空,那么上届等于下届。 3、即使取值范围为空,上届也永远不可能小于下届。 数组下标正是采用了不对称边界,利用了其优点。
阅读全文
摘要:/*c语言动态数组,运行时确定数组元素个数。*/#include <stdio.h>#include <malloc.h>int main(void){ int *p; int n; /*运行时分配内存*/ scanf("%d", &n); p = (int *)malloc(sizeof(int) * n); /*输入数组元素*/ int i; for (i = 0; i < n; i++) { scanf("%d", &p[i]); } /*输出数组元素*/ int j; for (j = 0; j &l
阅读全文
摘要:int calendar[12][31]; 看作,“一维”数组有12个元素,每个元素是一个有31个整型元素的数组。
阅读全文
摘要:友元函数不是类的成员函数,没有this指针,所以必须在参数表中显式列出每一个操作数。#include <iostream>using namespace std;class Test {public: int a; int b; Test(): a(2), b(3) {}; friend ostream& operator<< (ostream& out, const Test& t) { out << t.a << "hello" << t.b; return out; }};int ma
阅读全文
摘要:在词法分析中,有条规则:每个符号应该包含尽可能多的字符,被称为“贪心法”或“大嘴法”。 K&R表述如下:如果(编译器的)输入流截止至某个字符之前都已经被分解为一个个符号,那么下一个符号将包括从该字符之后可能组成一个符号的最长字符串。 如: a---b 被编译器解释为 (a--)-b
阅读全文
摘要:常成员函数不能改变数据成员!#include <iostream>using namespace std;class Test {private: int a;public: int geta() { return a++; //正确! } int getConst() const { return a++; //错误!常成员函数不能改变数据成员。 }};int main(void){ return 0;}
阅读全文
摘要:静态成员函数如何初始化#include <iostream>using namespace std;class Test {private: static int statica; int b;public:// Test(): statica(1) {}; //错误!static数据成员不能通过初始化列表初始化。 Test(): b(1) {statica = 1;}; //正确。};int main(void){ return 0;}#include <iostream>using namespace std;class Test {private: static
阅读全文
摘要:1、友元的声明只能出现在类定义的内部,2、可以出现在类中的任何地方,3、友元不是类的成员函数!所以它的声明可以出现在类中任何地方,而不受声明前面的访问控制影响!以上几条可见下例子:#include <iostream>using namespace std;class TestPoint {private: int x; int y; friend int distanceOne(); //友元的声明可以出现在类内任何地方,它不是类的成员函数!public: friend int distanceTwo(); //友元的声明可以出现在类内任何地方,它不是类的成员函数!};int d
阅读全文
摘要:这里重点介绍?: #include <iostream>using namespace std;int main(void){ int a = 2; int b = 3; a > b ? a++ : b++; cout << a << endl; cout << b << endl; return 0;}输出:24这里a>b为假,则直接执行b++,不执行a++.
阅读全文
摘要:老版本有运算符 =- (现在是-=)这时,a=–1;有二义性!
阅读全文
摘要:/****************************************************# File : main.cpp# Author : lucasysfeng# Revision : 2014-10-13 17:04:43# Descripti...
阅读全文
摘要:/****************************************************************************** * 统计句子中的单词数。 * 算法:单词是用分隔符隔开的,因此,从开始遇到不为分割符的符号直到遇到分隔符,单词数加1. ************************************************************...
阅读全文
摘要:#include <iostream>#include <string>#include <utility>#include <vector>#include <map>#include <cctype>using namespace std;int main(){ string str("HellowoLD"); for (string::iterator iter = str.begin();...
阅读全文
摘要:/****************************************************************************** * 用str.find_first_of(cstr, pos)查找str中的所有数字, * 算法:没查找到一个pos,从该pos的下一元素重新查找。 *********************************************...
阅读全文
摘要:sting s;记住典型的查找操作:s.find(args) s.find_fist_of(args)s.find_fist_not_of(args)还有3个逆序的查找操作,s.rfind(args)s.find_last_of(args)s.find_last_not_of(args)其中args可以为下面4个版本之一:c,poss2,poscp,poscp,pos,n例子:/****************************************************************************** * str.find(args)是完全匹配args中的字符串
阅读全文
摘要:cin输入流,空格隔开每个变量,回车接收,ctrl+z结束! /******************************************************************/#include <iostream>#include <vector>#include <string>#include <cctype>using namespace std;int main()...
阅读全文
摘要:《c专家编程》区别1:#define peach intusigned peach i; /*正确*/typedef int banana;unsigned banana i; /*错误*/区别2:#define int_ptr int *int_ptr chalk, cheese;宏扩展后相当于int * chalk,cheese;typedef int * int_ptr;int_ptr chalk, cheese;则表示int *chalk,*cheese;用typedef定义的类型能够保证声明中的变量为同一类型!
阅读全文
摘要:下面定义是对的:const int *a;int *const b; //b指向不可改int const *c; //c指向的变量的值不可改下面定义错误!const *int e;const在*的左边则指向的变量值只读,const在*的右边则指针指向只读,即“左定值,右定向”!注意:左定值右定向的参照物是* !!!
阅读全文
摘要:http://yantingting1219.blog.sohu.com/71850367.html cin 读取并忽略 非空白字符 之前所有的空白字符,然后读取字符直至再次遇到空白字符,读取终止。 /*sting对象接收流中字符*/#include <iostream>#include <string>using namespace std;int main(){ string str; cin...
阅读全文
摘要:#include <iostream>#include <string>#include <cstring>using namespace std;int main(int argc, char** argv){ string str("hello world!,pun.test?good"); for (string::size_type i=0; i!=str.size(); i++) { ...
阅读全文
摘要:算法:只将 srcStr字符串中的 非标点字符 赋给destStr./*去掉字符串中的标点,字符指针操作*/#include <iostream>#include <ctype.h>#include <assert.h>using namespace std;char *delPunct(char *destStr, const char *srcStr){// assert(destStr...
阅读全文
摘要:单词用空白键隔开。算法:每当遇到一个非空白字符,且该非空白字符的前面是空白字符,则单词数加1.该非空白符的前面是不是空白符用isword表示。注意不能用空白符的个数来表示单词的个数!#include <stdio.h>#include <string.h>int nword(char *s){ int nword = 0; int isword = 1; while (*s) { if (iss...
阅读全文
摘要:/*字符串中大写字母变成小写,其余字符不变*/#include <stdio.h>#include <string.h>char* mystrlwr(char *s){ char *scopy = s; while (*s) { if (*s >= 'A' && *s <= 'Z') { *s = *s + 'a' - 'A'; } s++; } return scopy;}char *...
阅读全文
摘要:1、字符数组不会自动添加’\0’,字符串会自动添’\0’;所以sizeof(c1)==3,sizeof(c4)==4;strlen(c1)==不可预知,strlen(c3)==3;2、sizeof结果是变量所占内存大小;strlen结果是遇到的第一个’\0’之前的字符数。#include <stdio.h>int main(void){ char c1[] = {'a', 'b', 'c'}; char c2[] = {'a', 'b', 'c', '\0'}; char
阅读全文
摘要:qsort(数组名,元素个数,元素类型大小,cmp);#include <stdio.h>#include <stdlib.h>int cmp(const void *a, const void *b){ return (*(int *)a - *(int *)b); //从小到大。// return (*(int *)b - *(int *)a); //从大到小。}int main(vo...
阅读全文
摘要:#include <stdio.h>#include <string.h>#include <assert.h>#include <stdlib.h>int main(int argc, char* argv[]){ char dst[] = "abcdefgh"; char *src = "xyzd"; unsigned char map[32] = {'0'} ; printf("%d", s...
阅读全文
摘要:#include <stdio.h>#include <string.h>#include <assert.h>#include <stdlib.h>int main(int argc, char* argv[]){ char dst[] = "abcxdeyf"; char *src = "afbecd"; printf("%d", strspn(dst, src)); return 0;}输出...
阅读全文
摘要:/****char *strstr(string1, string2) - search for string2 in string1**Purpose:* finds the first occurrence of string2 in string1**Entry:* char *string1 - string to search in* char *st...
阅读全文
摘要:;***;char *strrchr(string, ch) - find last occurrence of ch in string;;Purpose:; Finds the last occurrence of ch in string. The terminating; null character is used as part of the search.;...
阅读全文
摘要:;***;char *strchr(string, chr) - search a string for a character;;Purpose:; Searches a string for a given character, which may be the; null character '\0'.;; Algorithm:; char *...
阅读全文
摘要:;***;int strncmp(first, last, count) - compare first count chars of strings;;Purpose:; Compares two strings for lexical order. The comparison stops; after: (1) a difference between the st...
阅读全文
摘要:;***;strcmp - compare two strings, returning less than, equal to, or greater than;;Purpose:; Compares two string, determining their lexical order. Unsigned; comparison is used.;; A...
阅读全文
摘要:#include <stdio.h>#include <string.h>#include <assert.h>#include <stdlib.h>char *mystrcat (char * dst, const char * src){ char * cp = dst; while( *cp ) /* 不写成while(*cp++)的原因是cp可能为'\0'*/ ++cp; ...
阅读全文
摘要:#include <stdio.h>#include <string.h>#include <assert.h>#include <stdlib.h>char *mystrncpy(char *dst, const char *src, size_t n){ assert(dst != NULL && src != NULL); char *dstCopy = dst; while (n && (...
阅读全文
摘要:http://bbs.chinaunix.net/thread-25356-1-1.html #include <stdio.h>#include <string.h>#include <assert.h>#include <stdlib.h>char *strcpy(char *strDest, const char *strSrc){ assert(strDest != NULL && st...
阅读全文
摘要:http://www.onmoso.com/android/331.html
阅读全文
摘要:#include <assert.h>void assert(scalar expression);表达式为假,则调用abort终止程序的执行。也就是说,只有在满足表达式的情况下,程序才继续执行。
阅读全文
摘要:首先要知道控制字符,可打印字符等所对应的ASCII码是多少。 0-31和127 控制字符 32-126 可打印字符 65-90 大写字母 97-122 小写字母 48-57 0-9数字 32 空格 字符类测试库函数所在头文件 <ctype.h> isupper()测试字符是否为大写英文字 ispunct()测试字符是否为标点符号或特殊符号 isspace()测试字符是否为空格字符 isprin...
阅读全文
摘要:#include <stdio.h>#include <ctype.h>/*大小写转换的库函数:int tolower(int c)int toupper(int c)*/int my_tolower(unsigned char c){ return (c + ('a' - 'A'));}int my_toupper(unsigned char c){ return (c - ('a' - 'A'...
阅读全文
摘要:#include <stdio.h>#include <ctype.h>/*判断是控制字符(ASCII 0-31和127)的库函数:满足指定的条件,返回非0;否则返回0.iscntrl(c)*//*************** * 输入:要判断的字符。 * 输出:是空白,返回1;其他,返回0. **************/int my_iscntrl(unsigned char c){ if (...
阅读全文
摘要:#include <stdio.h>#include <ctype.h>/*判断是空白(空格-ASCII 32,换页-12,换行-10,回车-13,横向制表-9,纵向制表-11)的库函数:满足指定的条件,返回非0;否则返回0.isspace(c)*//*************** * 输入:要判断的字符。 * 输出:是空白,返回1;其他,返回0. **************/int my_is...
阅读全文
摘要:#include <stdio.h>#include <ctype.h>/*判断是字母或数字的库函数:满足指定的条件,返回非0;否则返回0.isalpha(c)isdigit(c)*//*************** * 输入:要判断的字符。 * 输出:是字母,返回1;其他,返回0. **************/int my_isalpha(unsigned char c){ if ((c >=...
阅读全文
摘要:#include <stdio.h>#include <ctype.h>/*判断大小写的库函数:满足指定的条件,返回非0;否则返回0.isupper(c)islower(c)*//*************** * 输入:要判断的字符。 * 输出:是小写,返回1;其他,返回0. **************/int my_islower(unsigned char c){ if (c >= 'a'...
阅读全文

浙公网安备 33010602011771号