08 2011 档案
摘要:1. C++中有两种数据成员,static 和 非static,以及3种成员函数,static,非static和virtual函数 static数据成员和 非static 成员函数放在class 对象的外面,virtual 函数机制(1) 每个class 产生一堆指向irtual 函数的指针,放在表格中(?) (2) 每一个class 对象被添加一个指针vptr,指向相关的vtable,这个vptr的设定和重置由每一个class的 构造函数,析构函数,和拷贝构造函数自动完成
阅读全文
摘要:1. 关于sizeof的运行结果#include <iostream>//在GCC 中using namespace std;class X{};class Y :public X{};class Z : public X{};class W :public Y,public Z{};int main(){ cout << sizeof(X) << endl;//1 cout << sizeof(Y) << endl;//1 cout << sizeof(Z) << endl;//1 cout <<
阅读全文
摘要:1.#include <iostream>using namespace std;class Foo{ public: Foo(),Foo(int);};//这里被合成的Bar default 构造函数内含有member //object,foo拥有default constructor,所以会初始化Bar::foo()//但是初始化str 的责任是程序员的,即编译器不会初始化str class Bar{ public: Foo foo;//内含 char *str;};void foo_bar(){ Bar bar; }int main(){ ...
阅读全文
摘要:1. 由于本例中使用了 boost 的shared_ptr,先看一个shared_ptr:shared_ptr使用的是引用计数方式#include <string>#include <iostream>#include <boost/shared_ptr.hpp>using namespace std;class implementation{ public: ~implementation() { cout << "析构函数" << endl; } void f() { cout << "函
阅读全文
摘要:1. Java实现interface NetWork{ public abstract void browser();}class Real implements NetWork{ @Override public void browser() { // TODO Auto-generated method stub System.out.println("浏览信息"); }}public class designpattern_proxy implements NetWork{ private NetWork newwork; ...
阅读全文
摘要:0 转自 http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html(1)Java中每个程序至少启动2个线程,一个main线程,一个垃圾回收线程,(2)判断线程是否启动的方法: isAlive()(3) 线程的强制执行: join()(4) 线程的休眠: sleep()Thread.sleep(2000);(5)demo.interrupt();//2s后中断线程(6)demo.setDaemon(true); //后台运行线程(7)h1.setPriority(8);//线程的优先级,优先级的高低与谁 先执行 还不是必然
阅读全文
摘要:1 当我们以面向对象的观点进入template C ++ 时,继承就表现的和以前不同了C ++ 编译器不进入template base class 观察,解决方式有3种:this->using 声明式明确的支出被调用的函数是属于base类的item 43 的例子#include <iostream>using namespace std;class companyA{ public: void sendCleartext(const string &msg); void sendEncrypted(const string &msg);};void compa
阅读全文
摘要:1. n! 当n比较大时,采用数组存放结果#include <iostream>using namespace std;#define MAX 1000//n比较小的时候void n_(int n){ int result = 1; for(int i = 2;i <= n; i ++) result *= i; cout << result << endl;}//函数功能,求n!void f(int n){ int a[MAX]; int p,h;//p 存储当前的结果的位数,h为进位 a[1] = 1; //用来存储运算结果 p = 1...
阅读全文
摘要:1. 递归法 #include <iostream>/* 整数划分问题 http://www.cnblogs.com/dolphin0520/archive/2011/04/04/2005098.html*////* f(n, m)= 1; (n=1 or m=1) f(n,m) = f(n, n); (n<m) 1+ f(n, m-1); (n=m) f(n-m,m)+f(n,m-1)...
阅读全文
摘要:转自: http://www.cnblogs.com/qsort/archive/2011/08/23/2150089.html1,一个整数数列,元素取值可能是1~N(N是一个较大的正整数)中的任意一个数,相同数值不会重复出现。设计一个算法,找出数列中符合条件的数对的个数,满足数对中两数的和等于N+1。最直接也最清晰的做法就是排序然后两边往中间同步走,O(nlogn) + O(n)。 这是标准做法,如果有序序列,更好,只用O(n)就行不过既然相同数值不重复出现,还对空间没啥说的,那就bitmap吧。。。可以做到O(n)。太无聊了,还要揣摩出题人的思路。2,一个整数数列,元素取值可能是0~655
阅读全文
摘要:1.给出一个有N个数字(-1000..1000,N<=10^5)的环状序列,求一个和最大的连续子序列。http://www.cppblog.com/baby-fly/archive/2010/08/04/122213.aspx?Pending=true来源于单调队列的应用#include<iostream>#include<queue>using namespace std;#define INF 0x3fffffff#define maxn 100010int num[maxn],sum[maxn];int main(){ int T; int N,K,n; c
阅读全文
摘要:来源于:http://blog.csdn.net/lin_bei/article/details/15655751. 中位数,也是利用2分查找,去掉一半的算法,只是这个 中位数 。。。。总是求的是最小的那个,而不是平均值。。。Gray 码,使用递归分治求GRAY码硬币找零问题#include <iostream>using namespace std;int findMedian(int *a,int *b,int n){ if(n == 1) return *a <= *b ? *a: *b; int m = (n - 1) >> 1; int p = m +
阅读全文
摘要:来自:http://www.cnblogs.com/hlxs/archive/2011/08/24/2151828.html 1. 数组相关 在排序找出数组中的两个数,他们之和为n 2. 数组中连续的数字之和为n的所有对数 3. 输出所以字符串的排列与组合 4. 序列化分问题1. 数组相关 在排序找出数组中的两个数,他们之和为n,当然如果数组不是有序的,先排序在找时间复杂度O(nlogn + n),更简单的当然还是hash或者如果元素不重复的话位图,bloom filter更好#include <iostream>using namespace std;//在排序找出数组中...
阅读全文
摘要:0 整理自网络1. Java版(转) 要比较的对象必须实现Comparable接口,重写compareTo 方法,Java在红自带有优先级队列的实现PriorityQueueQueue<ToDoItem> q = new PriorityQueue<ToDoItem>();import java.util.Collections;import java.util.PriorityQueue;import java.util.Queue;public class ToDoItem implements Comparable<ToDoItem>{ private
阅读全文
摘要:属于转贴一、 Bloom-Filter算法简介。 Bloom-Filter,即布隆过滤器,1970年由Bloom中提出。它可以用于检索一个元素是否在一个集合中,其优点是空间效率和查询时间都远远超过其他算法,其不足在于Bloom- Filter存在着误判。二、 Bloom-Filter的基本思想。 Bloom-Filter算法的核心思想就是利用多个不同的Hash函数来解决“冲突”。 计算某元素x是否在一个集合中,首先能想到的方法就是将所有的已知元素保存起来构成一个集合R,然后用元素x跟这些R中的元素一一比较来判断是否存在于集合R中;我们可以采用链表等数据结构来实现。但是,随着集合R中元素的增加,
阅读全文
摘要:1. 反转字符串。#include <iostream>using namespace std;/** 编写反转字符串的程序,要求优化速度、优化空间。*/void reverse(char *str,int len){ char *p = str; char *q = str + len - 1; len /= 2; while(len > 0) { *p = *p ^ *q; *q = *p ^ *q; *p = *p ^ *q; p ++; q --; len --; }...
阅读全文
摘要:各种排序算法实现的收集,肯定有你没有见过的。归并,快排,奇偶排,cocktail 排序,梳排序,计数排序,基数排序,LSD基数排序,shell排序,桶排序,鸽巢排序等1. 归并排序的实现#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>using namespace std;void merage(int a[],int low,int mid,int high){ int *temp = (int *)malloc((high - low + 1
阅读全文
摘要:1. 100万数据的产生,随机数方式#include <iostream>#include <time.h>#include <assert.h>#include <stdio.h>#include <stdlib.h>using namespace std;const int size = 10000000;int num[size];int main(){ int n; FILE *fp = fopen("data.txt", "w"); assert(fp); for (n = 1; n
阅读全文
摘要:第一节、一道简单的虚函数的面试题题目要求:写出下面程序的运行结果? 1、当上述程序中的函数p()不是虚函数,那么程序的运行结果是如何?即如下代码所示: class A{public: void p() { cout << "A" << endl; }}; class B : public A{public: void p() { cout << "B" << endl; }}; 没有虚函数,就不是...
阅读全文
摘要:1. 模板模板分类模板和函数模板,其中函数模板是一个通用的函数,其参数类型和返回类型不具体指定,类模板类似,建立一个通用类,其数据成员的类型、成员函数的返回类型和参数类型都不具体指定,用一个虚拟类型来代表。 1 #include <iostream> 2 3 using namespace std; 4 //函数模板 5 template <class T> 6 T cmin(T m,T n) 7 { 8 return ( m<n) ? m : n; 9 }10 int main()11 {12 int n1 = 2;13 int n2 = 10;14 doubl
阅读全文
摘要:1 链表#include <stdio.h>#include <malloc.h>typedef struct node{ int data; struct node * lchild; struct node * rchild;}node;void Init(node *t){ t = NULL;}node * Insert(node *t , int key){ if(t == NULL) { node * p; p = (node *)malloc(sizeof(node)); p->data = key; p->lchild = NULL; p-&g
阅读全文
摘要:在转自:http://www.cnblogs.com/vongang/ stack//数组实现#include <stdio.h>#include <malloc.h>#define maxsize 10typedef struct{ int data[maxsize]; int top; int base;}seqstack;seqstack * s;void initstack(seqstack * s){ s->top = 0; s->base = 0;}int empty(seqstack * s){ if(s->top == s->ba
阅读全文
摘要:转自:http://www.iteye.com/topic/692103原理:将Cache的所有位置都用双连表连接起来,当一个位置被命中之后,就将通过调整链表的指向,将该位置调整到链表头的位置,新加入的Cache直接加到链表头中。这样,在多次进行Cache操作后,最近被命中的,就会被向链表头方向移动,而没有命中的,而想链表后面移动,链表尾则表示最近最少使用的Cache。当需要替换内容时候,链表的最后位置就是最少被命中的位置,我们只需要淘汰链表最后的部分即可。 帅!import java.util.Hashtable;/* * 来源于: http://www.iteye.com/topic/69
阅读全文
摘要:前言:前些天问了问了一道题:http://home.cnblogs.com/q/27511/结果网上的评价 都还是理解有点不对,这里记录下我试验的结果以及解决方案1.原题如下:问题是当p离开作用域时,不是应该p变成野指针么,为什么test函数输出依然正确?#include <iostream>using namespace std;class A{ public: virtual void func(){ cout << "A func()" << endl;}};void test(){ A* p; { A a; p = &a;
阅读全文
摘要:0.最近在复习C++,好多东西都忘了 ==!!从博客中,书中看到的一些东西,在这里简单总结下,没有章法,看到哪,复习到哪1. 引用 C++中引用 具备了 指针的 所有功能 区别: (1) 引用在定义时必须初始化.引用和变量共享同一块内存空间,而指针单独有内存空间 (2) 指针进行删除后,一般需要将其指向NULL,防止野指针,而引用至始而终都是它初始化时的地址,而且也不用删除,它会在作用域范围外由系统回收引用和它引用的变量指向的是同一块内存空间 当修改其中任意一个值时,两个值都改变,当对引用重新赋一个新值时,引用的值和原来引用指向的值都改变为这个新值,而引用地址不变在C ++ 中还可以定义一个类
阅读全文
摘要:1. map功能:自动建立Key - value的对应。key 和 value可以是任意你需要的类型。根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。快速插入Key - Value 记录。快速删除记录根据Key 修改value记录。遍历所有记录。2. 使用 #include <map> //注意,STL头文件没有扩展名.h 插入元素:两种方式 (1) enumMap[1] = "One"; enumMap[2] = "Two"; 但存在一个性能的问题。插
阅读全文