数组
#include <iostream>
int main()
{
using namespace std;
int yams[3];
yams[0] = 7;
yams[1] = 8;
yams[2] = 6;
int yamcosts[3] = {20,50,5};
cout << "Total yams = ";
cout << yams[0] + yams[1] +yams[2] << endl;
cout << "The package with " << yams[1] << "yams costs ";
cout << yamcosts[1] << "cents per yam.\n";
int total = yams[0] * yamcosts[0] + yams[1] * yams[1];
total = total + yams[2] * yamcosts[2];
cout << "The total yam expense is" << total << "cents.\n";
cout << "\nsize of yams array = " <<sizeof yams;
cout << "bytes.\n";
cout << "size of one element = " << sizeof yams[0];
cout << "bytes.\n";
return 0;
}
Total yams = 21
The package with 8yams costs 50cents per yam.
The total yam expense is234cents.
size of yams array = 12bytes.
size of one element = 4bytes.
字符串
//在数组中使用字符串
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int size = 15;
char name1[size];//此处定义了一个空字符串
char name2[size] = "c++owboy";//初始化数组
cout << "Howdy! I'm " << name2;
cout << "! What's your name?\n";
cin >> name1;
cout << "Well, " << name1 << " your name has";
cout << strlen(name1) <<"letters and is sorted\n";//strlen()计算可见的字符,不把空字符计算在内。
cout << "in an array of " << sizeof(name1) << " bytes.\n";
cout << "Your initial is " << name1[0] << ".\n";
name2[3] = '\0';
//设置name2数组的第四个元素为空字符(索引号是3),这可使得字符串在第3个字符后就结束,即使数组中还有其他字符。
cout << "Here are the first 3 characters of my name: ";
cout << name2 << endl;
return 0;
}
Howdy! I'm c++owboy! What's your name?
guo
Well, guo your name has3letters and is sorted
in an array of 15 bytes.
Your initial is g.
字符串输入
#include <iostream>
int main()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name.\n";
cin >> name;
cout << "Enter your favorite dessert.\n";
cin >> dessert;
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
Enter your name.
yang guo
Enter your favorite dessert.
I have some delicious guo for you, yang.
每次读取一行字符串
(改进上面的程序)
#include <iostream>
int main()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin.getline(name, ArSize);
cout << "Enter your favorite dessert:\n";
cin.getline(dessert,ArSize);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
Enter your name:
yang guo
Enter your favorite dessert:
fish
I have some delicious fish for you, yang guo.
面向行的输入:get()
#include <iostream>
int main()
{
using namespace std;
const int Arsize = 20;
char name[Arsize];
char dessert[Arsize];
cout << "Enter your name:\n";
cin.get(name,Arsize).get();//使用get()的方式是将两个类成员拼接起来
//是由于cin.get(name,Arsize)返回一个cin对象,
//该对象随后将被用来调用get()函数,其效果与两次调用cin.getline()相同
cout << "Enter your favorite dessert :\n";
cin.get(dessert,Arsize).get();
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
Enter your name:
yang guo
Enter your favorite dessert :
fish
混合输入字符串和数字
#include <iostream>
int main()
{
using namespace std;
cout << "What year was your house built?\n";
int year;
cin >> year;
cin.get();//还可以用(cin >> year).get(),也就是表达式cin >> year返回cin对象,将调用拼接起来
cout << "What is its street address?\n";
char address[80];
cin.getline(address,80);
cout << "Year bulit " << year << endl;
cout << "Address: " << address << endl;
cout << "Done!\n";
return 0;
}
What year was your house built?
1966
What is its street address?
xian
Year bulit 1966
Address: xian
Done!
string类
简介
#include <iostream>
#include <string>
int main()
{
using namespace std;
char charr1[20];//定义了一个字符数组
char charr2[20] = "jaguar";
string str1;//定义了一个字符串变量,在c++中也被成为string对象
string str2 = "panther";
cout << "Enter a kind of feline: ";
cin >> charr1;
cout << "Enter another kind of feline: ";
cin >> str1;
cout << "Here are some felines :\n";
cout << charr1 <<" " << charr2 << " "
<< str1 << " " << str2
<<endl;
cout << "The third letter in " << charr2 << "is "
<< charr2[2] << endl;
cout << "The third letter in " << str2 << " is "
<< str2[2] << endl;
return 0;
}
Enter a kind of feline: ocelot
Enter another kind of feline: tiger
Here are some felines :
ocelot jaguar tiger panther
The third letter in jaguaris g
The third letter in panther is n
赋值、拼接和附加
#include <iostream>
#include <string>
int main()
{
using namespace std;
string s1 = "penguin";
string s2 , s3;
cout << "You can assign one string object to another :s2 = s1\n";
s2 = s1;
cout << "s1: " << s1 << ", s2: " << s2 << endl;
cout << "You can assign a C-style string to a string object.\n";
cout << "s2 = \"buzzard\"\n";
s2 = "buzzard";
cout << "s2:" << s2 << endl;
cout << "You can concatenate strings: s3 = s1 + s2\n";
s3 = s1 + s2;
cout << "s3: " << s3 << endl;
cout << "You can append string.\n";
s1 += s2;
cout << "s1 += s2 yield s1 = " << s1 <<endl;
s2 += "for a day";
cout << "s2 += \" for a day \" yield s2 = " << s2 <<endl;
return 0;
}
You can assign one string object to another :s2 = s1
s1: penguin, s2: penguin
You can assign a C-style string to a string object.
s2 = "buzzard"
s2:buzzard
You can concatenate strings: s3 = s1 + s2
s3: penguinbuzzard
You can append string.
s1 += s2 yield s1 = penguinbuzzard
s2 += " for a day " yield s2 = buzzardfor a day
string类的其他操作
#include <iostream>
#include <string>
#include <cstring>
int main()
{
using namespace std;
char charr1[20];
char charr2[20] = "jaguar";
string str1;
string str2 = "panther";
str1 = str2;//字符串变量直接可以相互赋值
strcpy(charr1, charr2);
//把字符数组charr2赋值给字符数组charr1,函数strcpy()可以将字符串赋值给字符数组
str1 += "paste";
strcat(charr1, "juice");//可使用strcat函数将字符串附加到字符数末尾
int len1 = str1.size();//确定字符串中字符数的方法一:字符串对象.size这个类方法()
int len2 = strlen(charr1);//使用函数strlen函数
cout << "The string " << str1 << " contains "
<< len1 << " The characters.\n";
cout << "The string " << charr1 << " contains "
<< len2 << " characters.\n";
return 0;
}
The string pantherpaste contains 12 The characters.
The string jaguarjuice contains 11 characters.
string类 I/O
#include <iostream>
#include <string>
#include <cstring>
int main()
{
using namespace std;
char charr[20];
string str;
cout << "Length of string in charr before input: "
<< strlen(charr) <<endl;
cout << "Length of string in str before input:"
<< str.size() <<endl;
cout << "Enter a line of text:\n";
cin.getline(charr,20);//cin是iostream类的一个对象,句点表示getline是iostream类的一个方法
cout << "You entered: " << charr << endl;
cout << "Enter another line of text:\n";
getline(cin, str);//此处无句点,说明getline不是某一个类的类方法,它将cin作为参数,指出到哪里去查找输入
//此处也没有指出长度,这是因为string将根据字符串的长度自动调整自己的大小。
cout << "You enterd: " << str << endl;
cout << "Length of string in charr after input: "
<< strlen(charr) << endl;
cout << "Length of string in str after input: "
<< str.size() << endl;
return 0;
}
Length of string in charr before input: 3
Length of string in str before input:0
其他形式的字符串字面值
#include <iostream>
#include <string>
#include <cstring>
int main()
{
using namespace std;
wchar_t title[] = L"guo";//定义了一个wchar_t
char16_t name[] = u"guo";//定义了一个char_16_string
char32_t car[] = U"guo";//定义了一个char_32_string
wcout << title <<endl;
return 0;
}