intarray[10]={1,2,3,4,5,6};
for(inti=0;i<10;i++)
cout<< array[i];
定义一个数组,他的下标就是就代表其中每个元素!
**************************************************
complex在c99中是一种数据类型,编译的时候要加-lm选项
_Complexdouble z1 = -1; // C99
doublecomplex sz1;//gnu gcc extension
sz1= csqrt(z1); //C99 and in math library, build with -lm
complex在C++中被定义为class,两者不能兼容!
complex<double> z1 = complex<double>(1,2);
complex<double>sz1;
*****************************************************
如何找到字符串结尾?
char*s1, char*s2
while(*s1) //找到字符串结尾的方法!!
s1++;
len= strlen(s2);
while(*s2&& len) //这里将把s2字符串的len长度复制到s1后面
{
*s1 = *s2;
s1++;
s2++;
len--;
}
******************************************************
charca[]= "abc\012\0x34";
//注意\012表示一个字节,而\0x34则表示已经到了结尾,所以一共a,b,c,\012,共4个字节
//其实如果\和x之间没有0的话,则最后几个字节都是算得!!!!!!!!!!!
cout<< "Sizeis "<< strlen(ca)<<endl;
所以ca的大小是4个字节
******************************************************
overload:
同一个函数名,不同的定义,在调用的时候会根据参数类型不同,数量不同自动判断
#include<iostream>
usingnamespacestd;
voidf(inti); // integer parameter
voidf(inti, intj); // two integer parameters
voidf(doublek); // one double parameter
intmain()
{
f(10); //call f(int)
f(10, 20); //call f(int, int)
f(12.23); //call f(double)
return0;
}
voidf(inti)
{
cout<< "In f(int), i is "<< i << '\n';
}
voidf(inti, intj)
{
cout<< "In f(int, int), i is "<< i;
cout<< ", j is "<< j << '\n';
}
voidf(doublek)
{
cout<< "In f(double), k is "<< k << '\n';
}
*****************************************************
关于reference的使用!
double&f();
doubleval = 100.0;
intmain()
{
doublenewval;
cout<< f() << '\n';// display val's value
newval = f();// assign value of val to newval
cout<< newval << '\n';// display newval's value
f()= 99.1; // change val's value
cout<< val<< '\n';// display val's new value
//因为是引用,所以将原来的变量也该变了!!!!
//所以最后的val=99.1
return0;
}
double&f()
{
returnval; // return reference to val
}
***********************************************************
关于有符号及无符号的区别:
shortinti; // a signed short integer
shortunsignedintj; // an unsigned short integer
j= 32768; //无符号
i= j; //有符号,i变为负值!!
cout<< i << ""<< j <<endl;
//short类型,无符号的是16位,2的16次方是65536,所以范围是到65535
//有符号时,去掉符号位还剩15位,2的15次方是32768,所以范围是32767
//当进行赋值时,如果有符号的数大于32767,有符号的将会变成负数!!!!!
************************************************************
同样的数组下标问题:
对某一下标元素的改变将会改变字符串对应的字母,注意下标从0开始!
charact[]= "Learn";
cout<< act << " "<< name <<endl;
name[5]='';
cout<<"Name was changed"<<endl;
act[2]='';
cout<< act << " "<< name <<endl;
如果被定义成constcharact[] = "Learn";
因为有const,不能对其进行改变!
******************************************************************
将两变量定义为指针类型,利用取地址符&进行交换
#include<iostream>
usingnamespacestd;
//Declare swap() using pointers.
voidswap(int*x, int*y);
intmain()
{
inti, j;
i= 10;
j= 20;
cout<< "initial values of i andj: ";
cout<< i << ' '<< j << '\n';
swap(&j,&i); // call swap() with addressesof i and j
cout<< "swapped values of i andj: ";
cout<< i << ' '<< j << '\n';
return0;
}
//Exchange arguments.
voidswap(int*x, int*y)
{
inttemp;
temp= *x; // save the value at address x
*x= *y; // put y into x
*y= temp; // put x into y
}
****************************************************************
将两个变量定义成reference进行交换:
#include<iostream>
usingnamespacestd;
//Declare swap() using reference parameters.
voidswap(int&x, int&y);
intmain()
{
inti, j;
i= 10;
j= 20;
cout<< "initial values of i andj: ";
cout<< i << ' '<< j << '\n';
swap(j, i);
cout<< "swapped values of i andj: ";
cout<< i << ' '<< j << '\n';
return0;
}
/*Here, swap() is defined as using call-by-reference,
notcall-by-value. Thus, it can exchange the two
arguments itis called with.
*/
voidswap(int&x, int&y)
{
inttemp;
temp= x; // save the value at address x
x= y; // put y into x
y= temp; // put x into y
}
***************************************************
使用union交换比特位的方式,注意后面的位与方法,底层经常用到:
#include<iostream>
usingnamespacestd;
voiddisp_binary(unsignedu);
unionswap_bytes{ //union结构特性的一种使用
shortintnum; //short类型2个子节,16位
charch[4]; //char类型1个子节,8位
};
intmain()
{
swap_bytessb;
chartemp;
sb.num= 15 ; // binary: 0000 0000 0000 00000000 0000 0000 1111
cout<< "Original bytes: ";
disp_binary(sb.ch[1]);
cout<< " ";
disp_binary(sb.ch[0]);
cout<< "\n\n";
//exchange the bytes
temp= sb.ch[0];
sb.ch[0]= sb.ch[1];
sb.ch[1]= temp;
cout<< "Exchanged bytes: ";
disp_binary(sb.ch[1]);
cout<< " ";
disp_binary(sb.ch[0]);
cout<< "\n\n";
return0;
}
//Display the bits within a byte. 按照字节输出比特位!
voiddisp_binary(unsignedu)
{
registerintt;
for(t=512;t>0; t=t/2) //这里t的值决定打印出来的位数,2的8次方是256,所以打印出来时是 //9位!!!!
if(u& t) cout << "1 "; //这里用的方法是位与
elsecout << "0 ";
}
***************************************************