3.32
数组拷贝:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void main()
{
int arr[10];
for (int i=0; i < 10; ++i)
arr[i] = i;
for (auto u : arr)
cout << u << " ";
int arr1[10];
for (auto i = 0; i < 10; ++i)
arr1[i] = arr[i];
cout << '\n' << "拷贝之后的数组内容如下:" << endl;
for (auto u : arr1)
cout << u << " ";
system("pause");
}
利用vector重写:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void main()
{
vector<int> arr,arr1;
for (int i = 0; i < 10; ++i)
arr.push_back(i);
arr1 = arr;
for (auto u : arr)
cout << u << " ";
cout << '\n' << "拷贝之后的数组内容如下:" << endl;
for (auto u : arr1)
cout << u << " ";
system("pause");
}
3.33
3.35
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void main()
{
int arr[] = { 0,1,2,3,4,5,6,7,8,9 };
int *p1 = arr;
for (auto i : arr)
{
*p1 = 0;
++p1;
}
for (auto i : arr)
cout << arr[i] << " ";
system("pause");
}
3.36
比较两个数组是否相等
#include <iostream>
using namespace std;
void main()
{
int arr[] = {0,1,2,3,4,5,6,7,8,9}, arr1[] = {0,-1,2,3,4,5,6,7,8,9};
int flag = 0;
auto a = end(arr) - begin(arr), b = end(arr1) - begin(arr1);//获取两个数组的长度
//判断数组是否相等
if (a == b)
{
for (int i = 0; i < a; ++i)
{
if (arr[i] == arr1[i]) {
++flag;
}
}
if (flag == a)
cout << "两个数组相等" << endl;
else
cout << "两个数组不相等" << endl;
}
else
cout << "两个数组不相等" << endl;
system("pause");
}
比较两个vector是否相等
#include <iostream>
#include <vector>
using namespace std;
void main()
{
vector<int> arr = { 0,1,2,3,4,5,6,7,8,9 }, arr1 = { 0,1,-2,3,4,5,6,7,8,9 };
if (arr==arr1)
cout << "两个vector相等" << endl;
else
cout << "两个vector不相等" << endl;
system("pause");
}
3.37
while(*cp)会在ca数组(字符类型)中找有没有'\0'的字符,只有当遇到'\0'时while循环才会停止,题中那样的定义方式是不包含'\0'的,所以程序会一直找,直到找到'\0'为止,所以输出除了hello外还会输出其他的字符,每次运行结果都不一样。
3.38
两个指针相加没有任何意义,并且也是非法的。
3.39
比较两个string对象
#include <iostream>
#include <string>
using namespace std;
void main()
{
string str1 = "A string example";
string str2 = "A different string";
if (str1 == str2)
cout << "Equal" << endl;
else
cout << "Not Equal" << endl;
system("pause");
}
比较两个C风格字符串(要用strcmp函数)
#include <iostream>
using namespace std;
void main()
{
char str1[] = "A string example";//这样定义会自动添加表示字符串结束的空字符
char str2[] = "A different string";
if (strcmp(str1, str2) == 0)
cout << "Equal" << endl;
else
cout << "Not Equal" << endl;
system("pause");
}
3.40
#include <iostream>
using namespace std;
void main()
{
char a[] = "Hello,", b[] = "world";
char c[100];
strcpy_s(c, a);
strcat_s(c, " ");
strcat_s(c, b);
cout << c << endl;
system("pause");
}
3.41
#include <iostream>
#include <vector>
using namespace std;
void main()
{
int a[] = { 0,1,2,3,4,5 };
//vector<int> b(begin(a), end(a));//用整个数组初始化vector对象
vector<int> b(a + 1, a + 3);//用a[1]与a[2]初始化vector对象
for(auto i:b)
cout << i << endl;
system("pause");
}
3.42
#include <iostream>
#include <vector>
using namespace std;
void main()
{
vector<int> a = { 1,2,3,4,5 };
int b[5];
for (int i = 0; i < 5;i++)
{
b[i] = a[i];
}
for(auto i :b)
cout << i << " ";
system("pause");
}
3.43
#include <iostream>
using namespace std;
void main()
{
int ia[3][4] = { {1},{2},{3} };
//范围for语句
for (int(&row)[4] : ia) //使用范围for语句时,除了内存的所有层控制变量均为引用类型
{
for (int col : row)
cout << col << " ";
cout << endl;
}
//普通for语句,利用下标运算符
for (int row = 0; row < 3; ++row)
{
for (int col = 0; col < 4; ++col)
cout << ia[row][col] << " ";
cout << endl;
}
//普通for语句,利用指针
for (int(*p)[4] = ia; p != ia + 3; ++p)
{
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
}
system("pause");
}
3.44
#include <iostream>
using namespace std;
void main()
{
int ia[3][4] = { {1},{2},{3} };
using int_array = int(&)[4];
//范围for语句
for (int_array row : ia)//使用范围for语句时,除了内存的所有层控制变量均为引用类型
{
for (int col : row)
cout << col << " ";
cout << endl;
}
//普通for语句,利用下标运算符
for (int row = 0; row < 3; ++row)
{
for (int col = 0; col < 4; ++col)
cout << ia[row][col] << " ";
cout << endl;
}
//普通for语句,利用指针
using int_arrayR = int(*)[4];
using int_arrayRR = int*;
for (int_arrayR p = ia; p != ia + 3; ++p)
{
for (int_arrayRR q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
}
system("pause");
}
3.45
#include <iostream>
using namespace std;
void main()
{
int ia[3][4] = { {1},{2},{3} };
//范围for语句
for (auto &row : ia)//使用范围for语句时,除了内存的所有层控制变量均为引用类型
{
for (auto col : row)
cout << col << " ";
cout << endl;
}
//普通for语句,利用下标运算符
for (auto row = 0; row < 3; ++row)
{
for (auto col = 0; col < 4; ++col)
cout << ia[row][col] << " ";
cout << endl;
}
//普通for语句,利用指针
for (auto p = ia; p != ia + 3; ++p)
{
for (auto *q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
}
system("pause");
}