影响 -1 pac 当我 clas mes spa 定义
返回值是数组的a[0:n-1]的数组个数。
知识点:数组的两个特殊性质对我们定义和使用作用在数组上的函数有影响,这两个性质分别是:不允许拷贝数组以及使用数组时(通常)会将其转换成指针。因为不能拷贝数组,所以我们无法以值传递的方式使用数组参数。因为数组会被转换成指针,所以当我们为函数传递一个数组时,实际上传递的饰指向数组首元素的指针。
ex:
void print(const int*);
void print(const int[]);
void print(const int[10])
答案:
- template <class T>
- T count(const T *beg,const T *end)
- {
- int n = 0;
- while (beg != end)
- {
- *beg++;
- n++;
- }
- return n;
- }
能运行代码如下:
- #include < iostream >
- using namespace std;
- int count(const int * beg, const int * end) {
- int n = 0;
- while (beg != end) { * beg++;
- n++;
- }
- return n;
- }
- int main() {
- int a[] = {
- 1,
- 2,
- 3,
- 4
- };
- cout << count(begin(a), end(a)) << endl;
- return 0;
- }
编写一个模版函数count
来源: http://www.bubuko.com/infodetail-2283592.html