简体   繁体   English

我不知道如何填充数组

[英]I can't figure out how to fill an array

I am trying to fill a given array with a passed in value so if I wanted an array to be all 12's it would simply replace all the elements with 12s. 我试图用传入的值填充给定的数组,所以如果我想让数组全部为12,则只需将所有元素替换为12s。 The prototype I have for this function looks like this: 我具有此功能的原型如下所示:

void fill(T *left, T *end, T fill)

The driver for this function looks like this: 该函数的驱动程序如下所示:

static void TestFill1(void)
{
   cout << "***** Fill1 *****" << endl;
   int i1[10];
   int size = 10;

   fill(i1, i1 + size, 12);
   display(i1, i1 + size);
}

I am having a problem where I am given an array that is uninitialized. 我遇到了一个未初始化的数组的问题。 Previously in the assignment I was going through the array until the end. 在以前的作业中,我要遍历数组直到最后。 In this case I am given an uninitialized array which makes my T *end the same as T *left. 在这种情况下,我得到一个未初始化的数组,这使我的T * end与T * left相同。 I'm not familiar with a way to go through the passed in array. 我不熟悉通过传入数组的方法。

I was trying something that looked like this: 我正在尝试看起来像这样的东西:

template <typename T>
void fill(T *left, T *end, T fill)
{
  int i =  sizeof(*left) / sizeof(*(left + 0));

  while(*(left + i) != *end)
  {
    *(left + i) = fill;
    ++i;
  }
}

I'm not allowed to use subscripts or for loops for this assignment also, #include is off limits same with std::vector. 我也不允许为此任务使用下标或for循环,#include与std :: vector的限制相同。

The variable i , which represents the offset with respect to the first element, should start at zero: 代表相对于第一个元素的偏移量的变量i应该从零开始:

int i = 0;

The loop condition is checking whether the value of the array element is equal to the value of the array element at the end. 循环条件是检查数组元素的是否最后等于数组元素的

while(*(left + i) != *end)

The correct version is the following: 正确的版本如下:

while(left + i != end)

which checks if the pointer (left + i) has reached the end. 它检查指针 (left + i)是否到达末尾。

Your statement 您的声明

int i =  sizeof(*left) / sizeof(*(left + 0));

might not do, what you think it does. 可能不会,您认为会做。

The sizeof() function doesn't work on plain pointers the same way as for array declarations: sizeof()函数不适用于普通指针,而不适用于数组声明:

size_t s = sizeof(*left); // Will evaluate to sizeof(T)

while

int i1[10];
size_t s = sizeof(i1); // Will evaluate to sizeof(int) * 10

Your code can be simply fixed as follows: 您的代码可以简单地固定如下:

template <typename T>
void fill(T *left, T *end, T fill) {
    T* cur = left;

    while(cur < end) {
       *cur  = fill;
       ++cur;
    }
}

http://www.cplusplus.com/reference/vector/vector/assign/ http://www.cplusplus.com/reference/vector/vector/assign/

#include <vector>

int main ()
{
  std::vector<int> first;

  first.assign (10,12);  // 10 ints with a value of 12
  return 0;
}

This is how real men do it ™. 这就是真正的男人™。 lol sorry I couldn't resist. 大声抱歉,我无法抗拒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道如何打印数组,反过来,我也不知道如何交换数组中的元素 - I can't figure out how to print arrays and in turn, I can't figure out how to swap elements in an array 无法弄清楚如何将结构数组传递给函数 - Can't figure out how to pass struct array to function 我不知道如何重复整个程序 - I can't figure out how to repeat this whole program 简单的循环我不知道 - Simple for loop I can't figure out 编译错误我想不通 - Compile error i can't figure out 无法弄清楚为什么我的 if 语句不起作用:( if array[i] &lt;&lt; average) - Can't figure out why my if statement isn't working: ( if array[i] << average) 无法从数组中打印信息时,无法找出如何遍历存储在类中的数组? - Having trouble printing the information from an array, can't figure out how to run through the array that was stored in a class? Domino程序。 我不知道如何将向量拉成空白以将其打印出来 - Dominoes program. I can't figure out how to pull my vector into a void to print it out 我有一个未声明的标识符错误,我无法弄清楚 - I have an undeclared identifier error, that I can't figure out 我想使用计数器来查找用户输入的原始号码,但我不知道如何 - I want to use a counter to find the original number that the user entered, but I can't figure out how
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM