Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
列表::排序更改值c++; 我写了一个C++的简单算法,不起作用。我发现,当我对自定义类中指向对象的指针列表进行排序时,它们会发生变化。或者更准确地说,列表将从该类更改为奇怪的随机对象 // ConsoleApplication45.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <map> #include <list> #include <math.h> using namespace std; class LocationNode; class NodeMap; class LocationNode { private: char name; int xLocation; int yLocation; //map<LocationNode*,int> neighbors; LocationNode* neighbors[100]; int neighborWeight[100]; int neighborCount; LocationNode *previous; int score; int CalcDistance(LocationNode &dest) { return (int)sqrt(pow(dest.xLocation-xLocation,2) + pow(dest.yLocation-yLocation,2)); } public: int finalScore; bool operator<(LocationNode const& rhs) const { // Use whatever makes sense for your application. return (finalScore < rhs.finalScore); } bool operator==(LocationNode const& rhs) const { // Use whatever makes sense for your application. return (name == rhs.name && xLocation == rhs.xLocation && yLocation ==rhs.yLocation ); } LocationNode(char name, int x, int y) { neighborCount = 0; this->name = name; this->xLocation = x; this->yLocation = y; } string GetPath() { if(previous!=NULL) { return string(1, name).append((*previous).GetPath()); } else { return string(1, name); } } void Connect(LocationNode &other, int weight) { this->neighbors[neighborCount] = &other; this->neighborWeight[neighborCount] = weight; } void CalcScore(LocationNode &previous, LocationNode &dest) { int index = 0; for (int i = 0; i < neighborCount; i++) { if(neighbors[i] == &previous) { index = i; } } score = previous.score + neighborCount[&index]; finalScore = previous.score + neighborCount[&index] + CalcDistance(dest); } void CalcNeighbors(LocationNode &dest) { for (int i = 0; i < neighborCount; i++) { (*neighbors[i]).CalcScore(*this,dest); } /*for (pair<LocationNode,int> node : neighbors) { node.first.CalcScore(*this,dest); }*/ } }; bool my_compare (LocationNode* a, LocationNode* b) { return a->finalScore < b->finalScore; } class NodeMap { private: static LocationNode& str; static LocationNode& dest; static LocationNode* node; static list<LocationNode*> nodes; static void loop(bool isFirst) { if(isFirst) { node = &str; } (*node).CalcNeighbors(dest); nodes.sort(my_compare); node = nodes.front(); } public: static string start() { Init(); loop(true); while(node != &dest) { loop(false); } return dest.GetPath(); } static void Init() { nodes.clear(); LocationNode A ('A',1,2); nodes.push_back(&A); LocationNode B ('B',7,1); nodes.push_back(&B); LocationNode C ('C',2,8); nodes.push_back(&C); LocationNode D ('D',4,3); nodes.push_back(&D); LocationNode E ('E',9,6); nodes.push_back(&E); LocationNode F ('F',1,2); nodes.push_back(&F); A.Connect(B,2); B.Connect(D,3); D.Connect(E,2); E.Connect(F,3); A.Connect(C,1); C.Connect(F,10); dest = F; str = A; } }; LocationNode& NodeMap::str = *(new LocationNode('A',1,2)); LocationNode& NodeMap::dest = *(new LocationNode('F',1,2)); LocationNode* NodeMap::node = &str; list<LocationNode*> NodeMap::nodes; int _tmain(int argc, _TCHAR* argv[]) { cout << &(NodeMap::start()); cin.get(); return 0; } //ConsoleApplication45.cpp:定义控制台应用程序的入口点。 // #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; 类位置节点; 类节点映射; 类位置节点 { 私人: 字符名; int-xLocation; 内部定位; //地图邻居; LocationNode*邻居[100]; 国际邻里体重[100]; 国际邻里计数; 位置节点*上一个; 智力得分; 内部计算距离(位置节点和目的地) { 返回(int)sqrt(功率(目标位置xLocation,2)+功率(目标位置,2)); } 公众: 国际金融中心; bool operatorname=名称; 这->xLocation=x; 这个->y位置=y; } 字符串GetPath() { 如果(上一个!=NULL) { 返回字符串(1,name).append((*previous.GetPath()); }否则{ 返回字符串(1,名称); } } 无效连接(位置节点和其他,内部重量) { 此->邻居[邻居计数]=&其他; 此->邻居重量[邻居计数]=重量; } 作废CalcCore(LocationNode和previous、LocationNode和dest) { int指数=0; 对于(int i=0;i_C++_Visual Studio_Sorting - Fatal编程技术网 xLocation=x; 这个->y位置=y; } 字符串GetPath() { 如果(上一个!=NULL) { 返回字符串(1,name).append((*previous.GetPath()); }否则{ 返回字符串(1,名称); } } 无效连接(位置节点和其他,内部重量) { 此->邻居[邻居计数]=&其他; 此->邻居重量[邻居计数]=重量; } 作废CalcCore(LocationNode和previous、LocationNode和dest) { int指数=0; 对于(int i=0;i,c++,visual-studio,sorting,C++,Visual Studio,Sorting" /> xLocation=x; 这个->y位置=y; } 字符串GetPath() { 如果(上一个!=NULL) { 返回字符串(1,name).append((*previous.GetPath()); }否则{ 返回字符串(1,名称); } } 无效连接(位置节点和其他,内部重量) { 此->邻居[邻居计数]=&其他; 此->邻居重量[邻居计数]=重量; } 作废CalcCore(LocationNode和previous、LocationNode和dest) { int指数=0; 对于(int i=0;i,c++,visual-studio,sorting,C++,Visual Studio,Sorting" />

