C++ 来自相同对象的不同指针值
我有一个非常奇怪的行为,我想知道我错过了什么 我有一个C++ 来自相同对象的不同指针值,c++,pointers,C++,Pointers,我有一个非常奇怪的行为,我想知道我错过了什么 我有一个总线对象,它表示一个图形节点 我有一个表示图形边的分支对象 然后我有一个电路对象来承载总线和分支 Circuit对象具有一个函数addBus,该函数创建一个Bus对象,在向量中存储一个副本,并返回向量中指向Bus对象的指针 分支使用这些指针“记住”它连接到的两个节点。请参阅代码中的主要功能 在电路中,函数compile_all将创建一个字典,其中包含指向总线向量中的总线实例及其相应的向量索引的指针。这允许我对总线和分支进行编号,并建立邻接矩阵
总线
对象,它表示一个图形节点
我有一个表示图形边的分支
对象
然后我有一个电路
对象来承载总线和分支
Circuit
对象具有一个函数addBus
,该函数创建一个Bus
对象,在向量中存储一个副本,并返回向量中指向Bus
对象的指针
分支使用这些指针“记住”它连接到的两个节点。请参阅代码中的主要功能
在电路
中,函数compile_all
将创建一个字典,其中包含指向总线向量中的总线
实例及其相应的向量索引的指针。这允许我对总线和分支进行编号,并建立邻接矩阵等,这些矩阵将用于电路矩阵计算
问题在于,我从addBus
函数得到的Bus
实例指针与我在compile\u all
函数中再次循环总线向量时得到的实例指针不同。
/**
* Bus bar
*/
template <int n_phase> class Bus{
public:
string name;
/**
* @brief Bus constructor
*/
Bus(string name_){
name = name_;
}
};
/**
* General pi-model branch element
*/
template <int n_phase> class Branch{
public:
/**
* @brief Node connection 1
*/
Bus<n_phase> * from;
/**
* @brief Node connection 2
*/
Bus<n_phase> * to;
/**
* @brief Branch
* @param z0_
* @param z1_
*/
Branch(Bus<n_phase> * from_, Bus<n_phase> * to_){
from = from_;
to = to_;
}
};
/**
* general single-island circuit model.
*/
template <int n_phase> class Circuit{
private:
std::vector<Bus<n_phase>> buses;
std::vector<Branch<n_phase>> branches;
std::map<Bus<n_phase>*, int> bus_dict;
public:
/**
* @brief Circuit
*/
Circuit(){
}
/**
* @brief Add a bus to the circuit
* @return poiter to the added bus object
*/
Bus<n_phase>* addBus(string name_){
buses.push_back(Bus<n_phase>(name_));
return &buses[buses.size()-1];
}
/**
* @brief Add a branch to the circuit
* @return pointer to the branch objects that has been added
*/
Branch<n_phase>* addBranch(Bus<n_phase>* bf, Bus<n_phase>* bt){
branches.push_back(Branch<n_phase>(bf, bt));
return &branches.back();
}
/**
* @brief compile all the data into matrices for calculation
*/
void compile_all(){
// initialize variables
int n = buses.size();
int m = branches.size();
int f, t;
// initialize structures
bus_dict.clear();
// loop through the nodes
for (int i = 0; i < n; i++){
// create entry in the bus_dict dictionary
bus_dict[&buses[i]] = i;
}
std::cout << std::endl;
// loop through the branches
for (int i = 0; i < m ; i++){
f = bus_dict[branches[i].from];
t = bus_dict[branches[i].to];
// add connectivity relation
connectivity.set_relation(f, t, i);
}
}
};
void main(){
cout << "Test Engine 1" << endl;
const int phases = 3;
Circuit<phases> circuit;
Bus<phases>* b0 = circuit.addBus("B0");
Bus<phases>* b1 = circuit.addBus("B1");
Bus<phases>* b2 = circuit.addBus("B2");
Bus<phases>* b3 = circuit.addBus("B3");
Bus<phases>* b4 = circuit.addBus("B4");
Bus<phases>* b5 = circuit.addBus("B5");
circuit.addBranch(b0, b1);
circuit.addBranch(b0, b2);
circuit.addBranch(b2, b3);
circuit.addBranch(b3, b5);
circuit.addBranch(b2, b4);
circuit.addBranch(b4, b5);
circuit.compile_all();
};
在向量中插入对象时的指针值:
ptr: 0x555d54bfc0d0
ptr: 0x555d54bfc268
ptr: 0x555d54bfc4d0
ptr: 0x555d54bfc598
ptr: 0x555d54bfc990
ptr: 0x555d54bfca58
指针值,当我在compile_all函数中循环向量时:
set: 0x55b59aafd670
set: 0x55b59aafd738
set: 0x55b59aafd800
set: 0x55b59aafd8c8
set: 0x55b59aafd990
set: 0x55b59aafda58
我不明白为什么它们不同,因为它们指向的对象是相同的。
/**
* Bus bar
*/
template <int n_phase> class Bus{
public:
string name;
/**
* @brief Bus constructor
*/
Bus(string name_){
name = name_;
}
};
/**
* General pi-model branch element
*/
template <int n_phase> class Branch{
public:
/**
* @brief Node connection 1
*/
Bus<n_phase> * from;
/**
* @brief Node connection 2
*/
Bus<n_phase> * to;
/**
* @brief Branch
* @param z0_
* @param z1_
*/
Branch(Bus<n_phase> * from_, Bus<n_phase> * to_){
from = from_;
to = to_;
}
};
/**
* general single-island circuit model.
*/
template <int n_phase> class Circuit{
private:
std::vector<Bus<n_phase>> buses;
std::vector<Branch<n_phase>> branches;
std::map<Bus<n_phase>*, int> bus_dict;
public:
/**
* @brief Circuit
*/
Circuit(){
}
/**
* @brief Add a bus to the circuit
* @return poiter to the added bus object
*/
Bus<n_phase>* addBus(string name_){
buses.push_back(Bus<n_phase>(name_));
return &buses[buses.size()-1];
}
/**
* @brief Add a branch to the circuit
* @return pointer to the branch objects that has been added
*/
Branch<n_phase>* addBranch(Bus<n_phase>* bf, Bus<n_phase>* bt){
branches.push_back(Branch<n_phase>(bf, bt));
return &branches.back();
}
/**
* @brief compile all the data into matrices for calculation
*/
void compile_all(){
// initialize variables
int n = buses.size();
int m = branches.size();
int f, t;
// initialize structures
bus_dict.clear();
// loop through the nodes
for (int i = 0; i < n; i++){
// create entry in the bus_dict dictionary
bus_dict[&buses[i]] = i;
}
std::cout << std::endl;
// loop through the branches
for (int i = 0; i < m ; i++){
f = bus_dict[branches[i].from];
t = bus_dict[branches[i].to];
// add connectivity relation
connectivity.set_relation(f, t, i);
}
}
};
void main(){
cout << "Test Engine 1" << endl;
const int phases = 3;
Circuit<phases> circuit;
Bus<phases>* b0 = circuit.addBus("B0");
Bus<phases>* b1 = circuit.addBus("B1");
Bus<phases>* b2 = circuit.addBus("B2");
Bus<phases>* b3 = circuit.addBus("B3");
Bus<phases>* b4 = circuit.addBus("B4");
Bus<phases>* b5 = circuit.addBus("B5");
circuit.addBranch(b0, b1);
circuit.addBranch(b0, b2);
circuit.addBranch(b2, b3);
circuit.addBranch(b3, b5);
circuit.addBranch(b2, b4);
circuit.addBranch(b4, b5);
circuit.compile_all();
};
/**
*母线
*/
模板类总线{
公众:
字符串名;
/**
*@brief总线构造器
*/
总线(字符串名称){
名称=名称;
}
};
/**
*广义pi模型分支元素
*/
模板类分支{
公众:
/**
*@brief节点连接1
*/
巴士*从;
/**
*@brief节点连接2
*/
巴士*至;
/**
*@brief分支
*@param z0_
*@param z1_
*/
分支(总线*从\到\总线*){
from=from;
to=to;
}
};
/**
*一般单岛电路模型。
*/
模板类电路{
私人:
向量总线;
std::向量分支;
标准:映射总线;
公众:
/**
*@短路
*/
电路(){
}
/**
*@brief将总线添加到电路中
*@将指针返回到添加的总线对象
*/
Bus*addBus(字符串名称){
公共汽车。向后推(公共汽车(名字));
返回和总线[bus.size()-1];
}
/**
*@brief将分支添加到电路中
*@return指向已添加的分支对象的指针
*/
分支*添加分支(总线*bf,总线*bt){
分支机构。推回(分支机构(bf、bt));
return&branchs.back();
}
/**
*@brief将所有数据编译成矩阵进行计算
*/
void compile_all(){
//初始化变量
int n=总线大小();
int m=branchs.size();
int f,t;
//初始化结构
总线指令清除();
//循环遍历节点
对于(int i=0;i std::coutstd::deque或std::list应该可以工作。如果继续插入对象,就不能使用向量中元素的地址。我在Java、Python和C#中已经成功地完成了这项工作。我应该如何继续在这里实现唯一的对象指针?std::deque或std::list应该工作td::deque是1对1的替换,因此如果您认为答案是否定的,我将把它标记为解决方案。当你继续推回元素时,容器可能需要重新分配内存并移动对象。这会使所有的迭代器和指向元素的指针无效。只是为了澄清,我刚刚读到deque不能保证对象的顺序存储。这是否意味着第一个object I insert在某个点上可能不是索引0?不,这意味着deque不是像向量一样的一大块内存,而是许多较小的块。但是如果在位置0处插入,它将保持在那里。