P105
#include <iostream>
#include <string>
using namespace std;
string sa[10];
int ia[10];
int main()
{
int ia[] = { 1,2,3,4,5,6,7,8,9 };
auto iaa(ia);//auto iaa(&ia[0]);
cout << typeid(iaa).name() << endl;
system("pause");
return 0;
}
练习3.35:编写一段程序,利用指针将数组中的元素置为0。
#include <iostream>
using namespace std;
int main()
{
const int sz = 10; //常量sz作为数组的维度
int a[sz], i = 0;
//通过for循环为数组元素赋值
for (i = 0; i < 10; i++)
{
a[i] = i;
}
cout << "初始状态下数组的内容是:" << endl;
for (auto val : a)
{
cout << val << " ";
}
cout << endl;
int *p = begin(a); //令p指向数组首元素
while (p != end(a))
{
*p = 0; //修改p所指元素的值
p++; //p向后移动一位
}
cout << "修改后的数组内容是:" << endl;
//通过范围for循环输出数组的全部元素
for (auto val : a)
{
cout << val << " ";
}
cout << endl;
return 0;
}
练习3.36:编写一段程序,比较两个数组是否相等。再写一段程序,比较两个vector对象是否相等。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
const int sz = 5; //常量sz作为数组的维度
int a[sz], b[sz], i;
srand((unsigned) time(NULL)); //生成随机数种子
//通过for循环为数组元素赋值
for (i = 0; i < sz; i++)
{
//每次循环生成一个10以内的随机数并添加到a中
a[i] = rand() % 10;
}
cout << "系统数据已经生成,请输入您猜测的5个数字(0~9),可以重复:" << endl;
int uVal;
//通过for循环为数组元素赋值
for (i = 0; i < sz; i++)
{
if (cin >> uVal)
{
b[i] = uVal;
}
}
cout << "系统生成的数据是:" << endl;
for (auto val : a)
{
cout << val << " ";
}
cout << endl;
cout << "您猜测的数据是:" << endl;
for (auto val : b)
{
cout << val << " ";
}
cout << endl;
int *p = begin(a), *q = begin(b); //令p,q分别指向数组a和b的首元素
while (p != end(a) && q != end(b))
{
if (*p != *q)
{
cout << "您的猜测错误,两个数组不相等" << endl;
return -1;
}
p++; //p向后移动一位
q++; //q向后移动一位
}
cout << "恭喜您全都猜对了!" << endl;
return 0;
}
vector
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
const int sz = 5; //常量sz作为vector的容量
int i;
vector<int> a, b;
srand((unsigned) time(NULL)); //生成随机数种子
//通过for循环为数组元素赋值
for (i = 0; i < sz; i++)
{
//每次循环生成一个10以内的随机数并添加到a中
a.push_back(rand() % 10);
}
cout << "系统数据已经生成,请输入您猜测的5个数字(0~9),可以重复:" << endl;
int uVal;
//通过for循环为数组元素赋值
for (i = 0; i < sz; i++)
{
if (cin >> uVal)
{
b.push_back(uVal);
}
}
cout << "系统生成的数据是:" << endl;
for (auto val : a)
{
cout << val << " ";
}
cout << endl;
cout << "您猜测的数据是:" << endl;
for (auto val : b)
{
cout << val << " ";
}
cout << endl;
//令it1,it2分别指向vector对象a和b的首元素
auto it1 = a.cbegin(), it2 = b.cbegin();
while (it1 != a.cend() && it2 != b.cend())
{
if (*it1 != *it2)
{
cout << "您的猜测错误,两个vector不相等" << endl;
return -1;
}
it1++; //p向后移动一位
it2++; //q向后移动一位
}
cout << "恭喜您全都猜对了!" << endl;
return 0;
}
练习3.39:编写一段程序,比较两个string对象.再编写一段程序,比较两个C风格字符串的内容。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1, str2;
cout << "请输入两个字符串:" << endl;
cin >> str1 >> str2;
if (str1 > str2)
{
cout << "第一个字符串大于第二个字符串" << endl;
}
else if (str1 < str2)
{
cout << "第一个字符串小于第二个字符串" << endl;
}
else
{
cout << "两个字符串相等" <<endl;
}
return 0;
}
C风格的
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str1[80], str2[80];
cout << "请输入两个字符串:" << endl;
cin >> str1 >> str2;
//利用cstring头文件定义的strcmp函数比较大小
auto result = strcmp(str1, str2);
switch (result)
{
case 1:
cout << "第一个字符串大于第二个字符串" << endl;
break;
case -1:
cout << "第一个字符串小于第二个字符串" << endl;
break;
case 0:
cout << "两个字符串相等" <<endl;
break;
default:
cout << "未定义的结果" << endl;
break;
}
return 0;
}
练习3.40:编写一段程序,定义两个字符数组并用字符串字面值初始化它们;接着再定义一个字符数组存放前两个数组连接后的结果。使用strcpy和strcat把前两个数组的内容拷贝到第三个数组中。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str1[] = "Welcome to ";
char str2[] = "C++ family!";
//利用strlen函数计算两个字符串的长度,并求得结果字符串的长度
char result[strlen(str1) + strlen(str2) - 1];
strcpy(result, str1); //把第一个字符串拷贝到结果字符串中
strcat(result, str2); //把第二个字符串拼接到结果字符串中
cout << "第一个字符串是:" << str1 << endl;
cout << "第二个字符串是:" << str2 << endl;
cout << "拼接后的字符串是:" << result << endl;
return 0;
}
二位数组:数组的数组
练习3.43:编写3个不同版本的程序,令其均能输出ia的元素。
版本1使用范围for语句管理迭代过程;
版本2和版本3都使用普通的for语句,其中版本2要求用下标
运算符,版本3要求用指针。
#include <iostream>
#include <string>
using namespace std;
string sa[10];
int ia[10];
int main()
{
int ia[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
for (auto (&row)[4] : ia)
{
for (auto col : row)
{
cout << col << " ";
}
}
cout << endl;
for (auto p = begin(ia); p != end(ia); ++p)
{
for (auto q = begin(*p); q != end(*p); ++q)
{
cout << *q << " ";
}
}
cout << endl;
for (auto p = ia; p != ia + 3; ++p)
{
for (auto q = *p; q != *p + 4; ++q)
{
cout << *q << " ";
}
}
cout << endl;
system("pause");
return 0;
}
练习3.44:改写上一个练习中的程序,使用类型别名来代竹循环控制变量的类型。
#include <iostream>
#include <string>
using namespace std;
string sa[10];
int ia[10];
int main()
{
int ia[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
using int_array = int[4];
for (int_array *p = ia; p != ia + 3; ++p)
{
for (int *q = *p; q != *p + 4; ++q)
{
cout << *q << " ";
}
}
cout << endl;
system("pause");
return 0;
}