C++ primer 笔记


ch2

decltype 和 auto 的区别 

/* decltype 保留顶层const, auto 不保留(except,引用时保留)。
*/   
    const int a = 4;
	const int ic = 8;
	auto b = ic + a;
	decltype (ic) c = ic + a;
	b++;
	c++; // error: c is  const int
/* 如果decltype使用的表达式不是一个变量,decltype返回表达式结果对应类型
** 赋值表达式的返回结果类型为左值类型的引用
*/
    int a = 4;
	int i = 8;
	auto b = a;
	decltype (a = i) c = a;
	b++;
	std::cout << "a" << a 
	        << "\tb" << b
			<< "\tc" << c << std::endl;
	c++;
	std::cout << "a" << a
		<< "\tb" << b
		<< "\tc" << c << std::endl;




ch4 


int b[4][4] = { { 1,2,3,5 } };
int (*p)[4] = b;  //  caution!  //()
 
简洁是一种美德:
cout << *pbeg++ << endl;

===========================

?  :条件运算符

lettergrade = (grade == 100) ? "A++" : (grade >= 90) ? "A" :
(grade >= 80) ? "B" : (grade >= 70) ? "C" :
(grade >= 60) ? "D" : "F";



ch5

do{


}while();     //caution   ;


========

异常引发用 throw 即使在try 中  // 异常处理这里还不太会 



ch6

main(int argc, char *argv[])   //main 函数的第二个形参是什么鬼?! 等价于char**, 数组的指针。应该是 (*argv)[] 吧?

                                          //string 初始化可以用char* 

#include 
  
  
   
   
#include 
   
   
    
    
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main(int argc, char *argv[])
{
	string str;
	str = string(argv[1])+""+ string(argv[2]); // string initialization by char*
	cout << str <
    
    
   
   
  
  


利用迭代器传递vector,注意递归结束条件。

using Iter = vector
    
    
     
     ::const_iterator;
void print(Iter first, Iter last)      //caution: the stop condition for the recursion
{
    if (first != last)
    {
        cout << *first << " ";
        print(++first, last);
    }
}
    
    


默认实参 : 在函数声明中指定(最好放在头文件中),在函数定义中不需要也不允许再次标注。

string make_plural(size_t ctr, const string &word, const string &ending = "s");


string make_plural(size_t ctr, const string &word, const string &ending)
{
return (ctr > 1) ? word + ending : word;
}

ch7

ex7_32.cpp 友元成员函数 编译报错,但是可以运行(但暂未证实是否运行正确)。为什么啊?


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值