获得数组的大小由指针C ++ [英] getting size of array from pointer c++

查看:117
本文介绍了获得数组的大小由指针C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个简单的函数,返回数组中的最大整数。我遇到的问题是寻找数组中元素的数目。

I am writing a simple function that returns the largest integer in an array. The problem I am having is finding the number of elements in the array.

下面是函数头:

int largest(int *list, int highest_index)

我怎样才能在数组'名单'的整数数量。

How can I get the number of integers in the array 'list'.

我曾尝试以下方法:

int i = sizeof list/sizeof(int); //returns incorrect value
int i = list.size(); // does not compile

任何帮助将大大AP preciated!

Any help will be greatly appreciated!

推荐答案

有没有办法做到这一点。这是一个好理由(在许多)使用矢量而非数组。但是,如果你必须使用一个数组,那么你必须将数组作为参数的大小传递给你的函数

There's no way to do that. This is one good reason (among many) to use vectors instead of arrays. But if you must use an array then you must pass the size of the array as a parameter to your function

int largest(int *list, int list_size, int highest_index)

在C数组++都很差,你越快学会使用向量更容易,你会发现的东西。

Arrays in C++ are quite poor, the sooner you learn to use vectors the easier you will find things.

这篇关于获得数组的大小由指针C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