I decided to redo some of a few workout codes I had done a long time ago, but I used the overload of operators, and I got into that problem: how could I overload the + = operator so that it executes analogously to the join method in the code below . That is, G.join (2,1) should be equivalent to G [2] + = 1 . I left a few bits of attempts as a comment in the code, which are clearly wrong, but I'm not able to fit the type attribute of the class with the result of the operation. How could you do this overhead?
#include <bits/stdc++.h>
using namespace std;
#define M 5
struct Node{
int numedges; //edges's number of each vertice
vector <int> links; //vertice's adjacency list
Node(){
numedges =0;
}
};
class Graph{
Node* V;
const int N;
public:
Graph(): N(M) {
V = new Node[N];
}
void join (int a, int b){
V[a].links.push_back(b);
V[a].numedges++;
}
void print(){
vector<int>::iterator it;
for(int i =0; i<N;i++){
cout << i << ": ";
for(it = V[i].links.begin(); it!=V[i].links.end(); it++){
cout << *it << " ";
}
cout << endl;
}
}
//Node &operator[](const int i){
// return V[i];
//}
};
//void operator+=(vector <int> v, int x){
// v.push_back(x);
//}
int main(){
Graph G;
G.join(2,3);
G.join(1,4);
G.join(4,2);
G.join(1,2);
G.join(0,3);
G.join(2,4);
G.print();
//G[2]+=1;
return 0;
}