列表::排序更改值c++; 我写了一个C++的简单算法,不起作用。我发现,当我对自定义类中指向对象的指针列表进行排序时,它们会发生变化。或者更准确地说,列表将从该类更改为奇怪的随机对象 // ConsoleApplication45.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <map> #include <list> #include <math.h> using namespace std; class LocationNode; class NodeMap; class LocationNode { private: char name; int xLocation; int yLocation; //map<LocationNode*,int> neighbors; LocationNode* neighbors[100]; int neighborWeight[100]; int neighborCount; LocationNode *previous; int score; int CalcDistance(LocationNode &dest) { return (int)sqrt(pow(dest.xLocation-xLocation,2) + pow(dest.yLocation-yLocation,2)); } public: int finalScore; bool operator<(LocationNode const& rhs) const { // Use whatever makes sense for your application. return (finalScore < rhs.finalScore); } bool operator==(LocationNode const& rhs) const { // Use whatever makes sense for your application. return (name == rhs.name && xLocation == rhs.xLocation && yLocation ==rhs.yLocation ); } LocationNode(char name, int x, int y) { neighborCount = 0; this->name = name; this->xLocation = x; this->yLocation = y; } string GetPath() { if(previous!=NULL) { return string(1, name).append((*previous).GetPath()); } else { return string(1, name); } } void Connect(LocationNode &other, int weight) { this->neighbors[neighborCount] = &other; this->neighborWeight[neighborCount] = weight; } void CalcScore(LocationNode &previous, LocationNode &dest) { int index = 0; for (int i = 0; i < neighborCount; i++) { if(neighbors[i] == &previous) { index = i; } } score = previous.score + neighborCount[&index]; finalScore = previous.score + neighborCount[&index] + CalcDistance(dest); } void CalcNeighbors(LocationNode &dest) { for (int i = 0; i < neighborCount; i++) { (*neighbors[i]).CalcScore(*this,dest); } /*for (pair<LocationNode,int> node : neighbors) { node.first.CalcScore(*this,dest); }*/ } }; bool my_compare (LocationNode* a, LocationNode* b) { return a->finalScore < b->finalScore; } class NodeMap { private: static LocationNode& str; static LocationNode& dest; static LocationNode* node; static list<LocationNode*> nodes; static void loop(bool isFirst) { if(isFirst) { node = &str; } (*node).CalcNeighbors(dest); nodes.sort(my_compare); node = nodes.front(); } public: static string start() { Init(); loop(true); while(node != &dest) { loop(false); } return dest.GetPath(); } static void Init() { nodes.clear(); LocationNode A ('A',1,2); nodes.push_back(&A); LocationNode B ('B',7,1); nodes.push_back(&B); LocationNode C ('C',2,8); nodes.push_back(&C); LocationNode D ('D',4,3); nodes.push_back(&D); LocationNode E ('E',9,6); nodes.push_back(&E); LocationNode F ('F',1,2); nodes.push_back(&F); A.Connect(B,2); B.Connect(D,3); D.Connect(E,2); E.Connect(F,3); A.Connect(C,1); C.Connect(F,10); dest = F; str = A; } }; LocationNode& NodeMap::str = *(new LocationNode('A',1,2)); LocationNode& NodeMap::dest = *(new LocationNode('F',1,2)); LocationNode* NodeMap::node = &str; list<LocationNode*> NodeMap::nodes; int _tmain(int argc, _TCHAR* argv[]) { cout << &(NodeMap::start()); cin.get(); return 0; } //ConsoleApplication45.cpp:定义控制台应用程序的入口点。 // #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; 类位置节点; 类节点映射; 类位置节点 { 私人: 字符名; int-xLocation; 内部定位; //地图邻居; LocationNode*邻居[100]; 国际邻里体重[100]; 国际邻里计数; 位置节点*上一个; 智力得分; 内部计算距离(位置节点和目的地) { 返回(int)sqrt(功率(目标位置xLocation,2)+功率(目标位置,2)); } 公众: 国际金融中心; bool operatorname=名称; 这->xLocation=x; 这个->y位置=y; } 字符串GetPath() { 如果(上一个!=NULL) { 返回字符串(1,name).append((*previous.GetPath()); }否则{ 返回字符串(1,名称); } } 无效连接(位置节点和其他,内部重量) { 此->邻居[邻居计数]=&其他; 此->邻居重量[邻居计数]=重量; } 作废CalcCore(LocationNode和previous、LocationNode和dest) { int指数=0; 对于(int i=0;i

