【问题标题】:How can I assign an int array to linkedlist struct?如何将 int 数组分配给linkedlist 结构?
【发布时间】:2019-08-02 00:49:52
【问题描述】:

您好,我是 C++ 的初学者,我无法解决这个问题。我有一个节点链表,该节点包含 int 数组的数据和指向下一个节点的指针。

struct node {
   unsigned int numbers[6];
   node* next;
};

我也有一堂课:

private:
   ticket* ticketListHead;
   ticket* ticketListTail;

在公共方法中:

public:
    void newNode() {

       int arr[6];

       for(int i = 0; i < 6; ++i) {
           arr[i] = ( std::rand() % 49 ) + 1;
       }

       node *temp = new node;
       temp->numbers=arr;
       temp->next=NULL;
}

我认为问题在于temp-&gt;numbers=arr 行,因为我认为不能像在 C++ 中那样分配数组。在这种情况下,我不确定如何解决问题,我尝试在网上查找。一些帮助将不胜感激!

【问题讨论】:

  • 你可以使用std::array吗?
  • 我相信是的。我会使用什么方法?
  • 警告:使用rand() is highly problematic,我们强烈建议您使用合适的random number generator facility in the Standard Library,以产生高质量的随机值。您使用time(NULL) 作为随机数种子意味着如果在同一秒内运行,这将产生相同的结果,并且在许多平台上rand()barely random at all
  • @Samm 只需将循环移动到newNode() 的末尾,直接写到temp-&gt;numbers[i] 而不是arr[i]
  • 为什么node 没有构造函数?它可能应该避免错误地初始化它或忘记填充next 等。

标签: c++


【解决方案1】:

欢迎使用 C++,您正在迈出通往更光明世界的第一步,真是太棒了!您将要研究的是 C++ 容器类,它们将使您摆脱自己管理内存的业务。最接近您的代码的是 std::list 和 std::vector。我将忽略随机数播种,这是一个在其他地方更好讨论的复杂主题。

#include <vector>
#include <list>
#include <iostream>
#include <random>
#include <limits>
using namespace std;
struct node {
  node() : num(6) {
    numbers.reserve(num);
    for (int i = 0; i < num; ++i)
      numbers.push_back(random() % numeric_limits<int>::max());
  }
  vector<int> numbers;
  const int num;
};

int main(int argc, char** argv) {
  list<node> nodes;
  nodes.push_back(node()); // this replaces your newNode function
  nodes.push_back(node()); // another call to better illustrate
  for (auto mynode : nodes) {
    for (auto mynum : mynode.numbers)
      cout << mynum << " ";
    cout << endl;
  }
}

【讨论】:

    【解决方案2】:
    // Change node struct to this 
    struct node{
    unsigned int *numbers;
    node* next;
    };
    
    /*
    Remember that normal arrays are 
    implemented through pointers. So you 
    can use a pointer named numbers in 
    your node struct and it would behave 
    as an array.
    */
    
    // Change newNode() to this 
    void newNode() {
    int * arr = new int[6]; //Dynamic memory allocation
    for(int i = 0; i < 6; ++i) 
    { 
    arr[i] = ( std::rand() % 49 ) + 1; 
    }
    node *temp = new node; 
    temp->numbers = arr; 
    temp->next = NULL;
    }
    

    你明白了吗?

    【讨论】:

      【解决方案3】:

      您对无法分配数组的怀疑是正确的。您可以改为使用允许分配和定义的包装器类型

      using array = std::array<unsigned,6>;  // convenient short hand
      struct node {
          array numbers; 
          node* next = nullptr;              // ensure next defaults to null
      };
      

      当您的 newNode() 方法可能看起来像这样时

      node* newNode() {
          array arr;
          for(auto&x: arr)
              x = ( std::rand() % 49 ) + 1;   // std::rand() cannot be recommended
          auto temp = new node;
          temp->numbers = arr;
          return temp;
      }
      

      但是你可以通过直接写入新的node的数据来完全避免临时对象arr

      node* newNode() {
          auto temp = new node;
          for(auto&x: temp->numbers)
              x = ( std::rand() % 49 ) + 1;   // std::rand() cannot be recommended
          return temp;
      }
      

      顺便说一句,您不应该使用std::rand()(请参阅this post 和此presentation 了解原因)。而是使用&lt;random&gt; 提供的方法,当您的代码变为(另请参阅this answer

      template<typename Generator>
      node* newNode(Generator&rng) {
          std::uniform_int_distribution<unsigned> uni(1,49);
          auto temp = new node;
          for(auto&x: temp->numbers)
              x = uni(rng);             // guaranteed unbiased in [1,49] inclusive
          return temp;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-02
        • 2015-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-08
        • 1970-01-01
        相关资源
        最近更新 更多