C++primer reading notes

  1. 假定$是操作系统提示符,可通过“ $ addItems outfile ”使得已经编译的名为addItems.exe 的可执行文件调用infile文件中的数据,并将输出结果写入名为outfile的文件中(两个文件都位于当前目录中)。

  2. 因为引用不是对象,因此不能定义指向引用的指针 、引用的数组、引用的引用。

	int a = 9;
	int& b = a;
	int* c = &b;//此处实际上仍是对a操作
	//下面都为错误示范
	//int&* d = &b;此才为指向引用的指针
	//int& e[3];
	//int& &f;
	int* &g = c;//指针的引用
  1. void指针
    void
    指针可以存放任意对象的地址,因为其所指对象类型的不确定性,所以void*指针不能访问内存空间所存的对象。
    int a = 0;
	double c = 0.0001;
	void* b = &a;
	double* d = &c;
	b = d;
	cout << b <<"    "<< &c<<endl;
	//*b错误
  1. 阅读较复杂的声明语句时,从右向左阅读有利于理解。
	int a = 1;
	const int b = a;
	int* c = &a;//指向a的指针
	int** d = &c;//指向c指针的指针,即二级指针。
	int*& e = c;//c指针的引用。
	int const* f = &a;//==const int指针常量,不能改变值
	int* const g = &a;//常量指针,不能改变指向对象。
	const int* const h = &a;
  1. 类型别名。
    typedef or using。
    using G =Game;(Game 为类名)
typedef int in, * ini;
typedef const in const *inin;//==int const *。仅为指针常量,不能限定指针的取址。
int main() {
	in a = 9,c=1;
	ini b = &a;
	inin d = &c;//d为const int *类型
	b = &c;
	*b = 5;
	d = &a;
	//*d = 5;错误
  1. 范围for(range for)语句
    for (declaration : expression)
    statement
	int a[4];
	//auto 可改为int,要求是前后一致
	for (auto& val : a)
	{
		cout << "Enter an integer value: ";
		cin >> val;
	}
	for (auto c : a) {
		char b = c;
		if (b < 'm')
			cout << c << endl;
	}
  1. 迭代器与标准库函数begin和end
string s1("hello");
	int a[] = { 10,1,2,3,4 };
	auto n = s1.end();//尾后迭代器
	auto m = begin(a);
	auto b = a;//b为指向数组a的指针
  1. 用指针指向用string类型初始化的数组 & 用数组初始化vector对象
	int a[] = { 0,1,2,3,4,5 };
	string s1("hello world");
	vector<int>v1(a+1,a+4);//范围为a[1]~a[3]。
	vector<int>v2(begin(a), end(a));
	const char* str = s1.c_str();//用string对象初始化char*。
	cout << *(str+6) << endl;//输出w
	cout << v1[2] << endl;//输出3
	cout << v2[3] << endl;//输出3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值