无法将数据从一个向量复制到结构内的另一个向量

时间:2018-01-24 02:08:34

标签: c++ vector runtime-error push-back

代码基本上以节点数(N)和邻居数(k)作为输入。然后在main函数for循环中创建节点,该节点将在结构中声明所有变量。然后main函数调用初始化函数。

初始化函数创建一个由N-1个节点编号组成的向量g,该节点编号没有创建它的节点。(即如果N = 100且第一个节点调用了初始化函数,则向量g将有2,3,....,100,如果节点2调用了初始化函数,那么向量g将有1,3,4,...,100)。

在调用random_numbers函数之后,该函数随机地移动此向量g并从中拾取前k个元素并将其返回到初始化函数,该函数又将其返回到main函数。然后main函数将使用它收到的向量更新node1.neighbor_list。

代码完成运行后,输出必须看起来像这样 假设,N = 5,k = 3.

node1
{
userID = 1; 
neighbor_list = [2,5,4];
}
node2
{
userID = 2; 
neighbor_list = [5,4,1];
}
node3 {
userID = 3; 
neighbor_list = [2,5,4];
}
node4
{
userID = 4; 
neighbor_list = [2,5,1];
}
node5
{
userID = 5; 
neighbor_list = [3,2,4];
}

我能够跟踪工作,直到初始化函数将向量返回到main函数。但我无法将此向量复制到节点的neighbor_list向量中。请帮忙!!我此刻一直在努力。

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

int N;
int k;
vector<int> g;
int j;
int i;

struct nodes
{
    int userID;
    vector<int> neighbor_list;
};

vector<int> initialization(int j);
vector<int> random_numbers(vector<int> &r);

int main()
{
    int f;
    vector<int> received;
    cout << "Enter the number of nodes: "; //enter some number
    cin >> N;
    cout << "Enter the number of neighbors: "; // enter a number less than N
    cin >> k;
    cout << endl;

    for (i = 1; i <= N; i++)
    {
        nodes node[i]; // creates a node 
        node[i].userID = i;
        cout << "creating " << i << " node" << endl;
        cout << "Calling the initialization function" << endl;
        received = initialization(i);
        cout
                << "The vector received back from the initialization function is: ";
        for (f = 1; f <= k; f++)
        {
            cout << received[f - 1] << " ";
        }
        cout << endl;
        for (f = 1; f <= k; f++)
        {
            node[i].neighbor_list.push_back(received[f - 1]); //error(guessing)
        }
        cout << "The neighbor_list for node " << i << " is " << endl;
        for (f = 1; f <= k; f++)
        {
            cout << "f: " << f;
            cout << node[i].neighbor_list[f - 1] << " ";
        }
        cout << endl;
        cout << endl;
    }
    return 0;
}

vector<int> initialization(int j) //passing the value of i 
{
    cout << "Entered the initialization function" << endl;
    cout << "The value of the node created passed in is " << j << endl;
    int f;
    vector<int> g;
    vector<int> receiver;
    int x;
    int n;
    int z;
    int rand_array[N][N - 1]; //to store the neighbor list of the node[i]
    int d;
    int a = 0;
    for (z = 1; z <= N; z++)
    {
        if (j != z)
        {
            rand_array[j - 1][a] = z; //Put all values into array except i
            cout << "The value of rand_array[" << j - 1 << "][" << a << "] is "
                    << z << endl;
            a++;
        }
        else
        {
            continue;
        }
    }
    for (f = 1; f <= N - 1; f++)
    {
        g.push_back(rand_array[j - 1][f - 1]);
    }
    cout << "vector g after push back from the rand_array is: ";
    for (f = 1; f <= N - 1; f++)
    {
        cout << g[f - 1] << " ";
    }
    cout << endl;
    cout << "Calling the random function" << endl;
    receiver = random_numbers(g);
    cout << "value of receiver vector from random_numbers function: ";
    for (f = 1; f <= k; f++)
    {
        cout << receiver[f - 1] << " ";
    }
    cout << endl;
    return receiver;
}

vector<int> random_numbers(vector<int> &r)
{
    int h;
    int f;
    cout << "Entered the random_numbers function" << endl;
    cout << "The vector passed by the initialization function is: ";
    for (f = 1; f <= N - 1; f++)
    {
        cout << r[f - 1] << " ";
    }
    cout << endl;
    vector<int> sender;
    random_shuffle(r.begin(), r.end()); //random shuffle the vector
    for (h = 1; h <= k; h++)
    {
        sender.push_back(r[h - 1]);
    }
    return sender;
}

0 个答案:

没有答案