简体   繁体   English

使用C ++ void函数处理向量数组

[英]Manipulating a vector array with a C++ void function

In my program I have an array " vector< vector<double> > convos " with 3 rows and an arbitrary number of columns Nconv . 在我的程序中,我有一个数组“ vector< vector<double> > convos ”,具有3行和任意数量的列Nconv Each column represents a possible conversation between 2 individuals from a group with size N , and the 2nd & 3rd cells in each column contain a number labelling a particular individual. 每列代表大小为N的组中2个人之间的可能对话,每列的第2和第3单元格包含标记特定个人的数字。

So I've written a small section of code the cycle through this array and fill each column with a unique pair of integers eg. 因此,我在该数组的循环中写了一小段代码,并在每一列中填充了一对唯一的整数,例如。

convos[i][2] = 1 2 3 2 3 convos [i] [2] = 1 2 3 2 3

convos[i][1] = 0 0 0 1 1 .... and so on. convos [i] [1] = 0 0 0 1 1 ....依此类推。

The code I've written for works, but is way too messy to leave out in the my int main() , and it's probably good practice to use external functions anyway. 我为这些代码编写的代码很有效,但是太凌乱了,无法在我的int main()遗忘了,无论如何,使用外部函数可能是一个好习惯。

My problem is that when I call the function insertpairs in my main code, nothing happens... Can anyone point out something obvious that I am missing here? 我的问题是,当我在主代码中调用函数insertpairs时,什么也没发生...有人能指出我在这里明显不见的东西吗?

#include <iostream>
#include <vector>

using namespace std;
using std::vector;

void insertpairs(vector<vector<double> > convos, int Nconv, int N)
{
int a(0), b, c(0);
while (c < Nconv){
    b = a+1;
    while (b < N){
        convos[c][1] = a;
        convos[c][2] = b;
        b++;
        c++;
    }
   a++;
  }
 }

int main(){

int N(4), Nconv;
vector<vector<double> > convos;

Nconv = 0;
for (int i=1; i<=N; i++){
    Nconv = Nconv + N-i;
} // Calculates # of possible convos based on N.

insertpairs(convos, Nconv, N);
        // breakpoint here.
.....   // code continues, but rest isn't relevant.
}

If I put a breakpoint inside the void function I can see that it is working correctly, however when it finishes all the values in the array convos simply return to zero. 如果我在void函数中放置一个断点,我可以看到它正在正常工作,但是当它完成数组convos中的所有值时, convos只会返回零。 Why is this? 为什么是这样?

The vector doesn't change because you pass it by value: 向量不变,因为您通过值传递了它:

void insertpairs(vector<vector<double> > convos, int Nconv, int N)

This means that the vector will be copied to a variable that is local to the function. 这意味着矢量将被复制到函数本地的变量中。 The changes are done on that copy and it is destroyed after the function returns. 更改在该副本上完成,函数返回后销毁。 You must pass the vector by reference: 您必须通过参考传递矢量:

void insertpairs(vector<vector<double> >& convos, int Nconv, int N)

See this question for more explanation of the difference. 有关差异的更多说明,请参见此问题

Unless I'm mistaken, you pass an empty vector and add values to that? 除非我弄错了,否则您传递一个空向量并向其添加值? In that case, it might be simpler to not pass any vector but create a new one instead: 在这种情况下,不传递任何矢量而是创建一个新矢量可能会更简单:

vector<vector<double> createpairs(int Nconv, int N) {
    vector<vector<double> pairs;
    // now add the pairs here
    return pairs;
}

And in main: 而在主要方面:

vector<vector<double> > convos = createpairs(Nconv, N);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM