Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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++非常陌生,主要使用java、JavaScript、C语言和Scala等语言。p>_C++_Arrays_List - Fatal编程技术网

链表数组C++; 首先,我对C++非常陌生,主要使用java、JavaScript、C语言和Scala等语言。p>

链表数组C++; 首先,我对C++非常陌生,主要使用java、JavaScript、C语言和Scala等语言。p>,c++,arrays,list,C++,Arrays,List,我正在尝试编写一个程序,从控制台获取一个数字,该程序将确定团队的数量(以链表的形式(例如numTeams=5;表示5个链表)),然后获取以下输入,以便将第一个值添加到第一个链表,然后将第二个值添加到第二个链表,依此类推,直到所有i值都分配给所有n个团队。此外,当所有链表都用相同数量的值填充时,它将在第一个链表处重新开始,并再次遍历,将值添加到每个列表中 我知道我必须制作一个链表数组,但我不确定如何以我所说的方式分配这些值 任何帮助都将不胜感激 以下是我到目前为止的情况: #include <

我正在尝试编写一个程序,从控制台获取一个数字,该程序将确定团队的数量(以链表的形式(例如numTeams=5;表示5个链表)),然后获取以下输入,以便将第一个值添加到第一个链表,然后将第二个值添加到第二个链表,依此类推,直到所有i值都分配给所有n个团队。此外,当所有链表都用相同数量的值填充时,它将在第一个链表处重新开始,并再次遍历,将值添加到每个列表中

我知道我必须制作一个链表数组,但我不确定如何以我所说的方式分配这些值

任何帮助都将不胜感激

以下是我到目前为止的情况:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;

class balanceTeams{

    // Struct inside the class LinkedList
    struct Node {

          int x;
          Node *next;

     };


public:

  // constructor
  balanceTeams(){

       head = NULL; // set head to NULL

    }

  // New value at the beginning of the list
  void addUnit(int val){

        Node *n = new Node();   // create new Node
        n->x = val;             // set value
        n->next = head;         // make the node point to the next node.
        //  If the list is empty, this is NULL
        head = n;               // head point at the new node.

     }

  // returns the first element in the list and deletes the Node.
  int popUnit(){

          Node *n = head;
          int ret = n->x;

          head = head->next;
          delete n;
          return ret;

     }

  //void swapUnit()
  //void moveUnit()

private:

    Node *head; // pointer to the first Node

  };


int main() {


    string line;
    int numTeams = 0;
    int lines = 0;
    vector<balanceTeams> vect;
    //int team_size = lines / numTeams;


  getline(cin, line);
      numTeams = atoi (line.c_str());
      vect.resize(numTeams);
      cout << "Num teams: " << numTeams << endl;


  while (getline(cin, line)) {

      cout << "Unit " << line << endl;

  }

  return 0;

}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
班级平衡小组{
//类LinkedList中的结构
结构节点{
int x;
节点*下一步;
};
公众:
//建造师
平衡小组(){
head=NULL;//将head设置为NULL
}
//列表开头的新值
无效添加单位(int val){
Node*n=new Node();//创建新节点
n->x=val;//设置值
n->next=head;//使节点指向下一个节点。
//如果列表为空,则为空
head=n;//新节点处的head点。
}
//返回列表中的第一个元素并删除节点。
int popUnit(){
节点*n=头部;
int-ret=n->x;
头部=头部->下一步;
删除n;
返回ret;
}
//无效
//无效移动单元()
私人:
Node*head;//指向第一个节点的指针
};
int main(){
弦线;
int numTeams=0;
int行=0;
向量向量;
//int团队大小=线/数团队;
getline(cin,line);
numTeams=atoi(line.c_str());
调整向量大小(numTeams);

cout您的代码中似乎缺少的是:

1) 某些错误检查。输入的团队数必须至少为一个。如果有人输入0或负值,则显示错误消息,然后退出

2) 初始化

size_t i=0;
在现有循环之前

3) 在循环内部,解析现有值,然后调用

vect[i].addUnit(new_value);
4) 为下一个迭代器增加循环末尾的索引

i=(i+1) % vect.size();

这似乎是。每次循环时,你会给下一个向量添加一个值。< /p>如果你对C++ C++有一点复杂,你可能会有帮助。你试图同时解决两个问题:将值分配给数组中的元素,并将值添加到链表中。数组中的元素。请格式化您的代码,以便我们其他人阅读。特别是,请修复缩进。
line.substr(0,line.find(“”))的预期效果是什么
?当前它看起来是一个不可操作的行,因为它没有改变
。在尝试实现该行时,我遇到了三个错误。我将在我的原始帖子中包含这三个错误,以便您能更好地看到它。编译错误是不言自明的。当您将std::string作为参数传递时,您将类方法声明为以int作为参数参数。您需要决定链接列表是存储int还是std::strings,然后相应地更改代码。这正是编译器错误所说的。错误文本的哪一部分您不理解?
i=(i+1) % vect.size();