Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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++_Set - Fatal编程技术网

C++ 为什么我的集合中的元素没有被删除?

C++ 为什么我的集合中的元素没有被删除?,c++,set,C++,Set,那么,为什么我不能在这里删除节点 编辑:SSCCE here:使用node.deletenode并重试。在while循环中,查找距离中的最小元素,然后删除在节点中找到的键。擦除不会改变距离,因为它是一个完全不同的容器。因此,下一个循环过程将在距离上为您提供相同的元素,并且节点上的擦除调用将没有效果,因为您已经第一次删除了该数字 换句话说:您必须同时删除从远处找到的元素,否则您将始终找到相同的最小值,并且永远无法从节点中删除其余元素。。delete不是std::Set的成员,也不是int的成员,i

那么,为什么我不能在这里删除节点


编辑:SSCCE here:

使用node.deletenode并重试。

在while循环中,查找距离中的最小元素,然后删除在节点中找到的键。擦除不会改变距离,因为它是一个完全不同的容器。因此,下一个循环过程将在距离上为您提供相同的元素,并且节点上的擦除调用将没有效果,因为您已经第一次删除了该数字

换句话说:您必须同时删除从远处找到的元素,否则您将始终找到相同的最小值,并且永远无法从节点中删除其余元素。

。delete不是std::Set的成员,也不是int的成员,int是节点的类型。
map<int,int> distance;
map<int,int> previous;

map<int,int>::iterator distIt;
map<int,int>::iterator prevIt;

set<int> nodes;

[..removed..]

it = topo.begin();
while(it != topo.end())
{
    distance[it->first] = 9999;
    previous[it->first] = 9999;
    nodes.insert(it->first);
    cout << "Processed: " << it->first << endl;
    it++;
}

distance[source] = 0;

while(!nodes.empty())
{
    distIt = min_element(distance.begin(), distance.end());
    int node = distIt->first;
    cout << "MIN: " << distIt->first << "|wtih cost|" << distIt->second << endl;
    nodes.erase(node);
    cin.get();
}
Processed: 1
Processed: 1
Processed: 2
Processed: 2
Processed: 2
Processed: 3
Processed: 4
Processed: 4
Processed: 5
Processed: 5
MIN: 1|wtih cost|0

MIN: 1|wtih cost|0

MIN: 1|wtih cost|0