c补丁(c++)
时间:2020-12-05 17:45:24
收藏:0
阅读:29
c++补丁
1.基本输入输出
std::cout << ‘‘ const str "<< std::endl
#include <iostream>
using namesapce std;
int main(void){
cout<<"holle word!"<<endl;
return 0;
}
std::cin >> objectName
#include <iostream>
using namesapce std;
int main(void){
int agg ;
cin >> agg;
return 0;
}
2.&
引用
#include <iostream>
using namesapce std;
void swap(int &a,int &b)
{
int c;
c = a;
a = b;
b = c ;
}
int main(void)
{
int a =1, b=2;
cout<<a<<‘\t‘<<b<<‘\n‘;
swap(a,b);
cout<<a<<‘\t‘<<b<<‘\n‘;
return 0;
}
result:
1 2
2 1
汇编查看: 传入被调函数栈中参数为实参地址;
3.const
- 作为常量
- 作为引用形参 - 显式保护压入被调函数栈的地址下的数据。
#include <iostream>
using namespace std;
typedef struct STUDENT
{
char name[10];
int id;
}Std;
// 传入引用是为了减少栈的压入与推出
void StdOut(const Std &a){ // (编译检查)确保传入参数不会被修改
cout<<a.id;
cout<<"\n";
cout<<a.name;
cout<<"\n";
}
int main(void)
{
Std a ={"xiaoMing",100};
StdOut(a);
return 0;
}
- 符号常量指针 -- 无法修改地址下的数据
- 符号指针常量 -- 固定地址
4.默认值函数
#include <iostream>
using namespace std;
int max(int a, int b)
{
int max = a;
if (b > a)
{
max = b;
}
return max;
}
int main(void)
{
int max(int a =1,int b=2);
cout<<max()<<‘\n‘;
return 0;
}
result:
2
实现:编译过程优化
5.函数重载
#include <iostream>
using namespace std;
int myMax(int a, int b)
{
return a > b ? a : b;
}
float myMax(float a, float b)
{
return a > b ? a : b;
}
int main(void)
{
cout << myMax(1, 2) << ‘\n‘;
cout << myMax((float)1,(float)2) << ‘\n‘;
return 0;
}
reslut:
4
8
callq 0x555555555209 <myMax(int, int)>
callq 0x5555555551e9 <myMax(float, float)>
6.函数内联(?优化等级?)
#include <iostream>
using namespace std;
inline int max(int &a, int &b)
{
return a>b?a:b;
}
int main(void)
{
int a =1,b=2;
cout << max(a,b) << ‘\n‘;
return 0;
}
reslut:
2
汇编内联 可以避免栈的压入与推出
7.内存的动态分配与释放
typedef struct MYSTRING
{
char mychar;
MYSTRING *next;
}Mystr;
int main(void)
{
Mystr *head = new Mystr;
Mystr *one = new Mystr;
head->next=one;
delete head;
if(head->next!=one){
cout<<"delete"<<"\n";
}
else
{
cout<<"not delete"<<"\n";
}
return 0;
}
reslut:
delete
原文:https://www.cnblogs.com/haoge2000/p/14089920.html
评论(0)