图.h
struct Edge {
int from;
int to;
unsigned int id;
Edge(): from(0), to(0), id(0) {};
};
struct Vertex {
int label;
vector<Edge> edge;
};
class Graph: public vector<Vertex> {
int gid;
unsigned int edge_size;
};
class Trans {
public:
int tid;
vector<Graph> graph;
};
vector<Trans> database;
database
是一个全局变量,然后我在主函数中调用 run_algo(database);
。void run_algo(vector<Trans> &db) {
EdgeList edges;
for(unsigned int tid = 0; tid < db.size(); tid++) {
Trans &t = db[tid];
...
Graph g = t.graph[gid];
我想问
db
是 database
的别名,db[tid]
是一个事务 vector ,但是如果使用 Trans &t = db[tid];
和 Trans t = db[tid];
之间的区别呢,因为使用 Trans &t = db[tid];
编写示例的作者,但我认为它应该使用 Trans t = db[tid];
谢谢:)
最佳答案
后
Trans &t = db[tid];
t 和 db[tid] 中的项目完全一样,你改变 t,你改变 db[tid]
和
Trans t = db[tid];
t 只是 db[tid] 中项目的拷贝,在此处更改 t 不会更改 db[tid]。
关于c++ - "Object *obj"和 "Object &obj"的区别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9580661/