C++ 复合类型(上)

1.数组

1 数组是一种数据格式,能够存储多个同类型的值。

2 数组声明语句(如 short moths[12];)

  • 存储在每个元素中的值的类型。
  • 数组名。
  • 数组中的元素个数(不能是变量,变量的值是在程序运行时得到的)。

3 有效下标的重要性。编译器不会检查使用的下标是否有效。

4 数组的初始化

  • 只有在定义数组时才能使用初始化,此后就不能使用了,也不能将一个数组赋给另一个数组。
  • 如果只对数组的部分进行初始化,则编译器将把其他元素设置为0.
  • 如果数组没有初始化,则它的值将是不确定的。
  • 初始化数组时。[]内为空,C++编译器会自动计算元素个数。
//马铃薯分析程序
#include<iostream>
int main()
{
    using namespace std;
    int yams[3];//声明一个包含三个int类型元素的数组
    yams[0]=7;//数组下表从0开始
    yams[1]=8;
    yams[2]=6;

    int yamcosts[3]={20,30,5};//声明并初始化一个数组
    cout<<"Total yams=  "<<yams[0]+yams[1]+yams[2]<<endl;
    cout<<"The package with "<<yams[1]<<" yams costs "<<yamcosts[1]<<" cents per yam.
";
    
    int total=yams[0]*yamcosts[0]+yams[1]*yamcosts[1];
    total=total+yams[2]*yamcosts[2];
    cout<<"The total yam expense is "<<total<<" cents.
";

    cout<<"
 Size of yams array= "<<sizeof yams<<" bytes."<<endl;
    cout<<"Size of one element= "<<sizeof yams[0]<<" bytes."<<endl;
    return 0;
}
马铃薯分析程序

2.字符串(C-风格字符串)
1 字符串是存储在内存的连续字节中的一系列字符。

2 C风格字符串以空字符结尾,空字符被写作'’,其ASCII码值为0,用来标记字符串的结尾。

3 可以使用来截短字符串。

1 <span style="font-family: 幼圆; font-size: 15px;">//两个都是char数组,但只有第二个数组是字符串。
2 char dog[5]={'b','e','a','u','x'};
3 char cat[5]={'f','a','t','s',''};
4 </span>

4.关于's'和"s"的区别。"S"表示的是两个字符组成的字符串,更糟糕的是实际上它表示的是字符串所在的内存的地址。

5 拼接字符串常量使用" "--拼接时不会在被连接的字符串之间添加空格。

/将一个数组初始化为用引号括起来的字符串,使用cin将一个输入字符串放到另一个数组中
//使用strlen()确定字符串的长度
#include<iostream>
#include<cstring>//提供strlen()函数原型
int main()
{
    using namespace std;
    const int Size=15;//使用关键字
    char name1[Size];
    char name2[Size]="C++owboy";

    cout<<"Howdy! I'm "<<name2<<" ! What's your name?"<<endl;
    cin>>name1;
    cout<<"Well, "<<name1<<" , your name has "<<strlen(name1)<<" letters and is stored
";
    cout<<"in an array of "<<sizeof (name1)<<" bytes."<<endl;          
    cout<<"You initial ia "<<name1[0]<<".
";
    name2[3]='';
    cout<<"Here are the first 3 characters of my name: "<<name2<<endl;
    return 0;
}
//sizeof操作符指出整个数组的长度,strlen()函数返回的是存储在数组中的字符串的长度,而不是数组本身的长度
//strlen()只计算可见的字符,不把空字符计算在内。
在数组中使用字符串

6 cin使用空白来定字符串的界,意味着cin在获取字符数组输入时只读取一个单词,把该单词放到数组中后,自动在结尾添加空字符。

7 面向行的输入:getline()函数。读取整行,它使用通过回车键入的换行符来确定输入结尾。如:cin.getline(name,20);在读取指定数目的字符或遇到换行符时停止读取。

//面向行输入  getline()函数
#include<iostream>
int main()
{
    using namespace std;
    const int AriSize=20;
    char name[AriSize];
    char dessert[AriSize];

    cout<<"Enter your name:
";
    cin.getline(name,AriSize);
    cout<<"Enter your favorite dessert:
";
    cin.getline(dessert,AriSize);
    cout<<"I have some delicious "<<dessert<<" for you,"<<name<<".
";
    return 0;
}
getline()函数

8 面向行的输入:get()函数。get()函数并不读取并丢弃换行符,而是把它留在了输入队列中。

  • cin.get(x,x);cin.get();cin.get(y,y);//使用这样的方式来处理换行符。
  • 拼接方式:cin.get(x,x).get()
  • 当get()读取空行后将设置失效位cin.clear();
 1 //使用get()函数将两个类成员函数拼接
 2 //把输入中连续的两行分别读入到数组name1和数组name2中
 3 #include<iostream>
 4 int main()
 5 {
 6     using namespace std;
 7     const int AriSize=20;
 8     char name[AriSize];
 9     char dessert[AriSize];
10 
11     cout<<"Enter your name:
";
12     cin.getline(name,AriSize).get();
13     cout<<"Enter your favorite dessert:
";
14     cin.getline(dessert,AriSize).get();
15     cout<<"I have some delicious "<<dessert<<" for you,"<<name<<".
";
16     return 0;
17 }
拼接
 1 //混合输入字符串和数字
 2 /*
 3 #include<iostream>
 4 int main()
 5 {
 6     using namespace std;
 7     cout<<"What's year was your house bulit?
";
 8     int year;
 9     cin>>year;
10     cout<<"What's is its street address?
";
11     char address[80];
12     cin.getline(address,80);
13     cout<<"Year bulit: "<<year<<endl;
14     cout<<"Address: "<<address<<endl;
15     cout<<"Done!"<<endl;
16     return 0;
17 }
18 */
19 #include<iostream>
20 int main()
21 {
22     using namespace std;
23     cout<<"What's year was your house bulit?
";
24     int year;
25     (cin>>year).get();//两种方式,在读取地址之前,先读取并丢弃换行符。
26     //cin.get();
27     cout<<"What's is its street address?
";
28     char address[80];
29     cin.getline(address,80);
30     cout<<"Year bulit: "<<year<<endl;
31     cout<<"Address: "<<address<<endl;
32     cout<<"Done!"<<endl;
33     return 0;
34 }
混合输入字符串和数字

