- setfill
- 进制转换
- 默认参数
- 函数重载【杜绝重载的函数有默认参数,避免调用二义性的发生】
- 新特性 C++引用& 【C语言“&”取地址。C++中“&”只是起到标识的作用。】
- 内存堆分配alloc_meorry new/delete
在C语言中,动态分配内存用
malloc()
函数,释放内存用free()
函数。在C++中,这两个函数仍然可以使用,但是C++又新增了两个关键字,new
和delete:new
用来动态分配内存,delete
用来释放内存。使用new
和delete
会比函数malloc()
和free()
更好,realloc()
函数可以用vector()
代替。
-
cout<<endl;//相当于一个whitespace
setfill
#include <iostream>
#include <iomanip>
//using namespace std;
int main(){
double num = 3.14159;
std::cout << std::setprecision(2) << num << std::endl;
std::cout<<setiosflags(std::ios::fixed)<<num<<std::endl;
std::cout<<std::setw(4)<<2<<std::setw(10)<<1020<<std::endl;
std::cout<<std::setw(4)<<202<<std::setw(10)<<10<<std::endl;
//左对齐
std::cout<<setiosflags(std::ios::left);
std::cout<<std::setw(4)<<2<<std::setw(10)<<1020<<std::endl;
std::cout<<std::setw(4)<<202<<std::setw(10)<<10<<std::endl;
//重新右对齐
std::cout<<resetiosflags(std::ios::left);//这是left
std::cout<<std::setw(4)<<2<<std::setw(10)<<1020<<std::endl;
std::cout<<std::setw(4)<<202<<std::setw(10)<<10<<std::endl;
//setfill 填充
std::cout<<std::setw(4)<<std::setfill('-')<<202<<std::setw(10)<<10<<std::endl;
return 0;
}
进制转换
#include <iostream>
using namespace std;
int main(){
int a=12;
/*进制转换 decimal conversion*/
cout<<"oct="<<oct<<a<<endl; // oct=14
cout<<"dec="<<dec<<a<<endl; // dec=12
cout<<"hex="<<hex<<a<<endl; // hex=c
return 0;
}
默认参数 default_para
#include <iostream>
using namespace std;
void add(int x,int y=1,int z=2){
cout<<x+y+z<<endl;
}//可以把参数默认值设置为 0,这样可以求1,2,3个数的和
int main(){
add(1);//1+1+2=4
add(1,2);//1+2+2=5
add(1,2,3);//1+2+3=6
return 0;
}
函数重载overload
#include <iostream>
using namespace std;
void add(int x,int y){
cout<<"int:"<<x+y<<endl;
}
void add(double x,double y){
cout<<"double:"<<x+y<<endl;
}
int main(){
add(1,2);//int:3
add(10.0,2.5);//double:12.5
return 0;
}
引用&
引用是隐式的指针 但2者有着本质的区别
- 指针要占据额外内存,引用不用。
- 实参不同,指针的实参是变量的地址,而引用的是变量本身,但系统向引用传递的是变量的地址而不是变量的值。
#include <iostream>
using namespace std;
int main(){
int a=10;
int& ra=a;
cout<<"a="<<a<<endl; //a=10
cout<<"ra="<<ra<<endl; //ra=10
//引用和变量的值相同
cout<<"变量a的地址 "<<&a<<endl; //变量a的地址 0x78fe14
cout<<"引用ra的地址"<<&a<<endl; //引用ra的地址0x78fe14
//引用和变量的地址相同
/*cout<<"变量a的地址 "<<hex<<&a<<endl;
cout<<"引用ra的地址"<<hex<<&a<<endl;*/
return 0;
}
& 交换2个数的值
#include <iostream>
using namespace std;
void exchange(int& x,int& y){
int temp=x;
x=y;y=temp;
}
int main(){
int a,b;
cout<<"please input 2 nums ";
//please input 2 nums 1 34
cin>>a>>b;
cout<<"exchange before:"<<a<<" "<<b<<endl;
//exchange before:1 34
exchange(a,b);
cout<<"exchange after :"<<a<<" "<<b<<endl;
//exchange after :34 1
return 0;
}
内存堆分配
#include <iostream>
using namespace std;
int main(){
int* pi=new int(10);
cout<<"pi="<<*pi<<endl;
*pi=20;
cout<<"*pi="<<*pi<<endl;
char* pc=new char[10];
for(int i=0;i<10;i++)
pc[i]=i+65;
for(int i=0;i<10;i++)
cout<<pc[i]<<" ";
cout<<endl;//相当于一个whitespace
delete pi;//用完指针之后,一定要将其删掉。这样可以杜绝野指针的存在
delete []pc;
cout<<"pi="<<*pi<<endl;//野指针 pi=13532512(会变)
pi=NULL;//删除指针之后,一定要加上下面这句话,免得成为野指针
cout<<"pi="<<*pi<<endl;//pi=
return 0;
}