Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.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++_Console_Histogram - Fatal编程技术网

C++ 用于数组的循环索引?

C++ 用于数组的循环索引?,c++,console,histogram,C++,Console,Histogram,我想使用尽可能少的for循环输出直方图 int* histogram(int size, int* arr) { int bin[10] = {}; for (int i = 0; i < size; i++) { if (arr[i] >= 0 && arr[i] < 10) { bin[0]++; } else if (arr[i

我想使用尽可能少的for循环输出直方图

int* histogram(int size, int* arr)
{
    int bin[10] = {};

    for (int i = 0; i < size; i++)
    {

        if (arr[i] >= 0 && arr[i] < 10)
        {
            bin[0]++;       
        }
        else if (arr[i] >= 10 && arr[i] < 20)
        {
            bin[1]++;
        }

    return bin;
}
int*直方图(int大小,int*arr)
{
int-bin[10]={};
对于(int i=0;i=0&&arr[i]<10)
{
bin[0]++;
}
否则如果(arr[i]>=10&&arr[i]<20)
{
bin[1]++;
}
返回仓;
}
目前我正在输出直方图,如下所示:

cout << "0|";
    for (int j = 0; j < bin[0]; j++)
        cout << "*";
    cout << endl;
#include <iostream>

void histogram(int const size, int const * const arr, unsigned int const number_of_bins, float const bin_min, float const bin_max, int * output)
{
  float const binsize = (bin_max - bin_min)/number_of_bins;
  for (int i = 0; i < size; i++)
  {
    for(int j = 0; j < number_of_bins; ++j)
    {
      if (arr[i] >= bin_min + binsize*j && arr[i] < bin_min + binsize*(j+1))
      {
        output[j]++;
      }
    }
  }
}

int main(){
  int const number_of_bins = 10;
  float const bin_min = 0;
  float const bin_max = 100;
  int const size = 20;
  int const array[size] = {5,6,20,40,44,50,110,6,-1,51,55,56,20,50,60,80,81,0,32,3};
  int bin[number_of_bins] = {};
  histogram(size, array, number_of_bins, bin_min, bin_max, bin);
  for(int i = 0; i < number_of_bins; ++i)
  {
    std::cout << i << "|";
    for (int j = 0; j < bin[i]; j++)
    {
      std::cout << "*";
    }
    std::cout << std::endl;
  }
}

cout我将忽略直方图代码中的错误,因为它与优化直方图输出的问题并不相关。
有关错误(返回局部变量)的信息,请查看。
此外,您缺少一个大括号。在发布代码之前,请始终检查您的代码是否以最简单的形式编译和运行

您声明问题在于您使用的方法“冗长且烦人”,但不清楚您指的是代码的设计还是执行速度

演出 你能读到的最快的直方图是O(n),其中n是直方图中的存储箱数。从这个意义上说,你的代码在不进行微优化的情况下可以达到最快的速度

如果包括直方图的打印,则有O(n*m),其中m是每个箱子的平均条目数

编写直方图也是O(n*k),其中k是数组中的条目数,因为您必须找出每个值属于哪个bin

设计 如果您遇到的问题是代码过于臃肿和笨拙,那么使用更少的幻数并向函数添加更多参数,如下所示:

cout << "0|";
    for (int j = 0; j < bin[0]; j++)
        cout << "*";
    cout << endl;
#include <iostream>

void histogram(int const size, int const * const arr, unsigned int const number_of_bins, float const bin_min, float const bin_max, int * output)
{
  float const binsize = (bin_max - bin_min)/number_of_bins;
  for (int i = 0; i < size; i++)
  {
    for(int j = 0; j < number_of_bins; ++j)
    {
      if (arr[i] >= bin_min + binsize*j && arr[i] < bin_min + binsize*(j+1))
      {
        output[j]++;
      }
    }
  }
}

int main(){
  int const number_of_bins = 10;
  float const bin_min = 0;
  float const bin_max = 100;
  int const size = 20;
  int const array[size] = {5,6,20,40,44,50,110,6,-1,51,55,56,20,50,60,80,81,0,32,3};
  int bin[number_of_bins] = {};
  histogram(size, array, number_of_bins, bin_min, bin_max, bin);
  for(int i = 0; i < number_of_bins; ++i)
  {
    std::cout << i << "|";
    for (int j = 0; j < bin[i]; j++)
    {
      std::cout << "*";
    }
    std::cout << std::endl;
  }
}
输出:

0|*****
1|
2|**
3|*
4|**
5|*****
6|*
7|
8|**
9|

(另外,您的bug已经修复)

我将忽略直方图代码中的bug,因为它与优化直方图输出的问题并不相关。 有关错误(返回局部变量)的信息,请查看。 此外,您缺少一个大括号。在发布代码之前,请始终检查您的代码是否以最简单的形式编译和运行

您声明问题在于您使用的方法“冗长且烦人”,但不清楚您指的是代码的设计还是执行速度

演出 你能读到的最快的直方图是O(n),其中n是直方图中的存储箱数。从这个意义上说,你的代码在不进行微优化的情况下可以达到最快的速度

如果包括直方图的打印,则有O(n*m),其中m是每个箱子的平均条目数

编写直方图也是O(n*k),其中k是数组中的条目数,因为您必须找出每个值属于哪个bin

设计 如果您遇到的问题是代码过于臃肿和笨拙,那么使用更少的幻数并向函数添加更多参数,如下所示:

cout << "0|";
    for (int j = 0; j < bin[0]; j++)
        cout << "*";
    cout << endl;
#include <iostream>

void histogram(int const size, int const * const arr, unsigned int const number_of_bins, float const bin_min, float const bin_max, int * output)
{
  float const binsize = (bin_max - bin_min)/number_of_bins;
  for (int i = 0; i < size; i++)
  {
    for(int j = 0; j < number_of_bins; ++j)
    {
      if (arr[i] >= bin_min + binsize*j && arr[i] < bin_min + binsize*(j+1))
      {
        output[j]++;
      }
    }
  }
}

int main(){
  int const number_of_bins = 10;
  float const bin_min = 0;
  float const bin_max = 100;
  int const size = 20;
  int const array[size] = {5,6,20,40,44,50,110,6,-1,51,55,56,20,50,60,80,81,0,32,3};
  int bin[number_of_bins] = {};
  histogram(size, array, number_of_bins, bin_min, bin_max, bin);
  for(int i = 0; i < number_of_bins; ++i)
  {
    std::cout << i << "|";
    for (int j = 0; j < bin[i]; j++)
    {
      std::cout << "*";
    }
    std::cout << std::endl;
  }
}
输出:

0|*****
1|
2|**
3|*
4|**
5|*****
6|*
7|
8|**
9|

(另外,您的bug已经修复)

首先,您的程序是不正确的,因为正如所指出的,您从函数返回了一个指向局部变量的指针。要纠正这一点,您应该使用
std::array
std::vector

关于您的问题,如果您想要简洁的代码,请尝试以下方法:

#include <string>
#include <algorithm>
#include <iostream>
#include <array>

std::array<int, 10> bin;

// Fill your array here

int i = 0;
std::for_each(bin.begin(), bin.end(), [&i](auto x) 
{ 
    std::cout << i++ << "|" << std::string(x, '*') << std::endl;
});
#包括
#包括
#包括
#包括
std::数组bin;
//在这里填充你的数组
int i=0;
std::for_each(bin.begin()、bin.end()、[&i](自动x)
{ 

std::cout首先,您的程序是不正确的,因为正如所指出的,您返回了一个指向函数形式的局部变量的指针。要纠正这一点,您应该使用
std::array
std::vector

关于您的问题,如果您想要简洁的代码,请尝试以下方法:

#include <string>
#include <algorithm>
#include <iostream>
#include <array>

std::array<int, 10> bin;

// Fill your array here

int i = 0;
std::for_each(bin.begin(), bin.end(), [&i](auto x) 
{ 
    std::cout << i++ << "|" << std::string(x, '*') << std::endl;
});
#包括
#包括
#包括
#包括
std::数组bin;
//在这里填充你的数组
int i=0;
std::for_each(bin.begin()、bin.end()、[&i](自动x)
{ 

std::cout函数
histogram
在语法上是无效的。您的
histogram
函数有一个BUG,因为它返回一个局部变量的地址;通过指针的任何访问都会有UB。可以通过将int*arr更改为int-arr[]来修复这个问题吗@ HamHat实际上不能这么做。最好在C++中使用诸如<代码> STD::数组< /Cord>之类的类型。查看我的答案。函数<代码>直方图< /COD> AS是语法无效的。您的<代码>直方图函数有一个错误,因为它返回了本地变量的地址;通过指针的任何访问都将有UB。这可以通过将int*arr更改为int-arr[]来修复“HamHat实际上你不能这么做。最好使用C++中的代码,例如:代码> STD::数组< /Cord>。看看我的答案。谢谢。学到很多。我只想补充一下,我的答案比C++更适合C代码。我没有注意到问题中的标签,代码写得好像C.的C++更能看Aldarri。谢谢。你可以知道。我只想补充一下,我的答案比C++更适合C代码。我没有注意到问题中的标签,代码写得好像C.的C++更能看AlDrion的答案。