列举一些ReadLine的键绑定,凡是使用Readline控件的程序中都可以使用这些快捷键,比如bash、lftp、gdb等。首先,做一下约定:

  • \C-a 表示 Ctrl+a
  • \M-a 表示 Meta+a Meta键 在 PC 中通常为 ALT键或 ESC键
Tags: .

POJ 1028:http://acm.pku.edu.cn/JudgeOnline/problem?id=1028
模拟浏览器前进、后退、访问到某个页面的过程。又是一道水题,我什么时候才能做到水王之王的位置?

Tags: ,.

ZOJ上的一道题(illusive chase)让我憋了一整天才AC了,而且用了224行,最后优化到115行!我真真是没救了,也只能做做小儿科的题目了!sucks……这里有上次百度之星的一道水题……

题目描述:

一个正整数有可能可以被表示为n(n>=2)个连续正整数之和,如:

15=1+2+3+4+5
15=4+5+6
15=7+8
Tags: ,.

由于归并排序需要O(n)的额外空间,在普通函数中,我实在想不出好的办法来申请这块内存。于是只好把他写成一个模板类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <class T>
class Merger
{
public:
	Merger(T* array, size_t n):extra(new T[n/2]) //~ 归并需要O(n)的额外空间
	{
		sub_Merger(array, n); //~ 调用递归子函数
	}
	~Merger()
	{
		delete [] extra; //~ 释放内存
	}
private:
	T* extra;
	Merger();
	void sub_Merger(T* array, size_t n);
};

如果你有什么好的建议的话,敬请赐教,不胜感激涕零!:)

Tags: ,.
char ** s;
s是一个指针1,指向一个指针2, 指针2指向char

const char ** s;
s是一个指针1,指向一个指针2,指针2指向char,char是常数

char * const * s;
s是一个指针1,指向一个常量1,常量1是个指针2,指针2指向char

const char * const * s;
s是一个指针1,指向一个常量1,常量1是个指针2,指针2指向char,char是常数

const char * const * const s;
s是一个常量1,常量1是一个指针1,指针1指向一个常量2,常量2是个指针2,指针2指向char,char是常数

最后,感谢Felix的精彩解析!

Tags: ,.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
//~ #include <string>
 
using namespace std;
 
class demo
{
public:
	void* operator new(unsigned int size) //~ operator new must return "void *"
	{
		//~ do something like the following
		demo* ptr = ::new demo[size]; 
		return ptr;
	}
};
int 
main()
{
	demo * ptr = new demo[9];
	return 0;
}
Tags: ,.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
 
template <class T>
class Base
{
public:
	void set(int n){m_data = n;}
	int get(){return m_data;}
private:
	int static m_data;
};
template <class T>
int Base<T>::m_data = 0;
 
class Derived1: public Base<Derived1>{};
class Derived2: public Base<Derived2>{};
int 
main()
{
	Derived1 D1;
	Derived2 D2;
	D1.set(1);
	D2.set(2);
	cout<<D1.get()<<ends<<D2.get()<<endl;//~ 输出1 2
	return 0;
}

其实,这只是一个小把戏,用不同类型实例化后的类完全是两个类型了,当然会拥有不同的静态成员!

Tags: ,,.
1
2
3
4
5
6
7
8
9
10
11
int 
main()
{
	S * pS = new S;
	D1 * pD1 = pS;
	D2 * pD2 = pS;
	pS->foo(); //~ OK, 存在一条访问路径B->D1->S
	pD1->foo();
	//pD2->foo(); //~ 'B::foo' : no accessible path to private member declared in virtual base 'D2'
	return 0;
}
Tags: .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
//~ #include <string>
 
using namespace std;
 
class Base
{
public:
	virtual void foo()
	{
		cout<<"Base::foo()"<<endl;
	}
};
 
class Derived: public Base
{
private:
	void foo()
	{
		cout<<"Derived::foo()"<<endl;
	}
};
 
int 
main()
{
	Derived  * pD = new Derived;
	Base * pB = pD;
	pB->foo();				//~ OK, call Derived::foo()
	//~ pD->foo();				//~ oops, Derived::foo() is private!
	return 0;
}

可见,权限检查发生在名字查找之时,而虚函数表的访问不带有权限检查。

Tags: ,.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
//~ #include <sstream>
using namespace std;
 
int main()
{
	typedef vector<int> int_vector;
	typedef istream_iterator<int> istream_itr;
	typedef ostream_iterator<int> ostream_itr;
	typedef back_insert_iterator<vector<int> > back_ins_itr;
	//~ vector
	int_vector ivec;
	//~ 从标准输入读整数到vector容器
	copy(istream_itr(cin), istream_itr(), back_ins_itr(ivec));
	//~ 排序
	sort(ivec.begin(), ivec.end());
	//~ 输出到标准输出
	copy(ivec.begin(), ivec.end(), ostream_itr(cout, "\n"));
	return 0;
}
Tags: ,.