02 2013 档案

Cracking the Coding Interview(1)
摘要:1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?判断一个单词中是否存在相同字母#include <iostream>#include <string>using namespace std;int unique(const string& a){ bool boola[255]={0}; for (int i=0;i<a.length();i++) 阅读全文

posted @ 2013-02-25 14:05 mymemory 阅读(193) 评论(0) 推荐(0)

static成员函数、成员变量
摘要:class Cat{public: Cat(int age):itsAge(age){HowManyCats++;} virtual ~Cat(){HowManyCats--;} virtual int getAge(){return itsAge;} virtual void setAge(int age){itsAge=age;}; static int HowManyCats;private: int itsAge;};// int Cat::HowManyCats=0; //对静态成员数据如... 阅读全文

posted @ 2013-02-18 16:26 mymemory 阅读(227) 评论(0) 推荐(0)

对象初始化
摘要:struct Test{ Test(int){} Test(){} void fun(){cout<<"func"<<endl;}};int main(){ Test a(1); Test b(); //此处是一个函数声明,返回值为Test类型,而不是对象定义! Test b1; //对象定义 Test *c = new Test(); Test *d = new Test(2); a.fun(); b.fun(); //error!c... 阅读全文

posted @ 2013-02-18 16:19 mymemory 阅读(152) 评论(0) 推荐(0)

深拷贝与浅拷贝
摘要:c++默认的拷贝构造函数是浅拷贝浅拷贝就是对象的数据成员之间的简单赋值,如你设计了一个没有类而没有提供它的复制构造函数,当用该类的一个对象去给令一个对象赋值时所执行的过程就是浅拷贝,如:class A {public: A(int _data) : data(_data){} A(){}private: int data; };int main(){ A a(5); A b = a; // 仅仅是数据成员之间的赋值 }这一句b = a;就是浅拷贝,执行完这句后b.data = 5;如果对象中没有其他的资源(如:堆,文件,系统资源等),则深拷贝和浅拷贝没有什么区别... 阅读全文

posted @ 2013-02-16 21:08 mymemory 阅读(1545) 评论(0) 推荐(0)

zigzag数组,螺旋数组
摘要:/*zigzag数组是一个“之”字形排列的数组,如8*8的zigzag数组: 1 5 6 14 15 27 28 4 7 13 16 26 29 42 8 12 17 25 30 41 43 11 18 24 31 40 44 53 19 23 32 39 45 52 54 22 33 38 46 51 55 60 34 ... 阅读全文

posted @ 2013-02-16 16:34 mymemory 阅读(224) 评论(0) 推荐(0)

程序员面试宝典之输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序,即输出125,126,145,146
摘要://============================================================================// Name : 2例一.cpp// Author :// Version :// Copyright : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#in... 阅读全文

posted @ 2013-02-16 15:44 mymemory 阅读(1346) 评论(0) 推荐(0)

数组指针:a pointer to an array,即指向数组的指针
摘要:#include <iostream>#include <stdio.h>using namespace std;int main(){ int test[2][3]={{1,2,3},{4,5,6}}; int (*A)[3]; A = &test[0];// A = test cout<<(*A)[0]<<endl; cout<<(*A)[4]<<endl; cout<<**A<<endl; cout<<**(A+1)<<endl; // cout<< 阅读全文

posted @ 2013-02-16 00:52 mymemory 阅读(268) 评论(0) 推荐(0)

GetMemory
摘要:#include <iostream>#include <stdio.h>#include <stdlib.h>#include <cstring>using namespace std; void GetMemory(char* &p, int num) { p = (char *)malloc(sizeof(char)*num); } void GetMemory1(char** p, int num) { *p=(char*)malloc(sizeof(char)*num); } char* GetMemory2(char* p, 阅读全文

posted @ 2013-02-15 18:59 mymemory 阅读(195) 评论(0) 推荐(0)

const define sizeof extern
摘要:extern:extern是全局变量声明只要声明全局变量就默认,前面加extern(程序员可以不加,但编译器默认加上)若本文件 引用别的文件中的全局变量 一定要加上extern 声明一下例如: #include "my_Fun.c" extern int b; //b是在my_Fun.c中声明了的一个全局变量 这个extern 是个声明他可以在任何地方声明 引用了一个全局变量 (可以试试 在main()函数执行完之后声明 也不会出错)这样在 工程的总头文件中就不需要考虑 先#include 哪个文件了用#include可以包含其他头文件中变量、函数的声明,为什么还要exte 阅读全文

posted @ 2013-02-07 17:49 mymemory 阅读(280) 评论(0) 推荐(0)

x&(x-1)表达式的意义
摘要:求下面函数的返回值(微软) -- 统计1的个数-------------------------------------int func(int x){ int countx = 0; while(x) { countx++; x = x&(x-1); } return countx;}假定x = 999910011100001111答案: 8思路: 将x转化为2进制,看含有的1的个数。注: 每执行一次x = x&(x-1),会将x用二进制表示时最右边的一个1变为0,因为x-1将会将该位(x用二进制表示时最右边的一个1)变为0。 判断一... 阅读全文

posted @ 2013-02-05 15:23 mymemory 阅读(180) 评论(0) 推荐(0)

导航