1.
(1)没有交换的原因:是swap函数内的局部变量x,y进行了交换,并不是传入函数的实参进行了交换
(2)更改为
#include <iostream>
using namespace std;
void swap2(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int main()
{
int x1 = 55;
int y1 = 89;
cout << "x1:" << x1 << " y1:" << y1 << endl;
swap2(x1, y1);
cout << "x1:" << x1 << " y1:" << y1 << endl;
}
2.专用模板统计数组的个数
#include <iostream>
using namespace std;
template <typename T>
int count_array_size(const T* arr_begin, const T* arr_end) {
int cnt = 0;
while (arr_begin != arr_end) {
++arr_begin;
++cnt;
}
return cnt;
}
int main() {
int arr[5]{ 0, 1, 2, 3, 4 };
char array[] = { '0','1','2','3','4','6' };
int arr_number = count_array_size(begin(arr), end(arr));
int array_number = count_array_size(begin(array), end(array));
cout << "Count arr" << arr_number << endl;
cout << "Count array: " << array_number << endl;
return 0;
}
3.专用模板给数组赋值
#include <iostream>
using namespace std;
template <typename T>
//数组赋值
void fill_array(T &arr, int num)
{
for (int i = 0; i < num; i++)
{
cin >> arr[i];
}
}
int main() {
int arr[5];
fill_array(arr, 5);
cout << "After the assignment:" << endl;
for(int i = 0; i < 5 ; i++)
{
cout << arr[i] << endl;
}
return 0;
}
PS:不太好的地方就是需要自己传入赋值的个数,如果调用第2问结果可以不用自己输入结果,也在找更好的实现方式
4.模板函数求返回值
#include <iostream>
using namespace std;
template <class T1, class T2>
T1 inner_product(T1 a[], T2 b[], const T1* arr_begin, const T1* arr_end)
{
T1 result = 0;
int i = 0;
while (arr_begin!=arr_end)
{
result += a[i] * b[i];
arr_begin++;
i++;
}
return result;
}
int main() {
int arr[5] = { 1,2,3,4,5 };
int arr1[5] = { 1,2,3,4,5 };
int result;
result = inner_product(arr, arr1,begin(arr),end(arr));
cout << "The Result is: "<< result << endl;
return 0;
}
5.模板函数iota
#include <iostream>
using namespace std;
template <class T,class T1>
void iota(T &arr, T1 value,int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = i + value;
}
}
int main() {
int arr[5] = { 0 };
iota(arr, 5, 5);
cout << "After assignment: "<< endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
return 0;
}
6.is_sorted
#include <iostream>
using namespace std;
template <class T>
bool is_sorted(T arr[], const T * array_begin, const T * array_end)
{
int i = 0;
bool result = true;
bool order = true; //默认为正序
while (array_begin != array_end)
{
if (i == 1)
{
if (arr[1] < arr[0])
{
order = false;
}
}
if (i > 1)
{
if (order)
{
if (arr[i] < arr[i - 1])
{
result = false;
break;
}
}
else
{
if (arr[i] > arr[i - 1])
{
result = false;
break;
}
}
}
i++;
array_begin++;
}
return result;
}
int main() {
int arr[5] = { 9,8,6,5,2 };
if (is_sorted(arr,begin(arr),end(arr)))
{
cout << "orderly" << endl;
}
else
{
cout << "out of order" << endl;
}
return 0;
}
7.mismatch
#include <iostream>
using namespace std;
template <class T,class T1>
int mismatch(T arr[],T1 arr1[], const T * array_begin, const T * array_end)
{
int i = 0;
while (array_begin != array_end)
{
if (arr[i] != arr1[i])
{
return i;
}
i++;
array_begin++;
}
return -1;
}
int main() {
char arr[5] = { 'h' ,'e','l','l','o'};
char arr1[5] = { 'h' ,'e','l','l','o' };
if (mismatch(arr, arr1, begin(arr), end(arr)) != -1)
{
cout << "匹配的最小索引是:"<< mismatch(arr, arr1, begin(arr), end(arr)) << endl;
}
else
{
cout << "两个数组相同" << endl;
}
return 0;
}
8.
不是,他们是拥有相同签名的重载函数
9.
代码如下:
#include <iostream>
using namespace std;
int abc(int a, int b, int c)
{
return a + b + c;
}
float abc(float a, float b, float c)
{
return a + b + c;
}
int main() {
cout << abc(1, 2, 3) << endl;
cout << abc(1.0F, 2.0F, 3.0F) << endl;
cout << abc(1, 2, 3.0F) << endl;
cout << abc(1.0, 2.0, 3.0) << endl;
return 0;
}
最后两个不行
PS:如有错误,欢迎指正讨论