3.string类简介

 1 //string对象与字符数组之间的一些相同点和不同点
 2 //string对象和字符数组的主要区别是可以讲string对象声明为简单变量而不是数组
 3 //类设计让程序能够自动处理string的大小。
 4 #include<iostream>
 5 #include<string>
 6 int main()
 7 {
 8     using namespace std;
 9     char char1[20];
10     char char2[20]="yang";
11     string str1;
12     string str2="yong";
13 
14     cout<<"Enter a kind of feline: ";
15     cin>>char1;
16     cout<<"Enter another kind of feline: ";
17     cin>>str1;
18     cout<<"Here are some felines:
";
19     cout<<char1<<" "<<char2<<" "<<str1<<" "<<str2<<endl;
20     cout<<"The third letter in "<<char2<<" is"<<char2[2]<<endl;
21     cout<<"The third letter in "<<str2<<" is"<<str2[2]<<endl;
22     return 0;
23 }
View Code

1 string类包含在头文件<string>中,它提供了将字符串作为一种数据类型的表示方法。

2 string和字符数组之间的主要区别是:可以将string对象声明为简单变量,而不是数组。

3 类设计让程序能够自动处理string的大小,类声明时创建一个长度为0的string对象,以后会自动调整。

4 不能将一个数组赋给另一个数组,但string可以做到。或者是使用+=将一个字符串附加到一个string对象的末尾,或者使用操作符+将两个操作符合并起来。

 1  //字符串的赋值、拼接和附加
 2 #include<iostream>
 3 #include<string>
 4 int main()
 5 {
 6     using namespace std;
 7     string s1="heyun";
 8     string s2,s3;
 9     cout<<"You can assigan one string object to another:s2=s1: 
";
10     s2=s1;
11     cout<<"s1= "<<s1<<"s2= "<<s2<<endl;
12     cout<<"You can assigan a C—style string to a string object.
";
13     cout<<"s2= "buzzard"
";//打印双引号,初始化s2
14     s2="buzzard";
15     cout<<"s2= "<<s2<<endl;
16     cout<<"You can concatenate strings:s3=s1+s2
";
17     s3=s1+s2;
18     cout<<"s3= "<<s3<<endl;
19     cout<<"You can append strings.
";
20     s1+="for a day";
21     cout<<"s1+=s2 yields s1= "<<s1<<endl;
22     s2+="for a day";
23     cout<<"s2+="for a day" yields s2= "<<s2<<endl;
24 
25     return 0;
26 }
字符串的赋值、拼接和附加

5 两个函数strcpy(str1,str2);和stract(str1,str2);前一个复制,后一个附加。

6 两种确定字符串中字符数的方法

  • int len1=str1.size();
  • int len2=strlen(charr1);
 1 //对用于string对象的技术和用于字符数组的技术进行了比较
 2 #include<iostream>
 3 #include<string>
 4 #include<cstring>//函数原型
 5 int  main()
 6 {
 7     using namespace std;
 8     char char1[20];
 9     char char2[20]="jaguar";//初始化数组
10     string str1;
11     string str2="panther";
12 
13     str1=str2;//赋值
14     strcpy(char1,char2);//将字符串复制
15 
16     str1+="paste";//附加
17     strcat(char1,"juice");//将字符串附加
18 
19     int len1=str1.size();//计算str1的长度,用size()
20     int len2=strlen(char1);//计算char1的长度,用strlen函数
21     
22     cout<<"The string  "<<str1<<" contains "<<len1<<" characters.
";
23     cout<<"The string "<<char1<<" contains "<<len2<<" characters.
";
24 
25     return 0;
26 }
对用于string对象的技术和用于字符数组的技术进行了比较

7 string类I/O

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 int main()
 5 {
 6     using namespace std;
 7     char charr[20];
 8     string str;
 9 //strlen()
10     cout<<"Length of string in charr before input: "<<strlen(charr)<<endl;
11     cout<<"Length of string in str before input: "<<str.size()<<endl;//未被初始化的string对象的长度自动设置为0
12     cout<<"Enter a line of text:
";
13     cin.getline(charr,20);//(目标数组,数组长度),istream类的一个类方法
14     cout<<"You entered: "<<charr<<endl;
15     cout<<"Enter another line of text:
";
16     getline(cin,str);//这个getline()不是类方法
17     cout<<"You entered: "<<str<<endl;
18     cout<<"Length of string in charr after input: "<<strlen(charr)<<endl;
19     cout<<"Length of string in str after input: "<<str.size()<<endl;
20     return 0;
21 }
View Code
    • 未初始化的数组的内容是未定义的,函数strlen()从数组的第一个元素开始计算字节数,直到遇到空字符。
    • 对于未被初始化的数组,第一个空字符出现的位置是随机的。
    • cin.getline(charr,20);//函数getline()是istream类的一个类方法。cin是一个isotream对象。
原文地址:https://www.cnblogs.com/dondre/p/4090180.html