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 (*p)[4] = b; // caution! //()
===========================
? :条件运算符
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 友元成员函数 编译报错,但是可以运行(但暂未证实是否运行正确)。为什么啊?