c++课程总结----第零章

本文介绍了C++编程中的关键概念,如内存分配与释放、函数的默认参数、重载、引用、作用域运算符、优先队列以及命名空间的使用,是C++初学者的实用参考.
摘要由CSDN通过智能技术生成
#include <bits/stdc++.h>//+Ag
using namespace std;
#define qio ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
typedef long long ll;

int main()
{
	/*输出格式控制*/
	double y = 0.002365546;
	cout << setw(8) << setiosflags(ios::left)<< setfill('c')<< 100 << endl;
	//setw(n) 设置n位空间 setfill(n) 填满n 
	cout << setprecision(2) << y << endl;
	//控制会四舍五入 保留两位 第三位四舍五入 
	
	cpp动态内存管理
	
	
	/*new 申请空间*/
	int *p;
	p=new int[10];
	p=new int(10);//申请1个int 赋值为10 
	
	
	/*delete 释放空间*/
	cout << *p << endl;
	
	/*string输入*/
	string a;
//	cin >> a;//不可输入空格  
	getline(cin,a);//可输入空格
	char b[10];
	cin.getline(b,sizeof(b));//输入到数组里面 要告示大小 
	cout << a << endl;
	cout << b;
	
	
	/*函数的默认声明*/
//	void Fun(int i, int j = 5, int k = 10) 
	//定义具有默认参数值的函数Fun
	//要把默认声明放在声明里面 即具有默认值的参数必须全部集中在参数表的最右端。
//	void d (int i,int k=0,int j = 1)
//	d(1);
	
	
	
	/*函数重载*/
	 int sq(int x) return x*x;
	 float sq(int x) return x*x;
	 double sq(int x=1.5) return x*x;
	cout << sq();//会调用double 因为赋值了 
	cout << sq(10);
	cout << sq(2.05f);//为float类型 
	cout << sq(1.1);//计算机默认double类型 
	 //重载依靠形式参数的个数 类型 顺序 
	
	/*引用*/
	int x = 3;
	int &y = x; 
	y = 10;//等于x=10
	//诺不允许引用改变目标变量 const int &y = x
	
	/*把参数变成引用 有点像指针*/
	int a = 10,b = 20;
	void swa(int &x,int &y){
		int r;
		r=x;
		x=y;
		y=r;
	}
	swa(a,b);//可以改变a和b的值 
	
	/*引用作为函数返回值*/
//	正常下返回值有在main函数中分配临时单元 返回值定义成引用就不会 
	int & fun(const int &x,int &y,int z){
		z++;
		y=x+y;
		return y;
	}
	 int &fun(int &x,int &y,int z){
	y=x+y+z;
	x++;
	return y;
	}
	int main(){
		int a,b,c;
		cin >> a >> b >> c;
		int d;
		d=fun(a,b,c)*2;
//		还可以->
		fun(a,b,c)=20; //放回了y的引用,将20赋值给了b 
		cout << a << b << c << d;
		return 0;
	}
//	注意 return 要是一个全局变量或者引用 不能是局部变量(返回后也要存在) 
	
	
	
	
	/*作用域运算符::*/
	int i=10;
	void fun(){
	int i=20;
	cout<<"局部变量"<<i;
	cout<<"全局变量"<<::i;
	}
	
	/*优先队列加结构体*/
	struct e{
		int next;
		int to;
		int cost;
		bool operator <(const e &a) const{
			return a.cost<cost;
		}
	}; 
	
	vector<int> a(10)//开10个空间 
	vector<int> a[10]//开十个vector数组
	for(auto &i : a){
		cin >> i;
		
	} 
	/*命名空间*/
	namespace my{//定义一个命名空间
		namespace you{
			int max(int a,int b){
			return a;
			}
		}
	}
	int main()
	{
		int x = 20, y = 10;
		int z;
		z=my::you::max(x,y);//先找到箱子 
	}
//	命名空间第一种方法
	 //先声明
	 #include <bits/stdc++.h>
	 using namespace std;
	 namespace my{//定义一个命名空间
		namespace you{
			int max(int a,int b){
			return a;
			}
		}
	 }
	 using namespace my::you;//!!!
	 int main()
	 {
		int x = 20, y = 10;
		int z;
		z=max(x,y);//可以直接使用箱子了 
	 }
	
	
	
	
	/*内联函数*/
	inline //没讲懂
	
	
	
	return 0;
}

(学c++的预备知识 上课笔记)

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值