程序清单4.1

 #include<iostream>
using namespace std; void main(){
int yams[];
yams[]=;
yams[]=;
yams[]=;
int yams_cost[]={,,}; cout<<"Total yams="<<yams[]+yams[]+yams[]<<endl;
cout<<"The package with "<<yams[]<<" yams costs "<<yams_cost[]<<" cents per yam"<<endl;
cout<<"Total costs="<<yams[]*yams_cost[]+yams[]*yams_cost[]+yams[]*yams_cost[]<<" cents"<<endl; cout<<"Size of yams array="<<sizeof(yams)<<" bytes"<<endl;
cout<<"Size of yams[0]="<<sizeof(yams[])<<" bytes"<<endl; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.2

 #include<iostream>
#include<cstring>//strlen(),c语言版为string.h
using namespace std; void main(){
const int size=;
char name1[size];//char占一个字节
char name2[size]="C++owboy"; cout<<"I am "<<name2<<".What's your name?"<<endl;
cin>>name1;
cout<<"Well,"<<name1<<",your name has "<<strlen(name1)<<" letters and is stored in an array of "<<sizeof(name1)<<" bytes"<<endl;
cout<<"Your initial is "<<name1[]<<endl;
name2[]='\0';
cout<<"Here are the first 3 characters of my name:"<<name2<<endl;
system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.3

 #include<iostream>
using namespace std; void main(){
const int size=;
char name[size];
char dessert[size]; cout<<"What's your name?"<<endl;
cin>>name;
cout<<"What's your favorate dessert?"<<endl;
cin>>dessert;
cout<<"I have some delicious "<<dessert<<" for you."<<endl;
system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

注意:cin使用空白(空格、制表符和换行符)来确定字符串的结束位置,所以cin将Alise放在了name数组,将dreeb放在了dessert数组

为了解决这种问题,C++提供getline()和get()方法,如下所示:

程序清单4.4

 #include<iostream>
using namespace std; void main(){
const int size=;
char name[size];
char dessert[size]; cout<<"What's your name?"<<endl;
cin.getline(name,size);
cout<<"What's your favorate dessert?"<<endl;
cin.getline(dessert,size);
cout<<"I have some delicious "<<dessert<<" for you."<<endl;
system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.6

 #include<iostream>
using namespace std; void main(){
cout<<"你房子哪一年修的?"<<endl;
int year;
cin>>year;
cout<<"房子的地址?"<<endl;
char address[];
cin.getline(address,);
cout<<"Year built:"<<year<<endl<<"Address:"<<address<<endl<<"Done!";
system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

用户没有输入地址的机会,问题在于:cin读取年份后,将回车键生成的换行符留在了队列中,后面的cin.getline()看到换行符后,将认为是一个空行。解决如下:

  cin>>year;

  cin.get();

程序清单4.7(string和数组)

 #include<iostream>
#include<string>
using namespace std; void main(){
char ch1[];
char ch2[]="jaguar";
string str1;
string str2="panther"; cout<<"enter a kind of feline:";
cin>>ch1;
cout<<"enter another kind of feline:";
cin>>str1;
cout<<"Here are some felines:\n"<<ch1<<" "<<ch2<<" "<<str1<<" "<<str2<<endl;
cout<<"The third letter in "<<ch2<<" is "<<ch2[]<<endl;
cout<<"The third letter in "<<str2<<" is "<<str2[]<<endl;
system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.8(string赋值、拼接和附加)

 #include<iostream>
#include<string>
using namespace std; void main(){
string str="penguin";
string str2,str3,str4; str2=str;
cout<<"str2="<<str2<<endl;
str3="buzzard";
cout<<"str3="<<str3<<endl;
str4=str+str3;
cout<<"str4="<<str4<<endl;
str+=str3;
cout<<"str="<<str<<endl;
str2+=" is cute!";
cout<<"str2="<<str2<<endl;
system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.9(cstring)

 #include<iostream>
#include<string>
#include<cstring>//strcpy,strcat,strlen
using namespace std; void main(){
char ch1[];
char ch2[]="jaguar";
string str1;
string str2="panther"; str1=str2;
strcpy(ch1,ch2); str1+=" paste";
strcat(ch1," juice"); int len1=str1.size();
int len2=strlen(ch1); cout<<"the string "<<str1<<" contains "<<len1<<" characters"<<endl;
cout<<"the string "<<ch1<<" contains "<<len2<<" characters"<<endl; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.10

 #include<iostream>
#include<string>
#include<cstring>
using namespace std; void main(){
char ch[];
string str; cout<<"Length of string in ch before input:"<<strlen(ch)<<endl;
cout<<"Length of string in str before input:"<<str.size()<<endl;
cout<<"Enter a line of text:"<<endl;
cin.getline(ch,);
cout<<"You entered:"<<ch<<endl;
cout<<"Enter another line of text:"<<endl;
getline(cin,str);
cout<<"You entered:"<<str<<endl;
cout<<"Length of string in ch after input:"<<strlen(ch)<<endl;
cout<<"Length of string in str after input:"<<str.size()<<endl; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

注意:未初始化的数组内容未定,strlen()从数组中的第一个元素开始计算字节数,直到遇到空字符。在本例中数组内并未有空字符,所以字节数甚至超过了数组长度;未初始化的string对象长度置0

程序清单4.11+4.12(结构体)

 #include<iostream>
using namespace std; struct table{
char name[];
float volumn;
double price;
}; void main(){
table guest={"GG",1.88,29.99};
table pal={"AA",3.12,32.99};
cout<<"Expand your guest list with "<<guest.name<<" and "<<pal.name<<endl;
cout<<"You can have both for $"<<guest.price+pal.price<<endl; cout<<"------------------------------"<<endl; table choice=guest;
cout<<"choice:"<<choice.name<<" for $"<<choice.price<<endl; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.13

 #include<iostream>
using namespace std; struct table{
char name[];
float volumn;
double price;
}; void main(){
table guest[]={
{"GG",1.88,29.99},
{"AA",3.12,32.99}
};
cout<<"The guests "<<guest[].name<<" and "<<guest[].name<<endl<<"have a combined volumn of "<<guest[].volumn+guest[].volumn<<" cubic feet"<<endl; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.17(指针:new)

 #include<iostream>
using namespace std; void main(){
int nights=;
int *pt = new int;
*pt = ;
double *pd = new double;
*pd = 100000000000.1; cout << "nights value=" << nights << ":location " << &nights << endl;
cout << "int value=" <<*pt<< ":location " <<pt<< endl;
cout << "double value=" << *pd << ":location " << pd << endl;
cout << "location of pointer pd:" << &pd<< endl; cout << "size of pt=" <<sizeof(pt)<< endl;
cout << "size of *pt=" << sizeof(*pt) << endl;
cout << "size of pd=" << sizeof(pd) << endl;
cout << "size of *pd=" << sizeof(*pd) << endl; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.18(new完之后要delete)

 #include<iostream>
using namespace std; void main() {
double *p = new double[];//使用new创建动态数组
p[] = 0.2;
p[] = 0.5;
p[] = 0.8;
cout << "p[1]=" << p[] << endl;
p++;
cout << "Now p[0]=" << p[] << endl;
cout << "p[1]=" << p[] << endl;
p--;
cout << "And now p[0]=" << p[] << endl;
delete[] p;//回归初始位置释放内存
getchar();
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.19

 #include<iostream>
using namespace std; void main() {
double wages[] = { 10000.0,20000.0, 30000.0 };
short stacks[] = { , , };
double * pw = wages;
short * ps = &stacks[]; cout << "pw=" << pw <<",*pw="<<*pw<< endl;
pw++;
cout << "Now,pw=" << pw << ",*pw=" << *pw << endl;
cout << "ps=" << ps << ",*ps=" << *ps << endl;
ps++;
cout << "Now,ps=" << ps << ",*ps=" << *ps << endl; cout << "stacks[0]=" << stacks[] << ",stacks[1]=" << stacks[] << endl;
cout << "*stacks=" << *stacks<< ",*(stacks+1)=" << *(stacks + ) << endl;
cout << "sizeof(wages)=" << sizeof(wages)<< endl;
cout << "sizeof(pw)=" << sizeof(pw) << endl; getchar();
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.20

 #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
using namespace std; void main() {
char animal[] = "bear";
const char * bird = "wren";
char * ps; cout << animal <<" and "<< bird << endl;
cout << "Enter a kind of animal:";
cin >> animal;
ps = animal;
cout << "ps="<<ps<< endl;
cout << "Before using strcpy(): \n ";
cout << animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl; ps = new char[strlen(animal) + ];
strcpy(ps, animal);
cout << "After using strcpy(): \n ";
cout << animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl;
delete[] ps; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.21

 #include<iostream>
using namespace std; struct inflatable {
char name[];
float volumn;
double price;
}; void main() {
inflatable *ps = new inflatable; cout <<"Enter name of inflatable item:";
cin.get(ps->name,);
cout << "Enter volumn in cubic feet:";
cin>>(*ps).volumn;
cout << "Enter price:$";
cin >>ps->price; cout << "Name:" << (*ps).name<< endl;
cout << "Volumn:" <<ps->volumn<< endl;
cout << "Price:" <<ps->price<< endl;
delete ps; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.22

 #include<iostream>
#include<cstring>
using namespace std; char * getname() {
char temp[];
cout << "Enter last name:";
cin>>temp;
char *pn = new char[strlen(temp) + ];//长度+1:包括一个空字符
strcpy(pn, temp);
return pn;
} void main(){
char * name;
name = getname();
cout << name << " at " << (int *) name << endl;
delete[] name; name = getname();
cout << name << " at " << (int *) name << endl;
delete[] name; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

两次地址可能相同,也可能不同。

程序清单4.23(类型组合)

 #include<iostream>
#include<cstring>
using namespace std; struct Student {
int age;
}; void main(){
Student s1, s2, s3;
s1.age = ;
Student *p = &s2;
p->age = ;
Student ss[];
ss[].age = ; cout << "ss[0].age=" << ss->age << endl; const Student * arp[] = { &s1,&s2,&s3 };//指针数组:由指针组成的数组
cout << "s1.age="<<arp[]->age<< endl; const Student **ppa = arp;//arp是数组名称,即第一个元素的地址,而第一个元素为指针,所以ppa是二级指针
auto ppb = arp;//自动推断类型
cout << "s1.age=" << (*ppa)->age << endl;
cout << "s2.age=" << (*(ppa+))->age<< endl; //ppa + 1指向arp[1],即&s2
//cout << "s2.age=" << (*ppa + 1)->age << endl; 错误! system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

程序清单4.24(数组的替代品,array,vector)

 #include<iostream>
#include<vector>
#include<array>
using namespace std; void main() {
double a[] = { 1.2,2.4,3.6,4.8 };
vector<double> b();
b[] = 1.0 / 3.0;
b[] = 1.0 / 5.0;
b[] = 1.0 / 7.0;
b[] = 1.0 / 9.0;
array<double, > c = { 3.14,2.72,1.62,1.41 };
array<double, > d;
d = c; cout << "a[2]:" << a[] << " at " << &a[]<< endl;//&a[2]=a + 2
cout << "b[2]:" << b[] << " at " << &b[] << endl;
cout << "c[2]:" << c[] << " at " << &c[] << endl;
cout << "d[2]:" << d[] << " at " << &d[] << endl<<endl; a[-] = 20.2;//a[-2]=*(a-2)
cout << "a[2]:" << a[-]<< " at " << &a[-]<< endl;
cout << "c[2]:" << c[] << " at " << &c[] << endl;
cout << "d[2]:" << d[] << " at " << &d[] << endl; system("pause");
}

[C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete-LMLPHP

注意:无论是数组,vector还是array对象,都可以使用数组表示法来访问各个元素。其次,从地址可知,array和数组都是存储在栈中,而vector存储在自由存储区或堆中。第三,可以将array对象赋给另一个array对象;而对于数组,必须逐元素复制。

索引-2转换为:a[-2]=*(a-2)
04-04 00:52