GOOD DAY

天天向上<< High一下!

读书笔记

01 Apr 2015 - NanJing


jsj

C++ Prime

###数据类型###

	int &refVal3 = 10; // error: initializer must be an object  
	int i = 42;
	// legal for const references only  
	const int &r = 42;  
	const int &r2 = r + i;  
	double dval = 3.14;
	const int &ri = dval; 
	// point2d is 2, point2w is 3, point3d is 3, point3w is 4  
	enum Points { point2d = 2, point2w,	point3d = 3, point3w }; 
	extern int ival = 10; // initializer, so it's a definition  
	double fica_rate; // no extern, so it's a definition 

对于头文件不应该含有定义这一规则,有三个例外。头文件可以定义类、值在编译时就已知道的 const 对象和 inline 函数(第 7.6 节介绍 inline 函数)。这些实体可在多个源文件中定义,只要每个源文件中的定义是相同的。

###标准库(类模板)###

  
	int main()
	 {
		 string line;
		 // read line at time until end-of-file
		 while (getline(cin, line))
		 cout << line << endl;
		 return 0;
	 }
	//string支持下标操作
	string str("some string");
	for (string::size_type ix = 0; ix != str.size(); ++ix)
		cout << str[ix] << endl; 
  
    for(vector<int>::iterator it=arr.begin(); it!=arr.end(); )
    {
        if(* it == 8)
        {
            it = arr.erase(it);
        }
        else
        {
            ++it;
        }
    }

	//注意上面不能写成
    /*
        for(vector<int>::iterator it=arr.begin(); it!=arr.end(); it ++)
        {
            if(* it == 8)
            {
                arr.erase(it);     //在erase后,it失效,并不是指向vector的下一个元素,it成了一个“野指针”。
            }
        }
	*/
  
	for (vector<string>::const_iterator iter = text.begin();
	 iter != text.end(); ++ iter)
		*iter = " "; // error: *iter is const

	vector<int> nums(10); // nums is nonconst
	const vector<int>::iterator cit = nums.begin();
	*cit = 1; // ok: cit can change its underlying element
	++cit; // error: can't change the value of cit 
 
	bitset<32> bitvec; // 32 bits, all zero
	// bitvec1 is smaller than the initializer
	bitset<16> bitvec1(0xffff); // bits 0 ... 15 are set to 1
	// bitvec2 same size as initializer
	bitset<32> bitvec2(0xffff); // bits 0 ... 15 are set to 1; 16 ... 31 are 0
	// on a 32-bit machine, bits 0 to 31 initialized from 0xffff
	bitset<128> bitvec3(0xffff); // bits 32 through 127 initialized to zero 
	string str("1111111000000011001101");
	bitset<32> bitvec5(str, 5, 4); // 4 bits starting at str[5], 1100
	bitset<32> bitvec6(str, str.size() - 4); // use last 4 characters 

string 对象和 bitsets 对象之间是反向转化的:string 对象的最右边字符(即下标最大的那个字符)用来初始化 bitset 对象的低阶位(即下标为 0 的位)。当用 string 对象初始化 bitset 对象时,记住这一差别很重要。

###数组和指针###

###表达式###

###语句###

###函数###

 
 // Disaster: Function returns a reference to a local object
 const string &manip(const string& s)
 {
 string ret = s;
 // transform ret in some way
 return ret; // Wrong: Returning reference to a local object!
 }

这个函数会在运行时出错,因为它返回了局部对象的引用。当函数执行完毕,字符串 ret 占用的储存空间被释放,函数返回值指向了对于这个程序来说不再有效的内存空间。确保返回引用安全的一个好方法是:请自问,这个引用指向哪个在此之前存在的对象?

###序列容器###

###关联容器###

###泛型算法###

###类和数据抽象###

 
	Screen& Screen::move(index r, index c)
	{	
		index row = r * width; // row location
		cursor = row + c;
		return *this;
	}
 
	class Bar {
	public:
	// ...
	private:
	static Bar mem1; // ok
	Bar *mem2; // ok
	Bar mem3; // error
	}; 

###面对对象编程###

 
	Item_base *baseP = &derived;
	// calls version from the base class regardless of the dynamic type of baseP
	double d = baseP->Item_base::net_price(42); 

Effective c++

###让自己习惯C++###

###构造、析构、复制运算###

###资源管理###

###构造函数初始化列表###

###c++多线程总结### http://www.cnblogs.com/zhiranok/archive/2012/05/13/cpp_multi_thread.html

###c++注意事项###

###单例模式### 只维持一个实例。
适用场景:

###类型提升### long double,double, float,unsigned long int,long int,unsigned int, int(最低),向高处转型

###STL###

vector\set\map\istringstream\ostringstream stl应用

###堆排序### 堆排序