一、賦值運算符
ID:變量名=表達式
1.先計算右端表達式,再把值賦給左端變量
2.x=3*5/(2+8),x=7
3.右端轉換爲左端定義的變量類型(int x=10/3.0)
4.x=y=z=5
5.x+=5(等價x=x+5)
6.x+5=y(非法)
7.(x+=5)*=2 複合賦值
8.+=,-+,*=,/=,%=
二、算數運算符
ID:++/--;+;-;*;/;%
CPP:
#include <iostream>
using namespace std;
void main()
{ int x=10,y=4;
cout<<"x="<<x<<" "<<"x++="<<x++<<endl;
x=10;
cout<<"x="<<x<<" "<<"x--="<<x--<<endl;
x=10;
cout<<"x="<<x<<" "<<"--x="<<--x<<endl;
x=10;
cout<<"x="<<x<<" "<<"++x="<<++x<<endl;
x=10;
cout<<"x+y="<<x+y<<endl;
cout<<"x-y="<<x-y<<endl;
cout<<"x*y="<<x*y<<endl;
cout<<"x/y="<<x/y<<endl;
cout<<"x%y="<<x%y<<endl; }
Results:
x=11 x++=10
x=9 x--=10
x=9 --x=9
x=11 ++x=11
x+y=14
x-y=6
x*y=40
x/y=2
x%y=2
說明:
1.x++/x--/++x/--x都看成是表達式的值,遇到++/--在前則表達式先+1/-1,x值發生變化,遇到++/--在後則先表達式值保持不變,x值發生變化。
2."除"因爲是整形(int),故只能顯示實數部分;若想獲得精確值可以通過數據類型轉換((float)x/y),結果爲2.5
3.取模,非常有用(如控制格式輸出每行5個數值:if(i%5=0)cout<<endl;)
三、關係運算符
ID:<,<=,>,>=,==,!=
1.運算結果爲true或false
四、邏輯運算符
ID:!,&&,||
1.!單目運算—邏輯反
2.&&,||,運算兩邊分量的值—邏輯運算結果(且,或)
五、條件運算符
ID:表達式1?表達式2:表達式3
1.x=a>b?a:b //若a>b爲真,則x=a,否則x=b
cpp:#include <iostream>
using namespace std;
void main(){
int a,b, max;
cin>>a>>b;
max=a>b?a:b;
cout<<"Max="<<max<<endl;}
六、逗號運算符
ID:表達式1,表達式2,表達式3....表達式N
int x=5,y;
y=(t=x+5,t=t*t,t);//t1=10,t2=100,t3=100,則y=100
七、sizeof運算符
ID:sizeof()
1.sizeof(char),sizeof(int)..
2.int a;double b;
sizeof(a),sizeof(b)..
cpp:#include <iostream>
using namespace std;
void main(){
char ch;
cout<<"please input int=i,char=c,float=f,double=d?"<<endl;
cin>>ch;
switch(ch)
{case 'i':
cout<<"you want to the size of int="<<sizeof(int)<<endl;
break;
case 'c':
cout<<"you want to the size of char="<<sizeof(char)<<endl;
break;
case 'f':
cout<<"you want to the size of float="<<sizeof(float)<<endl;
break;
case 'd':
cout<<"you want to the size of double="<<sizeof(double)<<endl;
break;
default:
cout<<"Not in area!"<<endl;
break;}
八、圓括號運算符
ID:xx()
1.函數調用;sqrt(2),strcpy(d,s),myfunction(a,b)
cpp:
/*using a function template,模板其實就是具有相同參數個數的一組函數的抽象,如果參數個數不一,可以使用函數重載(overloading function),函數是兩者的基本單元*/
#include <iostream>
using namespace std;
//*************************************
template <class T>
T maximum(T value1,T value2,T value3)
{
T Max=value1;
if(value2>Max)Max=value2;
if(value3>Max)Max=value3;
return Max;
}
//**************定義模板*******************************
int main()
{
int int1,int2,int3;
cout<<"Enter three integer value:"<<endl;
cin>>int1>>int2>>int3;
cout<<"The Max value is:"<<maximum(int1,int2,int3)<<endl; //模板(副本)調用,返回最大值
double double1,double2,double3;
cout<<"Enter three double value:"<<endl;
cin>>double1>>double2>>double3;
cout<<"The Max value is:"<<maximum(double1,double2,double3)<<endl; //模板(副本)調用,返回最大值
char char1,char2,char3;
cout<<"Enter three character value:"<<endl;
cin>>char1>>char2>>char3;
cout<<"The Max value is:"<<maximum(char1,char2,char3)<<endl; //模板(副本)調用,返回最大值
return 0;
}
2.強制類型轉換(動態);int a;double(a)或(double)a;,char(65),double(8)
3.一元強制性類型轉換(靜態):static_case<type>()
九、數組下標運算符
ID:[]
十、指針運算符
ID:*(取地址)和&(引用傳遞)
十一、動態存儲分配運算符
ID:new,delete
十二、作用域限定運算符
ID: ::(一元作用域)
cpp:
#include <iostream>
using namespace std;
const double pi=3.1415926535;
int main()
{
const float pi=static_cast <float>(::pi)
cout<<"local float value of pi="<<pi<<endl;
cout<<"global double value of pi="<<(::pi)<<endl;
return 0;
}
vc++運算符
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.