Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 存储数组索引值C++;_C++_Arrays_Indexing - Fatal编程技术网

C++ 存储数组索引值C++;

C++ 存储数组索引值C++;,c++,arrays,indexing,C++,Arrays,Indexing,我目前声明了一个浮点数组,并用一系列值初始化,例如3.2、4.4、9.8。while循环将数组的最高值元素添加到可变的highestVal中。这将正常工作,并输出数组中最高的元素。我试图找到一种方法来存储最高元素的索引,即如果值为3.4,我还想在数组中存储该值的索引。谁能告诉我存储这个索引值的基本方法是什么 int const MAXVALUES = 10; int counter = 0; float floatPointNumber[MAXVALUES] = { 1.3, 9.2, 2.2,

我目前声明了一个浮点数组,并用一系列值初始化,例如3.2、4.4、9.8。while循环将数组的最高值元素添加到可变的highestVal中。这将正常工作,并输出数组中最高的元素。我试图找到一种方法来存储最高元素的索引,即如果值为3.4,我还想在数组中存储该值的索引。谁能告诉我存储这个索引值的基本方法是什么

int const MAXVALUES = 10;
int counter = 0;
float floatPointNumber[MAXVALUES] = { 1.3, 9.2, 2.2, 4.4, 2.5, 2.7, 9.2, 7.9, 9.2, 9.6 };
int index = -1;
float highestArrayVal = 0;

while (counter < MAXVALUES)
{
    cout << floatPointNumber[counter] << endl;

    while (floatPointNumber[counter]>highestArrayVal)
    {
        highestArrayVal = floatPointNumber[counter];

        //index = floatPointNumber[counter];
        break;
    }
    counter = counter + 1;
}

cout << "Highest Array Value " << highestArrayVal << endl;
cout << "Index of this element " << index << endl;

return 0;
int const MAXVALUES=10;
int计数器=0;
float floatPointNumber[MAXVALUES]={1.3,9.2,2.2,4.4,2.5,2.7,9.2,7.9,9.2,9.6};
int指数=-1;
浮动高度ArrayVal=0;
while(计数器<最大值)
{

cout这实际上非常简单,但是您将得到一个指向元素的迭代器,而不是实际的索引

const auto it = max_element(begin(floatingPointNumber), end(floatingPointNumber));
这个迭代器本质上是一个指针。例如,您可以找到最大元素的值,如下所示:

const auto highestArrayVal = *it;
由于
它是一个指针,您可能不再需要精确的索引,但如果仍然需要,您可以执行以下操作:

const auto index = distance(begin(floatingPointNumber), it);

显示您的代码,我们将把这一行添加到您的循环中。:)如果您还没有这样做,请花一些时间阅读,尤其是名为和的部分。另外,请。您可能还想学习如何创建一个。@user3057164单击链接。您知道当
floatPointNumber[counter]时,哪个其他变量恰好包含此索引吗
最高的数字是多少?