|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 这是她 于 2020-4-21 12:13 编辑
You were born with wings.Why prefer to crawl through life?
想知道复合类型有哪些吗?
快快快 跟上我的小步伐
古灵精怪的数组来喽
- #include<iostream> //输入输出是由iostream库提供的
- using namespace std;
- int main()
- {
- int cards[4] = {3,5,6,7};// √ 在数组初始化的时候给数组赋值
-
- int bee[4];//√ 数组初始化
- bee = {2,5,8,4};//× 不能先把数组先初始化,再赋值哦!注意了哦
- bee = cards;//× 数组初始化以后不能将一个数组赋值给另外一个数组
- bee[0] = 4;
- bee[1] = 6;
- bee[2] = 1;
- bee[3] = 8;//√ 数组初始化以后可以使用下标给数组赋值,下标要从0开始哦!
-
- int candy[5] = {1};//√ 如果只对数组的一部分初始化,编译器会将其他元素设置为0
-
- int tree[] = {4,6,8,1};//√ 如果初始化数组时方括号为空时,编译器将自动计算元素个数
-
- int ball[6] {3,5,2,6,8,0}; //√ 初始化数组时,可省略(=)——只在C++11才允许哦!
- //以上的对数组的初始化都要记牢哦!
-
- cout << "Total cards = ";
- cout << cards[0]+cards[1]+cards[2]+cards[3] << endl;//可以通过下标访问数组元素哦!
-
- cout << "The first member in candy is :" << candy[0] << endl;
- cout << "The third member in candy is :" << candy[4] << endl;//看吧!其他的没有赋值的是0呢
-
- cout << "Size of tree array = " << sizeof tree << endl;//sizeof用于数组名--->整个数组中的字节数
- cout << "Size of one element = " << sizeof tree[2] << endl;//sizeof用于数组元素--->元素的长度
-
- return 0;
- }
复制代码
自带bgm的字符串徐徐驶来............
- #include <iostream>
- #include <string>
- #include <cstring>
- using namespace std;
- int main()
- {
- char pork[10];//空空如也
- char beef[10] = {'d','e','l','i','c','i','o','u','s','0'};//认真看哦!它可不是一个字符串
- char roast[10] = {'d','e','l','i','c','i','o','u','s','\0'};//来啦来啦,这才是真真的呢,最后以空字符结尾
-
- char fish[20] = "delicious" ;//他看起来是不是简洁多了(*^_^*),还能隐式的包括不显示呢
- //在使用数组的时候还得确保数组足够大,以便能够存储字符串中的所有字符(包括'\0')
-
- string ham1,ham2;
- string ham3 = "tasty";
- //使用string类定义字符串
-
- beef = roast;//不可以呢,这是数组
- ham1 = ham3;//这是可以的哦
-
- cout << "The price of pork is :" << endl;
- cin.getline(pork,10);//读取一整行放入到pork数组中。它通过换行符来确定结尾,但不保存换行符
- cout << "You think the pork is :" << pork << endl;
-
- cout << "The price of pork is :" << endl;
- cin.get(pork,10).get();//这样写可以检查下一个字符是不是换行符。是-->已经读取了整行;否-->该行中还有其他输入
- cout << "You think the pork is :" << pork << endl;
-
- cout << strlen(fish) << "and" << sizeof(fish) << endl;
- //strlen是返回存储在数组中字符串的长度;sizeof是指出整个数组的长度
-
- cout << "ham1 :" << ham1 << endl;//将ham3赋值给ham1
-
- ham3 += " food";
- cout << "ham3 :" << ham3 << endl;
- strcat(ham1, " meat");
- cout << "ham1 :" << ham1 << endl;//将字符串拼接在另一个字符串之后
-
- ham2 = ham1;
- cout << "ham2 :" << ham2 << endl;
- strcpy(ham2, ham3);
- cout << "ham2 :" << ham2 << endl;//以上两个都是将一个字符串复制到另一个字符串中
-
- return 0;
- }
复制代码
渣渣一个,大佬们手下留情
点都点开了,不给点建议再走吗(会继续努力的呢)
|
|