lundi 28 octobre 2019

Make a loop to append in c++?

I have below code and i would like to make loop for appending to the graph!

Example:

    graph[1].push_back(make_pair(2, 3)); 
    graph[2].push_back(make_pair(4, 8)); 
    graph[3].push_back(make_pair(3, 7)); 
    ...
    graph[6].push_back(make_pair(5, 8)); 

To:

// is there any better way!!!
int n,a,b;

    vector< pair<int,int> > graph[n+1]; 


for (int i = 0; i < n; i++){
        cin >> a >> b;

    graph[n].push_back(make_pair(a, b)); 
}

My main Function...

int main() 
{ 
    // n is number nodes
    int n = 6; 

    vector< pair<int,int> > graph[n+1]; 

    // create undirected graph 
    // first edge 
    graph[1].push_back(make_pair(2, 3)); 
    graph[2].push_back(make_pair(1, 3)); 

    // second edge 
    graph[2].push_back(make_pair(3, 4)); 
    graph[3].push_back(make_pair(2, 4)); 

    // third edge 
    graph[2].push_back(make_pair(6, 2)); 
    graph[6].push_back(make_pair(2, 2)); 

    // fourth edge 
    graph[4].push_back(make_pair(6, 6)); 
    graph[6].push_back(make_pair(4, 6)); 

    // fifth edge 
    graph[5].push_back(make_pair(6, 5)); 
    graph[6].push_back(make_pair(5, 5)); 

    cout << myFunction(graph, n); 

    return 0; 
} 

Would anyone help me to fix the issues! Is there any better way to solve the loop!.............................. tnx

Aucun commentaire:

Enregistrer un commentaire