송요섭
송요섭

Reputation: 143

C++, How to pass 2 dimension Vector array to function

I am studying the vector array with my assignment, and with this assignment I have some question that is about the two dimension vector array.

THE ASSIGNMENT is : List the current process with show up their parent-child relation.

Question

I want to pass the vector value just ppids[i] to do this I write it only write

vector<process*>

If my function parameter declare like

vector<vector<process*>> 

it must write "ppids" so it is not my intent.

Error

Cannot convert 1 parameter from 'std::vector<process *,std::allocator<_Ty>>'to 'std::vector &'

So, here is my codes. (Just a part of my code)

Struct Vector :

struct process {
    string procName;
    DWORD procPid;
    DWORD procPpid;
}; 

main vector, get process information on msdn API

std::vector <process*> myProcess;

part of main below...

//
//  Group Vector by PPid

        //****(here is 2 dimension vector)****
std::vector< std::vector< process* > > ppids;

int n = 0;
int index = 1;
for (int i = 0, size = tempPid.size(); i < size; ++i) {
    ppids.push_back(vector<process*>());

    for (int j = 0, size2 = myProcess.size(); j < size2; ++j) {
        if (myProcess[j]->procPpid == tempPid[i]) {
            ppids[n].push_back(myProcess[j]);
        };
    }

    for (int k = 0, size3 = ppids[n].size(); k < size3; ++k)
    {
        _tprintf(TEXT("%d \t"), index);
        index++;

        _tprintf(TEXT("[%s]"), ppids[n][k]->procName.c_str());
        _tprintf(TEXT("[%d]"), ppids[n][k]->procPid);
        _tprintf(TEXT("[%d] \n"), ppids[n][k]->procPpid);
    }

    n++;
}
myProcess.clear();

the function is called on here.

// Combine vector
myProcess = ppids[0];
std::vector <process*> tmpProcess;
for (int i = 1, size = ppids.size(); i < size; ++i) {
    tmpProcess = combine(myProcess, ppids[i]);
    myProcess.clear();
    myProcess = tmpProcess;
}

and finally, this is my function.

vector<process*> combine(vector<process*> tempA, vector<process*> tempB) {
    std::vector <process*> alloProcess;
    for (int i = 0, size = tempA.size(); i < size; ++i) {
        if (tempA[i]->procPid == tempB[1]->procPpid)
        {
            alloProcess.push_back(tempA[i]);
            for (int j = 0, size2 = tempB.size(); j < size2; ++j) {
                alloProcess.push_back(tempB[j]);
            }
        }
        else {
            alloProcess.push_back(tempB[i]);
        }
    }
    return alloProcess;
}

Full codes on here: https://gist.github.com/anonymous/25e636086bbfacbec78508736935d3af

Upvotes: 1

Views: 130

Answers (1)

Ahmed Gamal
Ahmed Gamal

Reputation: 1706

void function(vector< vector<process> > *matrix)

Specifies a pointer, it is essentially passed by reference. However, in C++, it's better to avoid pointers and pass a reference directly:

void function(vector< vector<process> > &matrix)

and

function(matrix1); // Function call

Upvotes: 1

Related Questions