1题:知识点:函数参数类型分三种:
1、传值参数:传入的是参数的拷贝,对拷贝的操作并不能改变参数本身。
2、引用参数:传入的是参数的引用,也即参数的类型别名,可以被函数修改。
3、常量引用参数:传入的是参数的引用,但不能被函数修改。
本题:x, y 为传值类型,在函数里面的值为 x, y 值的拷贝,因此交换函数 swap 改变不了x, y 的值。若想达到目的,只需将传值参数类型改为引用参数类型即可:
void swap(int &x, int &y)
2题:传入参数为3个,分别是数组指针,元素个数和检测值
#include<iostream>
using namespace std;
template<class T>
int count(T *arr, int n, const T &value)
{
int num = 0;
for (int i = 0; i < n; ++i)
{
if (arr[i] == value)
++num;
}
return num;
}
void main()
{
int arr[10] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
const int value = 0;
cout << count(arr, 10, value) << endl;
system("pause");
}
3题:同2,不过传入的不是元素数量,而是数组操作区间。
#include<iostream>
using namespace std;
template <class T>
void fill(T* a, int start, int end, const T& value)
{
for (int i = start; i < end; i++)
a[i] = value;
}
void main()
{
int arr[10] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
const int value = 0;
fill(arr, 5, 10, value); //从下标5开始填充值至数组结束。
for (auto a : arr)
cout << a << ends;
cout << endl;
system("pause");
}
4题:
#include<iostream>
using namespace std;
template <class T>
T inner_product(T* a, T* b, const int &n) //计算内积
{
T total = 0;
for (int i = 0; i < n; i++)
total += a[i] * b[i];
return total;
}
void main()
{
const int n = 10;
int arr1[n] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
int arr2[n] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
cout << inner_product(arr1, arr2, n) << endl;
system("pause");
}
5题:
#include<iostream>
using namespace std;
template <class T>
void iota(T* a, int n, const T& value)
{// Set a[i] = value + i, 0 <= i < n.
for (int i = 0; i < n; i++)
a[i] = value + i;
}
void main()
{
const int value = 1;
int arr[10] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
iota(arr, 10, value);
for (auto a : arr)
cout << a << ends;
cout << endl;
system("pause");
}
6题:
#include<iostream>
#include<string>
using namespace std;
template <class T>
bool is_sort(T* a, const int n, const string &compare)
{
int num = 0;
if (compare == "up")
for (int i = 0; i < n - 1; ++i)
if (a[i] < a[i + 1])
++num;
else
for (int i = 0; i < n - 1; ++i)
if (a[i] > a[i + 1])
++num;
return (num == n - 1);
}
void main()
{
const int n = 10;
const string compare = "up";
int arr[n] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
if(is_sort(arr, n, compare))
cout<<"This is a "<< compare << " array!"<< endl;
system("pause");
}
7题:有序数组,就是指其数组元素按升序或降序排列的数组。
#include<iostream>
using namespace std;
template <class T>
int mismatch(T* a, T* b, const int n)
{
for (int i = 0; i < n; i++)
if (a[i] != b[i])
return i;
return n;
}
void main()
{
const int n = 10;
int arr1[n] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int arr2[n] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
if (mismatch(arr1, arr2, n) == n) //全部匹配时
cout << "两数组一样!" << endl;
else
cout << "第一对不相等元素的索引: " << mismatch(arr1, arr2, n) << endl;
system("pause");
}
8题:知识点:一个函数的签名,是由其形参类型以及形参个数确定的,且任意两个同名函数不能有同样的签名。
本题中:两个函数签名一样。
9题:1-1 中签名为(int, int, int),1-2 中签名为(float, float, float)。本题中:
1)调用第1个
2)调用第2个
3)报错:多个实例与参数列表匹配。因为从 float 转为 int 和从 int 转为 float 都可以,造成二义性。
4)调用第2个
10题:注意抛出异常的类型。
#include<iostream>
using namespace std;
int abc(int a, int b, int c)
{
if (a < 0 & b < 0 & c < 0) //abc全为负,则抛出1
throw 1;
if (a == 0 & b == 0 & c == 0) //abc全为0,则抛出2
throw 2;
return a + b*c;
}
int main()
{
int a = 0, b = 0, c = 0;
try{ cout << abc(a, b, c) << endl; }
//try的处理块
catch (int e) //异常类型为int
{
cout << "The parameters to abc were " << a << ends << b << ends << c << endl;
cout << "An exception has been thrown\n";
cout << e << endl;
system("pause");
return 1;
}
return 0;
}