标签:col 结束 get 动态数组 指针数组 student use define 访问
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 int yams[3]; 6 yams[0]=7; 7 yams[1]=8; 8 yams[2]=6; 9 int yams_cost[3]={20,30,5}; 10 11 cout<<"Total yams="<<yams[0]+yams[1]+yams[2]<<endl; 12 cout<<"The package with "<<yams[1]<<" yams costs "<<yams_cost[1]<<" cents per yam"<<endl; 13 cout<<"Total costs="<<yams[0]*yams_cost[0]+yams[1]*yams_cost[1]+yams[2]*yams_cost[2]<<" cents"<<endl; 14 15 cout<<"Size of yams array="<<sizeof(yams)<<" bytes"<<endl; 16 cout<<"Size of yams[0]="<<sizeof(yams[0])<<" bytes"<<endl; 17 18 system("pause"); 19 }
1 #include<iostream> 2 #include<cstring>//strlen(),c语言版为string.h 3 using namespace std; 4 5 void main(){ 6 const int size=15; 7 char name1[size];//char占一个字节 8 char name2[size]="C++owboy"; 9 10 cout<<"I am "<<name2<<".What‘s your name?"<<endl; 11 cin>>name1; 12 cout<<"Well,"<<name1<<",your name has "<<strlen(name1)<<" letters and is stored in an array of "<<sizeof(name1)<<" bytes"<<endl; 13 cout<<"Your initial is "<<name1[0]<<endl; 14 name2[3]=‘\0‘; 15 cout<<"Here are the first 3 characters of my name:"<<name2<<endl; 16 system("pause"); 17 }
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 const int size=15; 6 char name[size]; 7 char dessert[size]; 8 9 cout<<"What‘s your name?"<<endl; 10 cin>>name; 11 cout<<"What‘s your favorate dessert?"<<endl; 12 cin>>dessert; 13 cout<<"I have some delicious "<<dessert<<" for you."<<endl; 14 system("pause"); 15 }
注意:cin使用空白(空格、制表符和换行符)来确定字符串的结束位置,所以cin将Alise放在了name数组,将dreeb放在了dessert数组
为了解决这种问题,C++提供getline()和get()方法,如下所示:
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 const int size=15; 6 char name[size]; 7 char dessert[size]; 8 9 cout<<"What‘s your name?"<<endl; 10 cin.getline(name,size); 11 cout<<"What‘s your favorate dessert?"<<endl; 12 cin.getline(dessert,size); 13 cout<<"I have some delicious "<<dessert<<" for you."<<endl; 14 system("pause"); 15 }
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 cout<<"你房子哪一年修的?"<<endl; 6 int year; 7 cin>>year; 8 cout<<"房子的地址?"<<endl; 9 char address[80]; 10 cin.getline(address,80); 11 cout<<"Year built:"<<year<<endl<<"Address:"<<address<<endl<<"Done!"; 12 system("pause"); 13 }
用户没有输入地址的机会,问题在于:cin读取年份后,将回车键生成的换行符留在了队列中,后面的cin.getline()看到换行符后,将认为是一个空行。解决如下:
cin>>year;
cin.get();
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 void main(){ 6 char ch1[20]; 7 char ch2[20]="jaguar"; 8 string str1; 9 string str2="panther"; 10 11 cout<<"enter a kind of feline:"; 12 cin>>ch1; 13 cout<<"enter another kind of feline:"; 14 cin>>str1; 15 cout<<"Here are some felines:\n"<<ch1<<" "<<ch2<<" "<<str1<<" "<<str2<<endl; 16 cout<<"The third letter in "<<ch2<<" is "<<ch2[2]<<endl; 17 cout<<"The third letter in "<<str2<<" is "<<str2[2]<<endl; 18 system("pause"); 19 }
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 void main(){ 6 string str="penguin"; 7 string str2,str3,str4; 8 9 str2=str; 10 cout<<"str2="<<str2<<endl; 11 str3="buzzard"; 12 cout<<"str3="<<str3<<endl; 13 str4=str+str3; 14 cout<<"str4="<<str4<<endl; 15 str+=str3; 16 cout<<"str="<<str<<endl; 17 str2+=" is cute!"; 18 cout<<"str2="<<str2<<endl; 19 system("pause"); 20 }
1 #include<iostream> 2 #include<string> 3 #include<cstring>//strcpy,strcat,strlen 4 using namespace std; 5 6 void main(){ 7 char ch1[20]; 8 char ch2[20]="jaguar"; 9 string str1; 10 string str2="panther"; 11 12 str1=str2; 13 strcpy(ch1,ch2); 14 15 str1+=" paste"; 16 strcat(ch1," juice"); 17 18 int len1=str1.size(); 19 int len2=strlen(ch1); 20 21 cout<<"the string "<<str1<<" contains "<<len1<<" characters"<<endl; 22 cout<<"the string "<<ch1<<" contains "<<len2<<" characters"<<endl; 23 24 system("pause"); 25 }
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 using namespace std; 5 6 void main(){ 7 char ch[20]; 8 string str; 9 10 cout<<"Length of string in ch before input:"<<strlen(ch)<<endl; 11 cout<<"Length of string in str before input:"<<str.size()<<endl; 12 cout<<"Enter a line of text:"<<endl; 13 cin.getline(ch,20); 14 cout<<"You entered:"<<ch<<endl; 15 cout<<"Enter another line of text:"<<endl; 16 getline(cin,str); 17 cout<<"You entered:"<<str<<endl; 18 cout<<"Length of string in ch after input:"<<strlen(ch)<<endl; 19 cout<<"Length of string in str after input:"<<str.size()<<endl; 20 21 system("pause"); 22 }
注意:未初始化的数组内容未定,strlen()从数组中的第一个元素开始计算字节数,直到遇到空字符。在本例中数组内并未有空字符,所以字节数甚至超过了数组长度;未初始化的string对象长度置0
1 #include<iostream> 2 using namespace std; 3 4 struct table{ 5 char name[20]; 6 float volumn; 7 double price; 8 }; 9 10 void main(){ 11 table guest={"GG",1.88,29.99}; 12 table pal={"AA",3.12,32.99}; 13 cout<<"Expand your guest list with "<<guest.name<<" and "<<pal.name<<endl; 14 cout<<"You can have both for $"<<guest.price+pal.price<<endl; 15 16 cout<<"------------------------------"<<endl; 17 18 table choice=guest; 19 cout<<"choice:"<<choice.name<<" for $"<<choice.price<<endl; 20 21 system("pause"); 22 }
1 #include<iostream> 2 using namespace std; 3 4 struct table{ 5 char name[20]; 6 float volumn; 7 double price; 8 }; 9 10 void main(){ 11 table guest[2]={ 12 {"GG",1.88,29.99}, 13 {"AA",3.12,32.99} 14 }; 15 cout<<"The guests "<<guest[0].name<<" and "<<guest[1].name<<endl<<"have a combined volumn of "<<guest[0].volumn+guest[1].volumn<<" cubic feet"<<endl; 16 17 system("pause"); 18 }
1 #include<iostream> 2 using namespace std; 3 4 void main(){ 5 int nights=1001; 6 int *pt = new int; 7 *pt = 999; 8 double *pd = new double; 9 *pd = 100000000000.1; 10 11 cout << "nights value=" << nights << ":location " << &nights << endl; 12 cout << "int value=" <<*pt<< ":location " <<pt<< endl; 13 cout << "double value=" << *pd << ":location " << pd << endl; 14 cout << "location of pointer pd:" << &pd<< endl; 15 16 cout << "size of pt=" <<sizeof(pt)<< endl; 17 cout << "size of *pt=" << sizeof(*pt) << endl; 18 cout << "size of pd=" << sizeof(pd) << endl; 19 cout << "size of *pd=" << sizeof(*pd) << endl; 20 21 system("pause"); 22 }
1 #include<iostream> 2 using namespace std; 3 4 void main() { 5 double *p = new double[3];//使用new创建动态数组 6 p[0] = 0.2; 7 p[1] = 0.5; 8 p[2] = 0.8; 9 cout << "p[1]=" << p[1] << endl; 10 p++; 11 cout << "Now p[0]=" << p[0] << endl; 12 cout << "p[1]=" << p[1] << endl; 13 p--; 14 cout << "And now p[0]=" << p[0] << endl; 15 delete[] p;//回归初始位置释放内存 16 getchar(); 17 }
1 #include<iostream> 2 using namespace std; 3 4 void main() { 5 double wages[3] = { 10000.0,20000.0, 30000.0 }; 6 short stacks[3] = { 3 , 2, 1 }; 7 double * pw = wages; 8 short * ps = &stacks[0]; 9 10 cout << "pw=" << pw <<",*pw="<<*pw<< endl; 11 pw++; 12 cout << "Now,pw=" << pw << ",*pw=" << *pw << endl; 13 cout << "ps=" << ps << ",*ps=" << *ps << endl; 14 ps++; 15 cout << "Now,ps=" << ps << ",*ps=" << *ps << endl; 16 17 cout << "stacks[0]=" << stacks[0] << ",stacks[1]=" << stacks[1] << endl; 18 cout << "*stacks=" << *stacks<< ",*(stacks+1)=" << *(stacks + 1) << endl; 19 cout << "sizeof(wages)=" << sizeof(wages)<< endl; 20 cout << "sizeof(pw)=" << sizeof(pw) << endl; 21 22 getchar(); 23 }
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 6 void main() { 7 char animal[20] = "bear"; 8 const char * bird = "wren"; 9 char * ps; 10 11 cout << animal <<" and "<< bird << endl; 12 cout << "Enter a kind of animal:"; 13 cin >> animal; 14 ps = animal; 15 cout << "ps="<<ps<< endl; 16 cout << "Before using strcpy(): \n "; 17 cout << animal << " at " << (int *)animal << endl; 18 cout << ps << " at " << (int *)ps << endl; 19 20 ps = new char[strlen(animal) + 1]; 21 strcpy(ps, animal); 22 cout << "After using strcpy(): \n "; 23 cout << animal << " at " << (int *)animal << endl; 24 cout << ps << " at " << (int *)ps << endl; 25 delete[] ps; 26 27 system("pause"); 28 }
1 #include<iostream> 2 using namespace std; 3 4 struct inflatable { 5 char name[20]; 6 float volumn; 7 double price; 8 }; 9 10 void main() { 11 inflatable *ps = new inflatable; 12 13 cout <<"Enter name of inflatable item:"; 14 cin.get(ps->name,20); 15 cout << "Enter volumn in cubic feet:"; 16 cin>>(*ps).volumn; 17 cout << "Enter price:$"; 18 cin >>ps->price; 19 20 cout << "Name:" << (*ps).name<< endl; 21 cout << "Volumn:" <<ps->volumn<< endl; 22 cout << "Price:" <<ps->price<< endl; 23 delete ps; 24 25 system("pause"); 26 }
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 char * getname() { 6 char temp[80]; 7 cout << "Enter last name:"; 8 cin>>temp; 9 char *pn = new char[strlen(temp) + 1];//长度+1:包括一个空字符 10 strcpy(pn, temp); 11 return pn; 12 } 13 14 void main(){ 15 char * name; 16 name = getname(); 17 cout << name << " at " << (int *) name << endl; 18 delete[] name; 19 20 name = getname(); 21 cout << name << " at " << (int *) name << endl; 22 delete[] name; 23 24 system("pause"); 25 }
两次地址可能相同,也可能不同。
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 struct Student { 6 int age; 7 }; 8 9 void main(){ 10 Student s1, s2, s3; 11 s1.age = 19; 12 Student *p = &s2; 13 p->age = 20; 14 Student ss[3]; 15 ss[0].age = 22; 16 17 cout << "ss[0].age=" << ss->age << endl; 18 19 const Student * arp[3] = { &s1,&s2,&s3 };//指针数组:由指针组成的数组 20 cout << "s1.age="<<arp[0]->age<< endl; 21 22 const Student **ppa = arp;//arp是数组名称,即第一个元素的地址,而第一个元素为指针,所以ppa是二级指针 23 auto ppb = arp;//自动推断类型 24 cout << "s1.age=" << (*ppa)->age << endl; 25 cout << "s2.age=" << (*(ppa+1))->age<< endl; //ppa + 1指向arp[1],即&s2 26 //cout << "s2.age=" << (*ppa + 1)->age << endl; 错误! 27 28 system("pause"); 29 }
1 #include<iostream> 2 #include<vector> 3 #include<array> 4 using namespace std; 5 6 void main() { 7 double a[4] = { 1.2,2.4,3.6,4.8 }; 8 vector<double> b(4); 9 b[0] = 1.0 / 3.0; 10 b[1] = 1.0 / 5.0; 11 b[2] = 1.0 / 7.0; 12 b[3] = 1.0 / 9.0; 13 array<double, 4> c = { 3.14,2.72,1.62,1.41 }; 14 array<double, 4> d; 15 d = c; 16 17 cout << "a[2]:" << a[2] << " at " << &a[2]<< endl;//&a[2]=a + 2 18 cout << "b[2]:" << b[2] << " at " << &b[2] << endl; 19 cout << "c[2]:" << c[2] << " at " << &c[2] << endl; 20 cout << "d[2]:" << d[2] << " at " << &d[2] << endl<<endl; 21 22 a[-2] = 20.2;//a[-2]=*(a-2) 23 cout << "a[2]:" << a[-2]<< " at " << &a[-2]<< endl; 24 cout << "c[2]:" << c[2] << " at " << &c[2] << endl; 25 cout << "d[2]:" << d[2] << " at " << &d[2] << endl; 26 27 system("pause"); 28 }
注意:无论是数组,vector还是array对象,都可以使用数组表示法来访问各个元素。其次,从地址可知,array和数组都是存储在栈中,而vector存储在自由存储区或堆中。第三,可以将array对象赋给另一个array对象;而对于数组,必须逐元素复制。
索引-2转换为:a[-2]=*(a-2)
[C++ Primer Plus] 4、复合类型(一)程序清单
标签:col 结束 get 动态数组 指针数组 student use define 访问
原文地址:http://www.cnblogs.com/little-monkey/p/7401662.html