1)数组初始化
1.1 整数未赋值元素为0
int a[4]={2,3};
//结果 a[0]=2; a[1]=3; a[2]=0; a[3]=0;
//可以利用此性质,初始化所有元素为0;
1.2 字符数组未赋值为'\0',“赋值” 只能在初始化时进行
1.3 字符串数组初始化
char c[]="China";// 长度为6, 末尾自动补'\0'
char c[]={'C','h','i','n','a'}; 长度5位
char c[5]="China"; //错误
//所有字符串以'\0'结尾
字符串= 字符数组+'\0'
2) 输入一个字符
// 2.1 cin 跳过 空格、回车
// Example:
while(cin>>c)
cout<<c;
// 运行模拟:
// I:abc def g
// O:abcdefg
//小提示,利用ctrl+z 结束输入
// 2.2 cin.get() 不跳过空格、回车
// Example:
while((c=cin.get())!= EOF)
cout<<c;
// 运行模拟:
// I:abc def g
// O:abc def g
// 2.3 另一种写法 cin.get(char)
// Example:
while(cin.get(c))
cout<<c;
// 运行模拟:
// I:abc def g
// O:abc def g
//小提示,利用ctrl+z 结束输入
//2.4 getchar() 不跳过任何字符 包括ctrl+z
// Example:
while(c=getchar())
cout<<c;
// 运行模拟:
// I:abc def g
// O:abc def g
////小提示,利用ctrl+z 不再有效,需要利用ctrl+C 强制退出
3) 输入字符串
// 输入字符串
//
// char str[5]={'C','h'};
// cout<<str<<endl;
// cout<<sizeof(str)<<endl;
// //Firstly, the sizeof() operator does not give you the number of elements in an array,
// //it gives you the number of bytes a thing occupies in memory. Hence:
// // str[]="Ch" sizeof(str) return 3
// // str[]={'C','h'} 2
////3.1 cin
//while(cin>>str)
// cout<<str<<endl;
//输入:how are you
//how
//are
//you
//^Z
//
//Process returned 0 (0x0) execution time : 9.231 s
//Press any key to continue.
//
////3.2 cin.get()buffer 指针停止在结束符
// cin.get(字符数组,字符数n,终止符(默认\n))
// cin.get(ch,20,'\n');
// cout<<ch<<endl;;
//
//输入:how are you
//how are you
//
//Process returned 0 (0x0) execution time : 6.170 s
//Press any key to continue.
//3.3 cin.getline() buffer指针停在终止符后
// cin.getline(字符数组(或字符指针),字符个数n,终止标志字符)
// cin.getline(ch,20,'\n');
// cout<<ch<<endl;
//how are you
//how are you
//
//Process returned 0 (0x0) execution time : 5.791 s
//Press any key to continue.
图片来自:coursera 公开课