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;
}
&
引用 #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
汇编查看: 传入被调函数栈中参数为实参地址;
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;
}
#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
实现:编译过程优化
#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)>
#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
汇编内联 可以避免栈的压入与推出
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