1.
//原因:
//x和y是值传递,只需将x和y改为引用传递
#include <iostream>
using namespace std;
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
int main()
{
int x1 = 25;
int y1 = 35;
cout << "x1 = " << x1 << " y1 = " << y1 << endl;
swap(x1, y1);
cout << "x1 = " << x1 << " y1 = " << y1 << endl;
return 0;
}
2.
//模板函数count
#include <iostream>
using namespace std;
template <class T>
int count(T *array, const int size, const T &value)
{
int num = 0;
for (int i = 0; i < size; ++i)
{
if (array[i] == value)
{
++num;
}
}
return num;
}
int main()
{
int array[10] = { 12,17,54,32,66,89,17,49,17,65 };
const int size = sizeof(array) / sizeof(array[0]);
const int value = 17;
cout << count(array, size, value) << endl;
return 0;
}
3.
//模板函数fill
#include <iostream>
using namespace std;
template <class T>
void fill(T* array, const int size)
{
for (int i = 0; i < size; ++i)
{
cin >> array[i];
}
}
int main()
{
const int size = 5;
int array[size];
fill(array, size);
return 0;
}
4.
//模板函数inner_product
#include <iostream>
using namespace std;
template <class T>
T inner_product(T* array1, T* array2, const int size)
{
T result = 0;
for (int i = 0; i < size; ++i)
{
result += array1[i] * array2[i];
}
return result;
}
int main()
{
const int size = 10;
int array1[size] = { 1,2,3,4,5,6,7,8,9,10 };
int array2[size] = { 10,9,8,7,6,5,4,3,2,1 };
cout << "The result is:" << inner_product(array1, array2, size) << endl;
return 0;
}
5.
//模板函数iota
#include <iostream>
using namespace std;
template <class T>
void iota(T* array, const int size, const int value)
{
for (int i = 0; i < size; ++i)
{
array[i] = value + i;
}
}
int main()
{
const int size = 10;
const int value = 1;
int array[size] = { 0 };
iota(array, size, value);
for (int i = 0; i < size; ++i)
{
cout << array[i] << " ";
}
return 0;
}
6.
//模板函数is_sorted
#include <iostream>
using namespace std;
template <class T>
bool is_sorted(T* array, const int size)
{
bool result = true;
if (size == 1)
{
return true;
}
for (int i = 1; i < size; ++i)
{
if (array[i] < array[i - 1])
{
result = false;
break;
}
}
return result;
}
int main()
{
const int size = 10;
int array[size] = { 0,1,2,3,4,5,6,7,8,9 };
if (is_sorted(array, size))
{
cout << "The array id ordered!";
}
else
{
cout << "The array id unordered!";
}
return 0;
}
7.
//模板函数is_sorted
#include <iostream>
using namespace std;
template <class T>
int mismatch(T* array1, T* array2, const int size)
{
for (int i = 0; i < size; ++i)
{
if (array1[i] != array2[i])
{
return i;
}
}
return size;
}
int main()
{
const int size = 10;
int array1[size] = { 0,1,2,3,4,5,6,7,8,9 };
int array2[size] = { 0,1,2,3,4,5,6,7,8,10 };
if (mismatch(array1, array2, size) == size)
{
cout << "The two arrays are the same!";
}
else
{
cout << "Minimum index:" << mismatch(array1, array2, size) << endl;
}
return 0;
}
8.
#include <iostream>
using namespace std;
int abc(int a, int b, int c) {}
float abc(int a, int b, int c) {} //abc重定义,无法重载仅按返回类型区分的函数
int main()
{
return 0;
}
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; //错误,参数类型(int,int,float)
cout << abc(1.0, 2.0, 3.0) << endl; //错误,参数类型(double,double,doule)
return 0;
}
|