列表::排序更改值c++; 我写了一个C++的简单算法,不起作用。我发现,当我对自定义类中指向对象的指针列表进行排序时,它们会发生变化。或者更准确地说,列表将从该类更改为奇怪的随机对象 // ConsoleApplication45.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <map> #include <list> #include <math.h> using namespace std; class LocationNode; class NodeMap; class LocationNode { private: char name; int xLocation; int yLocation; //map<LocationNode*,int> neighbors; LocationNode* neighbors[100]; int neighborWeight[100]; int neighborCount; LocationNode *previous; int score; int CalcDistance(LocationNode &dest) { return (int)sqrt(pow(dest.xLocation-xLocation,2) + pow(dest.yLocation-yLocation,2)); } public: int finalScore; bool operator<(LocationNode const& rhs) const { // Use whatever makes sense for your application. return (finalScore < rhs.finalScore); } bool operator==(LocationNode const& rhs) const { // Use whatever makes sense for your application. return (name == rhs.name && xLocation == rhs.xLocation && yLocation ==rhs.yLocation ); } LocationNode(char name, int x, int y) { neighborCount = 0; this->name = name; this->xLocation = x; this->yLocation = y; } string GetPath() { if(previous!=NULL) { return string(1, name).append((*previous).GetPath()); } else { return string(1, name); } } void Connect(LocationNode &other, int weight) { this->neighbors[neighborCount] = &other; this->neighborWeight[neighborCount] = weight; } void CalcScore(LocationNode &previous, LocationNode &dest) { int index = 0; for (int i = 0; i < neighborCount; i++) { if(neighbors[i] == &previous) { index = i; } } score = previous.score + neighborCount[&index]; finalScore = previous.score + neighborCount[&index] + CalcDistance(dest); } void CalcNeighbors(LocationNode &dest) { for (int i = 0; i < neighborCount; i++) { (*neighbors[i]).CalcScore(*this,dest); } /*for (pair<LocationNode,int> node : neighbors) { node.first.CalcScore(*this,dest); }*/ } }; bool my_compare (LocationNode* a, LocationNode* b) { return a->finalScore < b->finalScore; } class NodeMap { private: static LocationNode& str; static LocationNode& dest; static LocationNode* node; static list<LocationNode*> nodes; static void loop(bool isFirst) { if(isFirst) { node = &str; } (*node).CalcNeighbors(dest); nodes.sort(my_compare); node = nodes.front(); } public: static string start() { Init(); loop(true); while(node != &dest) { loop(false); } return dest.GetPath(); } static void Init() { nodes.clear(); LocationNode A ('A',1,2); nodes.push_back(&A); LocationNode B ('B',7,1); nodes.push_back(&B); LocationNode C ('C',2,8); nodes.push_back(&C); LocationNode D ('D',4,3); nodes.push_back(&D); LocationNode E ('E',9,6); nodes.push_back(&E); LocationNode F ('F',1,2); nodes.push_back(&F); A.Connect(B,2); B.Connect(D,3); D.Connect(E,2); E.Connect(F,3); A.Connect(C,1); C.Connect(F,10); dest = F; str = A; } }; LocationNode& NodeMap::str = *(new LocationNode('A',1,2)); LocationNode& NodeMap::dest = *(new LocationNode('F',1,2)); LocationNode* NodeMap::node = &str; list<LocationNode*> NodeMap::nodes; int _tmain(int argc, _TCHAR* argv[]) { cout << &(NodeMap::start()); cin.get(); return 0; } //ConsoleApplication45.cpp:定义控制台应用程序的入口点。 // #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; 类位置节点; 类节点映射; 类位置节点 { 私人: 字符名; int-xLocation; 内部定位; //地图邻居; LocationNode*邻居[100]; 国际邻里体重[100]; 国际邻里计数; 位置节点*上一个; 智力得分; 内部计算距离(位置节点和目的地) { 返回(int)sqrt(功率(目标位置xLocation,2)+功率(目标位置,2)); } 公众: 国际金融中心; bool operatorname=名称; 这->xLocation=x; 这个->y位置=y; } 字符串GetPath() { 如果(上一个!=NULL) { 返回字符串(1,name).append((*previous.GetPath()); }否则{ 返回字符串(1,名称); } } 无效连接(位置节点和其他,内部重量) { 此->邻居[邻居计数]=&其他; 此->邻居重量[邻居计数]=重量; } 作废CalcCore(LocationNode和previous、LocationNode和dest) { int指数=0; 对于(int i=0;i,c++,visual-studio,sorting,C++,Visual Studio,Sorting,你在这里做什么?你在堆上创建了对象newlocationnode('A',1,2),然后复制这个对象LocationNode A=*(newlocationnode('A',1,2)),A是复制的,在init函数中作为局部变量创建。您将局部变量对象的地址推送到节点向量中,但当init函数结束时,所有局部对象都被删除,并且在节点中有悬空指针-它们指向不存在的对象。 你可以换成 LocationNode* A = new LocationNode('A',1,2); nodes.push_back(

你在这里做什么?你在堆上创建了对象
newlocationnode('A',1,2)
,然后复制这个对象
LocationNode A=*(newlocationnode('A',1,2))
A
是复制的,在
init
函数中作为局部变量创建。您将局部变量对象的地址推送到
节点
向量中,但当
init
函数结束时,所有局部对象都被删除,并且在
节点
中有悬空指针-它们指向不存在的对象。 你可以换成

LocationNode* A = new LocationNode('A',1,2);
nodes.push_back(A);
并记住删除所有动态分配的对象。

如下行

LocationNode A = *(new LocationNode('A',1,2));
正在堆栈上创建类型为
LocationNode
的对象。新操作在堆上创建一个新对象,该对象复制到本地对象中(请参见
a
B
,…),然后泄漏

当您将本地对象添加到
节点
容器中时,一旦退出
Init
函数,
节点
中包含的数据将无法用于任何目的

在中更改上面的行:

LocationNode* A = new LocationNode('A',1,2);
然后使用

nodes.push_back(A);

它应该可以解决你90%的问题。

你从哪里学会使用Microsoft monstrosity
\u tmain
(现在它已经过去了将近20年,甚至与任何事情都相关),你为什么要在上面发布它?1)“对不起,我不能直接在这里发布图像,我没有足够的声誉。”您不需要声誉来将信息发布为文本。如果可能,您应该始终这样做。2)为什么
LocationNode A=*(new LocationNode('A',1,2));
而不是
LocationNode A('A',1,2);
?您的对象创建会产生多个内存泄漏。“[…]列表会从该类更改为奇怪的随机对象”当涉及到指针时,“随机对象”这个词所带来的任何想法都会触发对内存操作错误的搜索
nodes.push_back(A);